Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Try to fix the finalization process of ruby simulations. THE REST WORKS, yuhu
[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 // Process Management
92 void rb_process_suspend(VALUE class,VALUE ruby_process) {
93
94   m_process_t process = rb_process_to_native(ruby_process);
95
96   if (!process) {
97     rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
98     return;  
99   }
100
101   // Trying to suspend The Process
102
103   if ( MSG_OK != MSG_process_suspend(process))
104     rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
105 }
106
107 void rb_process_resume(VALUE class,VALUE ruby_process) {
108   m_process_t process = rb_process_to_native(ruby_process);
109   if (!process) {
110     rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
111     return ;
112   }
113
114   // Trying to resume the process
115   if ( MSG_OK != MSG_process_resume(process))
116     rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
117 }
118
119 VALUE rb_process_isSuspended(VALUE class,VALUE ruby_process) {
120   m_process_t process = rb_process_to_native(ruby_process);
121   if (!process) {
122     rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
123     return Qfalse;
124   }
125
126   if(MSG_process_is_suspended(process))
127     return Qtrue;
128   return Qfalse;
129 }
130
131 void rb_process_kill_down(VALUE class,VALUE ruby_process) {
132   m_process_t process = rb_process_to_native(ruby_process);
133
134   if(!process) {
135     rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
136     return;
137   }
138   // Delete The Global Reference / Ruby Process
139   rb_process_kill_up(ruby_process);
140   // Delete the Native Process
141   MSG_process_kill(process);
142 }
143
144 VALUE rb_process_getHost(VALUE class,VALUE ruby_process) {
145   m_process_t process = rb_process_to_native(ruby_process);
146   m_host_t host;
147
148   if (!process) {
149     rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
150     return Qnil; // NULL
151   }
152
153   host = MSG_process_get_host(process);
154
155   if(!host->data) {
156     rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
157     return Qnil;
158   }
159
160   return Data_Wrap_Struct(class, 0, rb_host_free, host);
161 }
162
163 void rb_process_exit(VALUE class,VALUE ruby_process) {
164   m_process_t process = rb_process_to_native(ruby_process);
165   if(!process) {
166     rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
167     return;
168   }
169   SIMIX_context_stop(SIMIX_process_self()->context);
170 }