Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More ruby cleanups: stop defining everything static, and prefix functions with rb_...
[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 // Init Ruby
15 void initRuby(void) {
16   ruby_init();
17   ruby_init_loadpath();
18   rb_require("RubyProcess.rb");
19 }
20
21
22 /*
23  * Functions for Ruby Process Management (Up Calls)
24  */
25
26
27 // get Ruby Process Name
28 VALUE rb_process_getName(VALUE ruby_process) {
29   initRuby();
30   // instance = rb_funcall3(rb_const_get(rb_cObject, rb_intern("RbProcess")),  rb_intern("new"), 0, 0);
31   return rb_funcall(ruby_process,rb_intern("getName"),0);
32
33 }
34
35 // Get  Process ID
36 VALUE rb_process_getID(VALUE ruby_process) {
37   initRuby();
38   return rb_funcall(ruby_process,rb_intern("getID"),0);
39 }
40
41 // Get Bind
42 VALUE rb_process_getBind(VALUE ruby_process) {
43   initRuby();
44   return rb_funcall(ruby_process,rb_intern("getBind"),0);
45 }
46
47
48 // Set Bind
49 void rb_process_setBind(VALUE ruby_process,long bind) {
50   initRuby();
51   VALUE r_bind = LONG2FIX(bind);
52   rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind);
53 }
54
55 // isAlive
56 VALUE rb_process_isAlive(VALUE ruby_process) {
57   initRuby();
58   return rb_funcall(ruby_process,rb_intern("alive?"),0);
59 }
60
61 // Kill Process
62 void rb_process_kill_up(VALUE ruby_process) {
63   initRuby();  
64   rb_funcall(ruby_process,rb_intern("kill"),0);
65 }
66
67 // join Process
68 void rb_process_join( VALUE ruby_process ) {
69   initRuby();
70   rb_funcall(ruby_process,rb_intern("join"),0);
71 }
72
73 // unschedule Process
74 void rb_process_unschedule( VALUE ruby_process ) {
75   initRuby();
76   rb_funcall(ruby_process,rb_intern("unschedule"),0);
77 }
78
79 // schedule Process
80 void rb_process_schedule( VALUE ruby_process ) {
81   initRuby();
82   rb_funcall(ruby_process,rb_intern("schedule"),0);
83 }
84
85 /***************************************************
86
87 Function for Native Process ( Bound ) Management
88
89 Methods Belong to MSG Module
90
91  ****************************************************/
92
93 // Process To Native
94 m_process_t rb_process_to_native(VALUE ruby_process) {
95   VALUE id = rb_process_getBind(ruby_process);
96   if (!id) {
97     rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
98     return NULL;
99   }
100   long l_id= FIX2LONG(id);
101   return (m_process_t)l_id;
102 }
103
104 // Bind Process
105 void rb_process_bind(VALUE ruby_process,m_process_t process) {
106   long bind = (long)(process);
107   rb_process_setBind(ruby_process,bind);
108 }
109
110
111 // processCreate
112
113 void rb_process_create(VALUE class,VALUE ruby_process,VALUE host) {
114   VALUE rbName;      // Name of Java Process instance
115   m_process_t process; // Native Process to Create
116   const char * name ; // Name of C Native Process
117   char alias[MAX_ALIAS_NAME + 1 ] = {0};
118   msg_mailbox_t mailbox;
119   rbName = rb_process_getName(ruby_process);
120
121   if(!rbName) {
122     rb_raise(rb_eRuntimeError,"Internal error : Process Name Cannot be NULL");
123     return;
124   }
125   // Allocate the data for the simulation
126   process = xbt_new0(s_m_process_t,1);
127   process->simdata = xbt_new0(s_simdata_process_t,1);
128   // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
129   // Bind The Ruby Process instance to The Native Process
130   rb_process_bind(ruby_process,process);
131   name = RSTRING(rbName)->ptr;
132   process->name = xbt_strdup(name);
133   Data_Get_Struct(host,m_host_t,process->simdata->m_host);
134
135   if(!(process->simdata->m_host)) // Not Binded
136   {
137     free(process->simdata);
138     free(process->data);
139     free(process);
140     rb_raise(rb_eRuntimeError,"Host not bound...while creating native process");
141     return;
142   }
143   process->simdata->PID = msg_global->PID++; //  msg_global ??
144
145   DEBUG7("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)\n",
146       process->name , process->simdata->m_host->name,process->simdata->PID,
147       process,process->simdata, process->simdata->m_host,
148       process->simdata->m_host->simdata);
149
150   process->simdata->s_process =
151       SIMIX_process_create(process->name,
152           (xbt_main_func_t)ruby_process,
153           (void *) process,
154           process->simdata->m_host->simdata->smx_host->name,
155           0,NULL,NULL);
156
157  DEBUG1("context created (s_process=%p)\n",process->simdata->s_process);
158
159   if (SIMIX_process_self()) { // SomeOne Created Me !!
160     process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
161   }
162   else
163   {
164     process->simdata->PPID = -1;
165   }
166   process->simdata->last_errno = MSG_OK;
167   // let's Add the Process to the list of the Simulation's Processes
168   xbt_fifo_unshift(msg_global->process_list,process);
169   sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,
170       process->name);
171
172   mailbox = MSG_mailbox_new(alias);
173
174 }
175
176
177 // Process Management
178 void rb_process_suspend(VALUE class,VALUE ruby_process) {
179
180   m_process_t process = rb_process_to_native(ruby_process);
181
182   if (!process) {
183     rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
184     return;  
185   }
186
187   // Trying to suspend The Process
188
189   if ( MSG_OK != MSG_process_suspend(process))
190     rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
191 }
192
193 void rb_process_resume(VALUE class,VALUE ruby_process) {
194   m_process_t process = rb_process_to_native(ruby_process);
195   if (!process) {
196     rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
197     return ;
198   }
199
200   // Trying to resume the process
201   if ( MSG_OK != MSG_process_resume(process))
202     rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
203 }
204
205 VALUE rb_process_isSuspended(VALUE class,VALUE ruby_process) {
206   m_process_t process = rb_process_to_native(ruby_process);
207   if (!process) {
208     rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
209     return Qfalse;
210   }
211
212   if(MSG_process_is_suspended(process))
213     return Qtrue;
214   return Qfalse;
215 }
216
217 void rb_process_kill_down(VALUE class,VALUE ruby_process) {
218   m_process_t process = rb_process_to_native(ruby_process);
219
220   if(!process) {
221     rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
222     return;
223   }
224   // Delete The Global Reference / Ruby Process
225   rb_process_kill_up(ruby_process);
226   // Delete the Native Process
227   MSG_process_kill(process);
228 }
229
230 VALUE rb_process_getHost(VALUE class,VALUE ruby_process) {
231   m_process_t process = rb_process_to_native(ruby_process);
232   m_host_t host;
233
234   if (!process) {
235     rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
236     return Qnil; // NULL
237   }
238
239   host = MSG_process_get_host(process);
240
241   if(!host->data) {
242     rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
243     return Qnil;
244   }
245
246   return Data_Wrap_Struct(class, 0, rb_host_free, host);
247 }
248
249 void rb_process_exit(VALUE class,VALUE ruby_process) {
250   m_process_t process = rb_process_to_native(ruby_process);
251   if(!process) {
252     rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
253     return;
254   }
255   SIMIX_context_stop(SIMIX_process_self()->context);
256 }