Logo AND Algorithmique Numérique Distribuée

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