Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
855270f2056b674c13bf101f9635368b6bac0a63
[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 //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   MSG_error_t rv;
58   // Wrap Ruby Value to m_task_t struct
59   m_task_t tk;
60   Data_Get_Struct(task, s_m_task_t, tk);
61   MSG_task_set_data(tk,(void*)task);
62   DEBUG1("Sending task %p",tk);
63   rv = MSG_task_send(tk,RSTRING(mailbox)->ptr);
64   if(rv != MSG_OK)
65   {
66     if (rv == MSG_TRANSFER_FAILURE )
67       rb_raise(rb_eRuntimeError,"Transfer failure while Sending");
68     else if ( rv == MSG_HOST_FAILURE )
69       rb_raise(rb_eRuntimeError,"Host failure while Sending");
70     else if ( rv == MSG_TIMEOUT )
71       rb_raise(rb_eRuntimeError,"Timeout failure while Sending");
72     else 
73       rb_raise(rb_eRuntimeError,"MSG_task_send failed");
74   }
75 }
76
77 // Receiving Task (returns a Task)
78 VALUE rb_task_receive(VALUE class, VALUE mailbox) {
79   // We must put the location where we copy the task
80   // pointer to on the heap, because the stack may move
81   // during the context switches (damn ruby internals)
82   m_task_t *ptask = malloc(sizeof(m_task_t));
83   m_task_t task;
84   *ptask = NULL;
85   DEBUG2("Receiving a task on mailbox '%s', store it into %p",RSTRING(mailbox)->ptr,&task);
86   MSG_task_receive(ptask,RSTRING(mailbox)->ptr);
87   task = *ptask;
88   free(ptask);
89   return (VALUE)MSG_task_get_data(task);
90 }
91
92 // It Return a Native Process ( m_process_t )
93 VALUE rb_task_sender(VALUE class,VALUE task) {
94   m_task_t tk;
95   Data_Get_Struct(task,s_m_task_t,tk);
96   THROW_UNIMPLEMENTED;
97   return 0;//MSG_task_get_sender(tk);
98 }
99
100 // it return a Host 
101 VALUE rb_task_source(VALUE class,VALUE task) {
102   m_task_t tk;
103   Data_Get_Struct(task,s_m_task_t,tk);
104
105   m_host_t host = MSG_task_get_source(tk);
106   if(!host->data) {
107     rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
108     return Qnil;
109   }
110   THROW_UNIMPLEMENTED;
111   return 0;//host;
112 }
113
114 // Return Boolean
115 VALUE rb_task_listen(VALUE class,VALUE task,VALUE alias) {
116   m_task_t tk;
117   const char *p_alias;
118   int rv;
119
120   Data_Get_Struct(task,s_m_task_t,tk);
121   p_alias = RSTRING(alias)->ptr;
122
123   rv = MSG_task_listen(p_alias);
124
125   if(rv) return Qtrue;
126
127   return Qfalse;
128 }
129
130 // return Boolean
131 VALUE rb_task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host) {
132
133   m_task_t tk;
134   m_host_t ht;
135   const char *p_alias;
136   int rv;
137
138   Data_Get_Struct(task,s_m_task_t,tk);
139   Data_Get_Struct(host,s_m_host_t,ht);
140   p_alias = RSTRING(alias)->ptr;
141
142   rv = MSG_task_listen_from_host(p_alias,ht);
143
144   if (rv)
145     return Qtrue;
146   return Qfalse;
147 }
148
149
150 // Set Priority
151 void rb_task_set_priority(VALUE class,VALUE task,VALUE priority)
152 {
153   
154   m_task_t tk;
155   double prt = NUM2DBL(priority);
156   Data_Get_Struct(task,s_m_task_t,tk);
157   MSG_task_set_priority(tk,prt);
158   
159 }
160
161 // Cancel
162 void rb_task_cancel(VALUE class,VALUE task)
163 {
164  m_task_t tk;
165  Data_Get_Struct(task,s_m_task_t,tk);
166  MSG_task_cancel(tk);
167   
168 }
169