Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not request status if not requested by caller.
[simgrid.git] / src / bindings / ruby / rb_msg_process.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "msg/private.h"        /* s_simdata_process_t */
8 #include "bindings/ruby_bindings.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ruby, bindings, "Ruby Bindings");
11
12 /*
13  * Functions for Ruby Process Management (Up Calls)
14  */
15
16 // get Ruby Process Name
17 VALUE rb_process_getName(VALUE ruby_process)
18 {
19   return rb_funcall(ruby_process, rb_intern("getName"), 0);
20 }
21
22 // Get  Process ID
23 VALUE rb_process_getID(VALUE ruby_process)
24 {
25   return rb_funcall(ruby_process, rb_intern("getID"), 0);
26 }
27
28 // Get Bind
29 VALUE rb_process_getBind(VALUE ruby_process)
30 {
31   return rb_funcall(ruby_process, rb_intern("getBind"), 0);
32 }
33
34 // Set Bind
35 void rb_process_setBind(VALUE ruby_process, long bind)
36 {
37   VALUE r_bind = LONG2FIX(bind);
38   rb_funcall(ruby_process, rb_intern("setBind"), 1, r_bind);
39 }
40
41 // isAlive
42 VALUE rb_process_isAlive(VALUE ruby_process)
43 {
44   return rb_funcall(ruby_process, rb_intern("alive?"), 0);
45 }
46
47 // Kill Process
48 void rb_process_kill_up(VALUE ruby_process)
49 {
50   rb_funcall(ruby_process, rb_intern("kill"), 0);
51 }
52
53 // join Process
54 void rb_process_join(VALUE ruby_process)
55 {
56   rb_funcall(ruby_process, rb_intern("join"), 0);
57 }
58
59 // FIXME: all this calls must be manually inlined I guess
60 // unschedule Process
61 void rb_process_unschedule(VALUE ruby_process)
62 {
63   rb_funcall(ruby_process, rb_intern("unschedule"), 0);
64 }
65
66 // schedule Process
67 void rb_process_schedule(VALUE ruby_process)
68 {
69   rb_funcall(ruby_process, rb_intern("schedule"), 0);
70 }
71
72 /***************************************************
73 Function for Native Process ( Bound ) Management
74
75 Methods Belong to MSG Module
76 ****************************************************/
77
78 // Process To Native
79 m_process_t rb_process_to_native(VALUE ruby_process)
80 {
81   VALUE id = rb_process_getBind(ruby_process);
82   if (!id) {
83     rb_raise(rb_eRuntimeError, "Process Not Bound >>> id_Bind Null");
84     return NULL;
85   }
86   long l_id = FIX2LONG(id);
87   return (m_process_t) l_id;
88 }
89
90 // Bind Process
91 void rb_process_bind(VALUE ruby_process, m_process_t process)
92 {
93   long bind = (long) (process);
94   rb_process_setBind(ruby_process, bind);
95 }
96
97
98 // Process Management
99 void rb_process_suspend(VALUE class, VALUE ruby_process)
100 {
101
102   m_process_t process = rb_process_to_native(ruby_process);
103
104   if (!process) {
105     rb_raise(rb_eRuntimeError,
106              "Process Not Bound...while suspending process");
107     return;
108   }
109   // Trying to suspend The Process
110
111   if (MSG_OK != MSG_process_suspend(process))
112     rb_raise(rb_eRuntimeError, "MSG_process_suspend() failed");
113 }
114
115 void rb_process_resume(VALUE class, VALUE ruby_process)
116 {
117   m_process_t process = rb_process_to_native(ruby_process);
118   if (!process) {
119     rb_raise(rb_eRuntimeError,
120              "Process not Bound...while resuming process");
121     return;
122   }
123   // Trying to resume the process
124   if (MSG_OK != MSG_process_resume(process))
125     rb_raise(rb_eRuntimeError, "MSG_process_resume() failed");
126 }
127
128 VALUE rb_process_isSuspended(VALUE class, VALUE ruby_process)
129 {
130   m_process_t process = rb_process_to_native(ruby_process);
131   if (!process) {
132     rb_raise(rb_eRuntimeError,
133              "Process not Bound...while testing if suspended");
134     return Qfalse;
135   }
136   if (MSG_process_is_suspended(process))
137     return Qtrue;
138   return Qfalse;
139 }
140
141 void rb_process_kill_down(VALUE class, VALUE ruby_process)
142 {
143   m_process_t process = rb_process_to_native(ruby_process);
144
145   if (!process) {
146     rb_raise(rb_eRuntimeError,
147              "Process Not Bound...while killing process");
148     return;
149   }
150   // Delete The Global Reference / Ruby Process
151   rb_process_kill_up(ruby_process);
152   // Delete the Native Process
153   MSG_process_kill(process);
154 }
155
156 VALUE rb_process_getHost(VALUE class, VALUE ruby_process)
157 {
158   m_process_t process = rb_process_to_native(ruby_process);
159   m_host_t host;
160
161
162   if (!process) {
163     rb_raise(rb_eRuntimeError, "Process Not Bound...while getting Host");
164     return Qnil;                // NULL
165   }
166
167   host = MSG_process_get_host(process);
168
169   return Data_Wrap_Struct(class, 0, rb_host_free, host);
170   /*if(host->data) printf("Ok\n"); 
171
172      if(!host->data) {
173      rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
174      return Qnil;
175      }
176      printf("Houuuuuuuuuuuuuuna3!!\n");
177      return Data_Wrap_Struct(class, 0, rb_host_free, host); */
178 }
179
180 void rb_process_exit(VALUE class, VALUE ruby_process)
181 {
182   m_process_t process = rb_process_to_native(ruby_process);
183   if (!process) {
184     rb_raise(rb_eRuntimeError,
185              "Process Not Bound...while exiting process");
186     return;
187   }
188   SIMIX_context_stop(SIMIX_process_self()->context);
189 }