2 * Copyright 2010, The SimGrid Team. All right reserved.
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.
9 #include "bindings/ruby_bindings.h"
16 rb_require("RubyProcess.rb");
21 * Functions for Ruby Process Management (Up Calls)
25 // get Ruby Process Name
26 VALUE rb_process_getName(VALUE ruby_process) {
28 // instance = rb_funcall3(rb_const_get(rb_cObject, rb_intern("RbProcess")), rb_intern("new"), 0, 0);
29 return rb_funcall(ruby_process,rb_intern("getName"),0);
34 static VALUE process_getID(VALUE ruby_process)
38 return rb_funcall(ruby_process,rb_intern("getID"),0);
43 static VALUE process_getBind(VALUE ruby_process)
47 return rb_funcall(ruby_process,rb_intern("getBind"),0);
53 static void process_setBind(VALUE ruby_process,long bind)
57 VALUE r_bind = LONG2FIX(bind);
58 rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind);
63 static VALUE process_isAlive(VALUE ruby_process)
67 return rb_funcall(ruby_process,rb_intern("alive?"),0);
72 static void process_kill(VALUE ruby_process)
76 rb_funcall(ruby_process,rb_intern("kill"),0);
81 static void process_join( VALUE ruby_process )
85 rb_funcall(ruby_process,rb_intern("join"),0);
90 static void process_unschedule( VALUE ruby_process )
94 rb_funcall(ruby_process,rb_intern("unschedule"),0);
99 static void process_schedule( VALUE ruby_process )
103 rb_funcall(ruby_process,rb_intern("schedule"),0);
107 /***************************************************
109 Function for Native Process ( Bound ) Management
111 Methods Belong to MSG Module
113 ****************************************************/
117 static m_process_t process_to_native(VALUE ruby_process)
120 VALUE id = process_getBind(ruby_process);
123 rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
126 long l_id= FIX2LONG(id);
127 return (m_process_t)l_id;
133 static void processBind(VALUE ruby_process,m_process_t process)
136 long bind = (long)(process);
137 process_setBind(ruby_process,bind);
144 static void processCreate(VALUE class,VALUE ruby_process,VALUE host)
147 VALUE rbName; // Name of Java Process instance
148 m_process_t process; // Native Process to Create
149 const char * name ; // Name of C Native Process
150 char alias[MAX_ALIAS_NAME + 1 ] = {0};
151 msg_mailbox_t mailbox;
152 rbName = process_getName(ruby_process);
155 rb_raise(rb_eRuntimeError,"Internal error : Process Name Cannot be NULL");
158 // Allocate the data for the simulation
159 process = xbt_new0(s_m_process_t,1);
160 process->simdata = xbt_new0(s_simdata_process_t,1);
161 // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
162 // Bind The Ruby Process instance to The Native Process
163 processBind(ruby_process,process);
164 name = RSTRING(rbName)->ptr;
165 process->name = xbt_strdup(name);
166 Data_Get_Struct(host,m_host_t,process->simdata->m_host);
168 if(!(process->simdata->m_host)) // Not Binded
170 free(process->simdata);
173 rb_raise(rb_eRuntimeError,"Host not bound...while creating native process");
176 process->simdata->PID = msg_global->PID++; // msg_global ??
179 printf("fill in process %s/%s (pid=%d) %p (sd%=%p , host=%p, host->sd=%p)\n",
180 process->name , process->simdata->m_host->name,process->simdata->PID,
181 process,process->simdata, process->simdata->m_host,
182 process->simdata->m_host->simdata);
184 process->simdata->s_process =
185 SIMIX_process_create(process->name,
186 (xbt_main_func_t)ruby_process,
188 process->simdata->m_host->simdata->smx_host->name,
192 printf("context created (s_process=%p)\n",process->simdata->s_process);
194 if (SIMIX_process_self()) { // SomeOne Created Me !!
195 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
199 process->simdata->PPID = -1;
201 process->simdata->last_errno = MSG_OK;
202 // let's Add the Process to the list of the Simulation's Processes
203 xbt_fifo_unshift(msg_global->process_list,process);
204 sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,
207 mailbox = MSG_mailbox_new(alias);
212 // Process Management
214 static void processSuspend(VALUE class,VALUE ruby_process)
217 m_process_t process = process_to_native(ruby_process);
221 rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
225 // Trying to suspend The Process
227 if ( MSG_OK != MSG_process_suspend(process))
228 rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
233 static void processResume(VALUE class,VALUE ruby_process)
235 m_process_t process = process_to_native(ruby_process);
238 rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
241 // Trying to resume the process
242 if ( MSG_OK != MSG_process_resume(process))
243 rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
247 static VALUE processIsSuspend(VALUE class,VALUE ruby_process)
250 m_process_t process = process_to_native(ruby_process);
253 rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
257 // 1 is The Process is Suspended , 0 Otherwise
258 if(MSG_process_is_suspended(process))
265 static void processKill(VALUE class,VALUE ruby_process)
267 m_process_t process = process_to_native(ruby_process);
271 rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
274 // Delete The Global Reference / Ruby Process
275 process_kill(ruby_process);
276 // Delete the Native Process
277 MSG_process_kill(process);
281 static VALUE processGetHost(VALUE class,VALUE ruby_process)
284 m_process_t process = process_to_native(ruby_process);
289 rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
293 host = MSG_process_get_host(process);
297 rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
301 return Data_Wrap_Struct(class, 0, host_free, host);
305 static void processExit(VALUE class,VALUE ruby_process)
308 m_process_t process = process_to_native(ruby_process);
311 rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
314 SIMIX_context_stop(SIMIX_process_self()->context);