Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ad934b47a3d6c296fef6720d632efd9598eb74a6
[simgrid.git] / src / bindings / ruby / rb_msg_task.c
1 /* Task-related bindings to ruby  */
2
3 /* Copyright 2010. The SimGrid Team. All right reserved. */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "bindings/ruby_bindings.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
12
13 // Free Method
14 void rb_task_free(m_task_t tk) {
15   MSG_task_destroy(tk);
16 }
17
18 // New Method
19 VALUE rb_task_new(VALUE class, VALUE name,VALUE comp_size,VALUE comm_size) {
20   m_task_t task = MSG_task_create(RSTRING(name)->ptr,NUM2INT(comp_size),NUM2INT(comm_size),NULL);
21   // Wrap m_task_t to a Ruby Value
22   return Data_Wrap_Struct(class, 0, rb_task_free, task);
23
24 }
25
26 //Get Computation Size
27 VALUE rb_task_comp(VALUE class,VALUE task) {
28   double size;
29   m_task_t tk;
30   // Wrap Ruby Value to m_task_t struct
31   Data_Get_Struct(task, s_m_task_t, tk);
32   size = MSG_task_get_compute_duration(tk);
33   return rb_float_new(size);
34 }
35
36 //Get Name
37 VALUE rb_task_name(VALUE class,VALUE task) {
38
39   // Wrap Ruby Value to m_task_t struct
40   m_task_t tk; 
41   Data_Get_Struct(task, s_m_task_t, tk);
42   return rb_str_new2(MSG_task_get_name(tk));
43 }
44
45 // Execute Task
46 VALUE rb_task_execute(VALUE class,VALUE task) {
47
48   // Wrap Ruby Value to m_task_t struct
49   m_task_t tk;
50   Data_Get_Struct(task, s_m_task_t, tk);
51   return INT2NUM(MSG_task_execute(tk));
52 }
53
54 // Sending Task
55 void rb_task_send(VALUE class,VALUE task,VALUE mailbox) {
56
57   // Wrap Ruby Value to m_task_t struct
58   m_task_t tk;
59   Data_Get_Struct(task, s_m_task_t, tk);
60   INFO1("Sending task %p",tk);
61   int res = MSG_task_send(tk,RSTRING(mailbox)->ptr);
62   if(res != MSG_OK)
63     rb_raise(rb_eRuntimeError,"MSG_task_send failed");
64 }
65
66 // Receiving Task (returns a Task)
67 VALUE rb_task_receive(VALUE class, VALUE mailbox) {
68   // We must put the location where we copy the task
69   // pointer to on the heap, because the stack may move
70   // during the context switches (damn ruby internals)
71   m_task_t *ptask = malloc(sizeof(m_task_t));
72   m_task_t task;
73   *ptask = NULL;
74   INFO2("Receiving a task on mailbox '%s', store it into %p",RSTRING(mailbox)->ptr,&task);
75   MSG_task_receive(ptask,RSTRING(mailbox)->ptr);
76   task = *ptask;
77   free(ptask);
78   return Data_Wrap_Struct(class, 0, rb_task_free, task);
79 }
80
81 // It Return a Native Process ( m_process_t )
82 VALUE rb_task_sender(VALUE class,VALUE task) {
83   m_task_t tk;
84   Data_Get_Struct(task,s_m_task_t,tk);
85   THROW_UNIMPLEMENTED;
86   return 0;//MSG_task_get_sender(tk);
87 }
88
89 // it return a Host 
90 VALUE rb_task_source(VALUE class,VALUE task) {
91   m_task_t tk;
92   Data_Get_Struct(task,s_m_task_t,tk);
93
94   m_host_t host = MSG_task_get_source(tk);
95   if(!host->data) {
96     rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
97     return Qnil;
98   }
99   THROW_UNIMPLEMENTED;
100   return 0;//host;
101 }
102
103 // Return Boolean
104 VALUE rb_task_listen(VALUE class,VALUE task,VALUE alias) {
105   m_task_t tk;
106   const char *p_alias;
107   int rv;
108
109   Data_Get_Struct(task,s_m_task_t,tk);
110   p_alias = RSTRING(alias)->ptr;
111
112   rv = MSG_task_listen(p_alias);
113
114   if(rv) return Qtrue;
115
116   return Qfalse;
117 }
118
119 // return Boolean
120 VALUE rb_task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host) {
121
122   m_task_t tk;
123   m_host_t ht;
124   const char *p_alias;
125   int rv;
126
127   Data_Get_Struct(task,s_m_task_t,tk);
128   Data_Get_Struct(host,s_m_host_t,ht);
129   p_alias = RSTRING(alias)->ptr;
130
131   rv = MSG_task_listen_from_host(p_alias,ht);
132
133   if (rv)
134     return Qtrue;
135   return Qfalse;
136 }