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 "msg/private.h" /* s_simdata_process_t */
10 #include "bindings/ruby_bindings.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ruby,bindings,"Ruby Bindings");
15 * Functions for Ruby Process Management (Up Calls)
18 // get Ruby Process Name
19 VALUE rb_process_getName(VALUE ruby_process) {
20 return rb_funcall(ruby_process,rb_intern("getName"),0);
24 VALUE rb_process_getID(VALUE ruby_process) {
25 return rb_funcall(ruby_process,rb_intern("getID"),0);
29 VALUE rb_process_getBind(VALUE ruby_process) {
30 return rb_funcall(ruby_process,rb_intern("getBind"),0);
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);
41 VALUE rb_process_isAlive(VALUE ruby_process) {
42 return rb_funcall(ruby_process,rb_intern("alive?"),0);
46 void rb_process_kill_up(VALUE ruby_process) {
47 rb_funcall(ruby_process,rb_intern("kill"),0);
51 void rb_process_join( VALUE ruby_process ) {
52 rb_funcall(ruby_process,rb_intern("join"),0);
56 void rb_process_unschedule( VALUE ruby_process ) {
57 rb_funcall(ruby_process,rb_intern("unschedule"),0);
61 void rb_process_schedule( VALUE ruby_process ) {
62 rb_funcall(ruby_process,rb_intern("schedule"),0);
65 /***************************************************
67 Function for Native Process ( Bound ) Management
69 Methods Belong to MSG Module
71 ****************************************************/
74 m_process_t rb_process_to_native(VALUE ruby_process) {
75 VALUE id = rb_process_getBind(ruby_process);
77 rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
80 long l_id= FIX2LONG(id);
81 return (m_process_t)l_id;
85 void rb_process_bind(VALUE ruby_process,m_process_t process) {
86 long bind = (long)(process);
87 rb_process_setBind(ruby_process,bind);
93 void rb_process_create(VALUE class,VALUE ruby_process,VALUE host) {
94 VALUE rbName; // Name of Java Process instance
95 m_process_t process; // Native Process to Create
96 const char * name ; // Name of C Native Process
97 char alias[MAX_ALIAS_NAME + 1 ] = {0};
98 msg_mailbox_t mailbox;
99 rbName = rb_process_getName(ruby_process);
102 rb_raise(rb_eRuntimeError,"Internal error : Process Name Cannot be NULL");
105 // Allocate the data for the simulation
106 process = xbt_new0(s_m_process_t,1);
107 process->simdata = xbt_new0(s_simdata_process_t,1);
108 // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
109 // Bind The Ruby Process instance to The Native Process
110 rb_process_bind(ruby_process,process);
111 name = RSTRING(rbName)->ptr;
112 process->name = xbt_strdup(name);
113 Data_Get_Struct(host,s_m_host_t,process->simdata->m_host);
115 if(!(process->simdata->m_host)) // Not Binded
117 free(process->simdata);
120 rb_raise(rb_eRuntimeError,"Host not bound...while creating native process");
123 process->simdata->PID = msg_global->PID++; // msg_global ??
125 DEBUG7("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)",
126 process->name , process->simdata->m_host->name,process->simdata->PID,
127 process,process->simdata, process->simdata->m_host,
128 process->simdata->m_host->simdata);
130 /* FIXME: that's mainly for debugging. We could only allocate this if XBT_LOG_ISENABLED(ruby,debug) is true since I guess this leaks */
131 char **argv=xbt_new(char*,2);
132 argv[0] = bprintf("%s@%s",process->name,process->simdata->m_host->simdata->smx_host->name);
134 process->simdata->s_process =
135 SIMIX_process_create(process->name,
136 (xbt_main_func_t)ruby_process,
138 process->simdata->m_host->simdata->smx_host->name,
141 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
143 if (SIMIX_process_self()) { // SomeOne Created Me !!
144 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
148 process->simdata->PPID = -1;
150 process->simdata->last_errno = MSG_OK;
151 // let's Add the Process to the list of the Simulation's Processes
152 xbt_fifo_unshift(msg_global->process_list,process);
153 sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,
156 mailbox = MSG_mailbox_new(alias);
161 // Process Management
162 void rb_process_suspend(VALUE class,VALUE ruby_process) {
164 m_process_t process = rb_process_to_native(ruby_process);
167 rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
171 // Trying to suspend The Process
173 if ( MSG_OK != MSG_process_suspend(process))
174 rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
177 void rb_process_resume(VALUE class,VALUE ruby_process) {
178 m_process_t process = rb_process_to_native(ruby_process);
180 rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
184 // Trying to resume the process
185 if ( MSG_OK != MSG_process_resume(process))
186 rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
189 VALUE rb_process_isSuspended(VALUE class,VALUE ruby_process) {
190 m_process_t process = rb_process_to_native(ruby_process);
192 rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
196 if(MSG_process_is_suspended(process))
201 void rb_process_kill_down(VALUE class,VALUE ruby_process) {
202 m_process_t process = rb_process_to_native(ruby_process);
205 rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
208 // Delete The Global Reference / Ruby Process
209 rb_process_kill_up(ruby_process);
210 // Delete the Native Process
211 MSG_process_kill(process);
214 VALUE rb_process_getHost(VALUE class,VALUE ruby_process) {
215 m_process_t process = rb_process_to_native(ruby_process);
219 rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
223 host = MSG_process_get_host(process);
226 rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
230 return Data_Wrap_Struct(class, 0, rb_host_free, host);
233 void rb_process_exit(VALUE class,VALUE ruby_process) {
234 m_process_t process = rb_process_to_native(ruby_process);
236 rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
239 SIMIX_context_stop(SIMIX_process_self()->context);