Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Still have to kill runnin' processes (on simix level)and clean ruby world when nothi...
[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 // Set Bind
34 void rb_process_setBind(VALUE ruby_process,long bind) {
35   VALUE r_bind = LONG2FIX(bind);
36   rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind);
37 }
38
39 // isAlive
40 VALUE rb_process_isAlive(VALUE ruby_process) {
41   return rb_funcall(ruby_process,rb_intern("alive?"),0);
42 }
43
44 // Kill Process
45 void rb_process_kill_up(VALUE ruby_process) {
46   rb_funcall(ruby_process,rb_intern("kill"),0);
47 }
48
49 // join Process
50 void rb_process_join( VALUE ruby_process ) {
51   rb_funcall(ruby_process,rb_intern("join"),0);
52 }
53
54 // unschedule Process
55 void rb_process_unschedule( VALUE ruby_process ) {
56   rb_funcall(ruby_process,rb_intern("unschedule"),0);
57 }
58
59 // schedule Process
60 void rb_process_schedule( VALUE ruby_process ) {
61   rb_funcall(ruby_process,rb_intern("schedule"),0);
62 }
63
64
65 /***************************************************
66 Function for Native Process ( Bound ) Management
67
68 Methods Belong to MSG Module
69 ****************************************************/
70
71 // Process To Native
72 m_process_t rb_process_to_native(VALUE ruby_process) {
73   VALUE id = rb_process_getBind(ruby_process);
74   if (!id) {
75     rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
76     return NULL;
77   }
78   long l_id= FIX2LONG(id);
79   return (m_process_t)l_id;
80 }
81
82 // Bind Process
83 void rb_process_bind(VALUE ruby_process,m_process_t process) {
84   long bind = (long)(process);
85   rb_process_setBind(ruby_process,bind);
86 }
87
88
89 // Process Management
90 void rb_process_suspend(VALUE class,VALUE ruby_process) {
91
92   m_process_t process = rb_process_to_native(ruby_process);
93
94   if (!process) {
95     rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
96     return;  
97   }
98
99   // Trying to suspend The Process
100
101   if ( MSG_OK != MSG_process_suspend(process))
102     rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
103 }
104
105 void rb_process_resume(VALUE class,VALUE ruby_process) {
106   m_process_t process = rb_process_to_native(ruby_process);
107   if (!process) {
108     rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
109     return ;
110   }
111
112   // Trying to resume the process
113   if ( MSG_OK != MSG_process_resume(process))
114     rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
115 }
116
117 VALUE rb_process_isSuspended(VALUE class,VALUE ruby_process) {
118   m_process_t process = rb_process_to_native(ruby_process);
119   if (!process) {
120     rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
121     return Qfalse;
122   }
123   if(MSG_process_is_suspended(process))
124     return Qtrue;
125   return Qfalse;
126 }
127
128 void rb_process_kill_down(VALUE class,VALUE ruby_process) {
129   m_process_t process = rb_process_to_native(ruby_process);
130
131   if(!process) {
132     rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
133     return;
134   }
135   // Delete The Global Reference / Ruby Process
136   rb_process_kill_up(ruby_process);
137   // Delete the Native Process
138   MSG_process_kill(process);
139 }
140
141 VALUE rb_process_getHost(VALUE class,VALUE ruby_process) {
142   m_process_t process = rb_process_to_native(ruby_process);
143   m_host_t host;
144
145   if (!process) {
146     rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
147     return Qnil; // NULL
148   }
149
150   host = MSG_process_get_host(process);
151
152   if(!host->data) {
153     rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
154     return Qnil;
155   }
156
157   return Data_Wrap_Struct(class, 0, rb_host_free, host);
158 }
159
160 void rb_process_exit(VALUE class,VALUE ruby_process) {
161   m_process_t process = rb_process_to_native(ruby_process);
162   if(!process) {
163     rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
164     return;
165   }
166   SIMIX_context_stop(SIMIX_process_self()->context);
167 }