Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First version of ruby bindings by Medhi
[simgrid.git] / src / bindings / ruby / rb_msg_process.c
1 #include "rb_msg_process.h"
2
3 // Init Ruby
4
5 static void initRuby()
6 {
7   
8   ruby_init();
9   ruby_init_loadpath();
10   rb_require("RubyProcess.rb");
11   
12 }
13
14
15 /***********************************************
16
17 Functions for Ruby Process Management ( Up Call)
18
19 Idependant Methods
20
21 ************************************************/
22
23
24 // get Ruby Process Name
25 static VALUE process_getName( VALUE ruby_process )
26 {
27   
28    initRuby();
29   // instance = rb_funcall3(rb_const_get(rb_cObject, rb_intern("RbProcess")),  rb_intern("new"), 0, 0);
30    return rb_funcall(ruby_process,rb_intern("getName"),0);
31   
32   
33 }
34
35 // Get  Process ID
36
37 static VALUE process_getID(VALUE ruby_process)
38 {
39
40   initRuby();
41   return rb_funcall(ruby_process,rb_intern("getID"),0);
42   
43 }
44
45 // Get Bind
46
47 static VALUE process_getBind(VALUE ruby_process)
48 {
49  
50   initRuby();
51   return rb_funcall(ruby_process,rb_intern("getBind"),0);
52   
53   
54 }
55
56
57 // Set Bind
58
59 static void process_setBind(VALUE ruby_process,long bind)
60 {
61
62   initRuby();
63   
64   VALUE r_bind = LONG2FIX(bind);
65   
66   rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind);
67   
68   
69   
70 }
71
72 // isAlive
73 static VALUE process_isAlive(VALUE ruby_process)
74 {
75   
76  initRuby();
77  return rb_funcall(ruby_process,rb_intern("alive?"),0);
78   
79 }
80
81 // Kill Process
82
83 static void process_kill(VALUE ruby_process)
84 {
85   
86   initRuby();  
87   rb_funcall(ruby_process,rb_intern("kill"),0);
88   
89 }
90
91 // join Process
92
93 static void process_join( VALUE ruby_process )
94 {
95   
96  initRuby();
97  
98  rb_funcall(ruby_process,rb_intern("join"),0);
99   
100 }
101
102 // unschedule Process
103
104 static void process_unschedule( VALUE ruby_process )
105 {
106  
107   initRuby();
108   
109   rb_funcall(ruby_process,rb_intern("unschedule"),0);
110   
111 }
112
113 // schedule Process
114
115 static void process_schedule( VALUE ruby_process )
116 {
117   
118  initRuby();
119  
120  rb_funcall(ruby_process,rb_intern("run"),0);
121   
122 }
123
124
125
126
127
128 /***************************************************
129
130 Function for Native Process ( Bound ) Management
131
132 Methods Belong to MSG Module
133
134 ****************************************************/
135
136 // Process To Native
137
138 static m_process_t process_to_native(VALUE ruby_process)
139 {
140   
141   VALUE id = process_getBind(ruby_process);
142   
143   if (!id)
144   {
145    rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
146    return NULL;
147   }
148   
149   long l_id= FIX2LONG(id);
150   
151   return (m_process_t)l_id;
152   
153 }
154
155 // Bind Process
156
157 static void processBind(VALUE ruby_process,m_process_t process)
158 {
159   
160   long bind = (long)(process);
161   
162   process_setBind(ruby_process,bind);
163   
164   
165 }
166
167
168 // processCreate
169
170 static void processCreate(VALUE class,VALUE ruby_process,VALUE host)
171 {
172   
173  VALUE rbName;      // Name of Java Process instance
174  m_process_t process; // Native Process to Create
175  const char * name ; // Name of C Native Process
176  
177  
178  char alias[MAX_ALIAS_NAME + 1 ] = {0};
179   
180  msg_mailbox_t mailbox;
181  
182  rbName = process_getName(ruby_process);
183  
184  if(!rbName)
185  {
186    
187   rb_raise(rb_eRuntimeError,"Internal error : Process Name Cannot be NULL");
188   return;
189    
190  } 
191  // Allocate the data for the simulation
192  process = xbt_new0(s_m_process_t,1);
193  process->simdata = xbt_new0(s_simdata_process_t,1);
194  
195  // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
196  
197  // Bind The Ruby Process instance to The Native Process
198  processBind(ruby_process,process); 
199  
200  name = RSTRING(rbName)->ptr;
201  process->name = xbt_strdup(name);
202  
203  Data_Get_Struct(host,m_host_t,process->simdata->m_host);
204
205  if(!(process->simdata->m_host)) // Not Binded
206  {
207    free(process->simdata);
208    free(process->data);
209    free(process);
210    rb_raise(rb_eRuntimeError,"Host not bound");
211    return;
212  }
213  
214  
215  process->simdata->PID = msg_global->PID++; //  msg_global ??
216  
217  DEBUG 
218  ("fil in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p) ",
219   process->name ,process->simdata->m_host->name,process->simdata->PID,
220   process,process->simdata, process->simdata->m_host,
221   process->simdata->m_host->simdata);
222   
223  
224   process->simdata->s_process =
225   SIMIX_process_create(process->name,
226                        (xbt_main_func_t)ruby_process,
227                        (void *) process,
228                        process->simdata->m_host->simdata->smx_host->name,
229                        0,NULL,NULL);
230
231
232  DEBUG ( "context created (s_process=%p)",process->simdata->s_process);
233  
234  if (SIMIX_process_self()) { // SomeOne Created Me !!
235    process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
236  }
237  else
238  {
239    process->simdata->PPID = -1;
240  }
241   
242   process->simdata->last_errno = MSG_OK;
243   
244   // let's Add the Process to the list of the Simulation's Processes
245   
246   xbt_fifo_unshift(msg_global->process_list,process);
247   
248   sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,
249           process->name);
250           
251   mailbox = MSG_mailbox_new(alias);
252   
253 }
254
255
256 // Process Management
257
258 static void processSuspend(VALUE class,VALUE ruby_process)
259 {
260   
261   m_process_t process = process_to_native(ruby_process);
262   
263   if (!process)
264   {
265     rb_raise(rb_eRuntimeError,"Process Not Bound");
266     return;  
267   }
268   
269   // Trying to suspend The Process
270   
271   if ( MSG_OK != MSG_process_suspend(process))
272       rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
273   
274     
275 }
276
277 static void processResume(VALUE class,VALUE ruby_process)
278 {
279   
280   m_process_t process = process_to_native(ruby_process);
281   
282   if (!process)
283   {
284     rb_raise(rb_eRuntimeError,"Process not Bound");
285     return ;
286   }
287   
288   // Trying to resume the process
289   if ( MSG_OK != MSG_process_resume(process))
290     rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
291   
292 }
293
294 static VALUE processIsSuspend(VALUE class,VALUE ruby_process)
295 {
296   
297   m_process_t process = process_to_native(ruby_process);
298   
299   if (!process)
300   {
301     rb_raise (rb_eRuntimeError,"Process not Bound");
302     return;
303   }
304   
305   // 1 is The Process is Suspended , 0 Otherwise
306   if(MSG_process_is_suspended(process))
307     return Qtrue;
308   
309   return Qfalse;
310   
311 }
312
313
314 static void processKill(VALUE class,VALUE ruby_process)
315 {
316  m_process_t process = process_to_native(ruby_process);
317  
318  if(!process)
319  {
320   rb_raise (rb_eRuntimeError,"Process Not Bound");
321   return ;
322  }
323   // Delete The Global Reference / Ruby Process
324   process_kill(ruby_process);
325   // Delete the Native Process
326   MSG_process_kill(process);
327   
328 }
329
330 static VALUE processGetHost(VALUE class,VALUE ruby_process)
331 {
332   
333   m_process_t process = process_to_native(ruby_process);
334   
335   m_host_t host;
336   
337   if (!process)
338   {
339   rb_raise(rb_eRuntimeError,"Process Not Bound");
340   return Qnil; // NULL
341   }
342   
343   host = MSG_process_get_host(process);
344   
345   if(!host->data)
346   {
347    rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
348    return Qnil;
349     
350   }
351   
352    return Data_Wrap_Struct(class, 0, host_free, host);
353   
354 }