Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Yet some more cleanups in ruby (and this time, I go to bed)
[simgrid.git] / src / bindings / ruby / rb_msg_process.c
1 /*
2  * Copyright 2010, The SimGrid Team. All right reserved.
3  *
4  * This program is free software; you can redistribute 
5  * it and/or modify it under the terms of the license 
6  *(GNU LGPL) which comes with this package. 
7  */
8
9 #include "msg/private.h" /* s_simdata_process_t */
10 #include "bindings/ruby_bindings.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ruby,bindings,"Ruby Bindings");
13
14 /*
15  * Functions for Ruby Process Management (Up Calls)
16  */
17
18 // get Ruby Process Name
19 VALUE rb_process_getName(VALUE ruby_process) {
20   return rb_funcall(ruby_process,rb_intern("getName"),0);
21 }
22
23 // Get  Process ID
24 VALUE rb_process_getID(VALUE ruby_process) {
25   return rb_funcall(ruby_process,rb_intern("getID"),0);
26 }
27
28 // Get Bind
29 VALUE rb_process_getBind(VALUE ruby_process) {
30   return rb_funcall(ruby_process,rb_intern("getBind"),0);
31 }
32
33
34 // Set Bind
35 void rb_process_setBind(VALUE ruby_process,long bind) {
36   VALUE r_bind = LONG2FIX(bind);
37   rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind);
38 }
39
40 // isAlive
41 VALUE rb_process_isAlive(VALUE ruby_process) {
42   return rb_funcall(ruby_process,rb_intern("alive?"),0);
43 }
44
45 // Kill Process
46 void rb_process_kill_up(VALUE ruby_process) {
47   rb_funcall(ruby_process,rb_intern("kill"),0);
48 }
49
50 // join Process
51 void rb_process_join( VALUE ruby_process ) {
52   rb_funcall(ruby_process,rb_intern("join"),0);
53 }
54
55 // unschedule Process
56 void rb_process_unschedule( VALUE ruby_process ) {
57   rb_funcall(ruby_process,rb_intern("unschedule"),0);
58 }
59
60 // schedule Process
61 void rb_process_schedule( VALUE ruby_process ) {
62   rb_funcall(ruby_process,rb_intern("schedule"),0);
63 }
64
65 /***************************************************
66
67 Function for Native Process ( Bound ) Management
68
69 Methods Belong to MSG Module
70
71  ****************************************************/
72
73 // Process To Native
74 m_process_t rb_process_to_native(VALUE ruby_process) {
75   VALUE id = rb_process_getBind(ruby_process);
76   if (!id) {
77     rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
78     return NULL;
79   }
80   long l_id= FIX2LONG(id);
81   return (m_process_t)l_id;
82 }
83
84 // Bind Process
85 void rb_process_bind(VALUE ruby_process,m_process_t process) {
86   long bind = (long)(process);
87   rb_process_setBind(ruby_process,bind);
88 }
89
90
91 // processCreate
92 // FIXME: don't mess with MSG internals here, use MSG_process_create_with_arguments()
93
94 void rb_process_create(VALUE class,VALUE ruby_process,VALUE host) {
95   VALUE rbName;      // Name of ruby Process instance
96   m_process_t process; // Native Process to Create
97   const char * name ; // Name of C Native Process
98   rbName = rb_process_getName(ruby_process);
99
100   if(!rbName)
101     rb_raise(rb_eRuntimeError,"Internal error: Process name cannot be NULL");
102
103   // Allocate the data for the simulation
104   process = xbt_new0(s_m_process_t,1);
105   process->simdata = xbt_new0(s_simdata_process_t,1);
106   // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
107   // Bind The Ruby Process instance to The Native Process
108   rb_process_bind(ruby_process,process);
109   name = RSTRING(rbName)->ptr;
110   process->name = xbt_strdup(name);
111   Data_Get_Struct(host,s_m_host_t,process->simdata->m_host);
112
113   if(!(process->simdata->m_host)) { // Not Binded
114     free(process->simdata);
115     free(process->data);
116     free(process);
117     rb_raise(rb_eRuntimeError,"Host not bound while creating native process");
118   }
119   process->simdata->PID = msg_global->PID++; //  msg_global ??
120
121   DEBUG7("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)",
122       process->name , process->simdata->m_host->name,process->simdata->PID,
123       process,process->simdata, process->simdata->m_host,
124       process->simdata->m_host->simdata);
125
126   /* FIXME: that's mainly for debugging. We could only allocate this if XBT_LOG_ISENABLED(ruby,debug) is true since I guess this leaks */
127   char **argv=xbt_new(char*,2);
128   argv[0] = bprintf("%s@%s",process->name,process->simdata->m_host->simdata->smx_host->name);
129   argv[1] = NULL;
130   process->simdata->s_process =
131       SIMIX_process_create(process->name,
132           (xbt_main_func_t)ruby_process,
133           (void *) process,
134           process->simdata->m_host->simdata->smx_host->name,
135           1,argv,NULL);
136
137   DEBUG1("context created (s_process=%p)",process->simdata->s_process);
138
139   if (SIMIX_process_self()) { // SomeOne Created Me !!
140     process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
141   } else {
142     process->simdata->PPID = -1;
143   }
144   process->simdata->last_errno = MSG_OK;
145   // let's Add the Process to the list of the Simulation's Processes
146   xbt_fifo_unshift(msg_global->process_list,process);
147 }
148
149
150 // Process Management
151 void rb_process_suspend(VALUE class,VALUE ruby_process) {
152
153   m_process_t process = rb_process_to_native(ruby_process);
154
155   if (!process) {
156     rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
157     return;  
158   }
159
160   // Trying to suspend The Process
161
162   if ( MSG_OK != MSG_process_suspend(process))
163     rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
164 }
165
166 void rb_process_resume(VALUE class,VALUE ruby_process) {
167   m_process_t process = rb_process_to_native(ruby_process);
168   if (!process) {
169     rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
170     return ;
171   }
172
173   // Trying to resume the process
174   if ( MSG_OK != MSG_process_resume(process))
175     rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
176 }
177
178 VALUE rb_process_isSuspended(VALUE class,VALUE ruby_process) {
179   m_process_t process = rb_process_to_native(ruby_process);
180   if (!process) {
181     rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
182     return Qfalse;
183   }
184
185   if(MSG_process_is_suspended(process))
186     return Qtrue;
187   return Qfalse;
188 }
189
190 void rb_process_kill_down(VALUE class,VALUE ruby_process) {
191   m_process_t process = rb_process_to_native(ruby_process);
192
193   if(!process) {
194     rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
195     return;
196   }
197   // Delete The Global Reference / Ruby Process
198   rb_process_kill_up(ruby_process);
199   // Delete the Native Process
200   MSG_process_kill(process);
201 }
202
203 VALUE rb_process_getHost(VALUE class,VALUE ruby_process) {
204   m_process_t process = rb_process_to_native(ruby_process);
205   m_host_t host;
206
207   if (!process) {
208     rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
209     return Qnil; // NULL
210   }
211
212   host = MSG_process_get_host(process);
213
214   if(!host->data) {
215     rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
216     return Qnil;
217   }
218
219   return Data_Wrap_Struct(class, 0, rb_host_free, host);
220 }
221
222 void rb_process_exit(VALUE class,VALUE ruby_process) {
223   m_process_t process = rb_process_to_native(ruby_process);
224   if(!process) {
225     rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
226     return;
227   }
228   SIMIX_context_stop(SIMIX_process_self()->context);
229 }