Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
acf55989f926991a02309f9a5af5bd91cb00a306
[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); ( This cause a bug !! is it really necessary ?!! not really sure !! )
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 // set Data : For the Moment , we will consider Data as asimple String ( char * )
27 void rb_task_set_data(VALUE class,VALUE task,VALUE data)
28 {
29  const char *str_data = RSTRING(data)->ptr;
30  m_task_t tk;
31  Data_Get_Struct(task, s_m_task_t, tk);
32  tk->data = (void*)str_data;
33  
34 }
35
36 // get Data
37 VALUE rb_task_get_data(VALUE class,VALUE task)
38 {
39   m_task_t tk;
40   Data_Get_Struct(task, s_m_task_t, tk);
41   return rb_str_new2(tk->data);
42   
43 }
44
45
46 //Get Computation Size
47 VALUE rb_task_comp(VALUE class,VALUE task) {
48   double size;
49   m_task_t tk;
50   // Wrap Ruby Value to m_task_t struct
51   Data_Get_Struct(task, s_m_task_t, tk);
52   size = MSG_task_get_compute_duration(tk);
53   return rb_float_new(size);
54 }
55
56 //Get Name
57 VALUE rb_task_name(VALUE class,VALUE task) {
58
59   // Wrap Ruby Value to m_task_t struct
60   m_task_t tk; 
61   Data_Get_Struct(task, s_m_task_t, tk);
62   return rb_str_new2(MSG_task_get_name(tk));
63 }
64
65 // Execute Task
66 VALUE rb_task_execute(VALUE class,VALUE task) {
67
68   // Wrap Ruby Value to m_task_t struct
69   m_task_t tk;
70   Data_Get_Struct(task, s_m_task_t, tk);
71   return INT2NUM(MSG_task_execute(tk));
72 }
73
74 // Sending Task
75 void rb_task_send(VALUE class,VALUE task,VALUE mailbox) {
76
77   MSG_error_t rv;
78   // Wrap Ruby Value to m_task_t struct
79   m_task_t tk;
80   Data_Get_Struct(task, s_m_task_t, tk);
81   DEBUG1("Sending task %p",tk);
82   rv = MSG_task_send(tk,RSTRING(mailbox)->ptr);
83   if(rv != MSG_OK)
84   {
85     if (rv == MSG_TRANSFER_FAILURE )
86       rb_raise(rb_eRuntimeError,"Transfer failure while Sending");
87     else if ( rv == MSG_HOST_FAILURE )
88       rb_raise(rb_eRuntimeError,"Host failure while Sending");
89     else if ( rv == MSG_TIMEOUT )
90       rb_raise(rb_eRuntimeError,"Timeout failure while Sending");
91     else 
92       rb_raise(rb_eRuntimeError,"MSG_task_send failed");
93   }
94 }
95
96 // Receiving Task (returns a Task)
97 VALUE rb_task_receive(VALUE class, VALUE mailbox) {
98   // We must put the location where we copy the task
99   // pointer to on the heap, because the stack may move
100   // during the context switches (damn ruby internals)
101   m_task_t *ptask = malloc(sizeof(m_task_t));
102   m_task_t task;
103   *ptask = NULL;
104   DEBUG2("Receiving a task on mailbox '%s', store it into %p",RSTRING(mailbox)->ptr,&task);
105   MSG_task_receive(ptask,RSTRING(mailbox)->ptr);
106   task = *ptask;
107   free(ptask);
108   return Data_Wrap_Struct(class, 0, rb_task_free, task);
109 }
110
111 // It Return a Native Process ( m_process_t )
112 VALUE rb_task_sender(VALUE class,VALUE task) {
113   m_task_t tk;
114   Data_Get_Struct(task,s_m_task_t,tk);
115   THROW_UNIMPLEMENTED;
116   return 0;//MSG_task_get_sender(tk);
117 }
118
119 // it return a Host 
120 VALUE rb_task_source(VALUE class,VALUE task) {
121   m_task_t tk;
122   Data_Get_Struct(task,s_m_task_t,tk);
123
124   m_host_t host = MSG_task_get_source(tk);
125   if(!host->data) {
126     rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
127     return Qnil;
128   }
129   THROW_UNIMPLEMENTED;
130   return 0;//host;
131 }
132
133 // Return Boolean
134 VALUE rb_task_listen(VALUE class,VALUE task,VALUE alias) {
135   m_task_t tk;
136   const char *p_alias;
137   int rv;
138
139   Data_Get_Struct(task,s_m_task_t,tk);
140   p_alias = RSTRING(alias)->ptr;
141
142   rv = MSG_task_listen(p_alias);
143
144   if(rv) return Qtrue;
145
146   return Qfalse;
147 }
148
149 // return Boolean
150 VALUE rb_task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host) {
151
152   m_task_t tk;
153   m_host_t ht;
154   const char *p_alias;
155   int rv;
156
157   Data_Get_Struct(task,s_m_task_t,tk);
158   Data_Get_Struct(host,s_m_host_t,ht);
159   p_alias = RSTRING(alias)->ptr;
160
161   rv = MSG_task_listen_from_host(p_alias,ht);
162
163   if (rv)
164     return Qtrue;
165   return Qfalse;
166 }
167
168
169 // Set Priority
170 void rb_task_set_priority(VALUE class,VALUE task,VALUE priority)
171 {
172   
173   m_task_t tk;
174   double prt = NUM2DBL(priority);
175   Data_Get_Struct(task,s_m_task_t,tk);
176   MSG_task_set_priority(tk,prt);
177   
178 }
179
180 // Cancel
181 void rb_task_cancel(VALUE class,VALUE task)
182 {
183  m_task_t tk;
184  Data_Get_Struct(task,s_m_task_t,tk);
185  MSG_task_cancel(tk);
186   
187 }
188