Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
not needed now.
[simgrid.git] / src / cxx / Process.cxx
1 #include <Process.hpp>\r
2 \r
3 namespace msg\r
4 {\r
5         \r
6 Process* Process::currentProcess = NULL;\r
7 \r
8 // Default constructor.\r
9 Process::Process()\r
10 {\r
11         this->nativeProcess = NULL;\r
12 }\r
13 \r
14 Process::Process(const char* hostname, const char* name)\r
15 throw(HostNotFoundException)\r
16 {\r
17         Host host = Host::getByName(hostname);\r
18                 \r
19         create(host, name, 0, NULL);    \r
20 }\r
21 \r
22 Process::Process(const char* hostname, const char* name, int argc, char** argv)\r
23 throw(HostNotFoundException)\r
24 {\r
25         Host host = Host::getByName(hostname);\r
26                 \r
27         create(host, name, argc, argv); \r
28 }\r
29 \r
30 Process::Process(const Host& rHost, const char* name)\r
31 throw(HostNotFoundException)\r
32 {\r
33         \r
34         create(rHost, name, 0, NULL);   \r
35 }\r
36 \r
37 Process::Process(const Host& rHost, const char* name, int argc, char** argv)\r
38 throw(HostNotFoundException)\r
39 {\r
40         \r
41         create(rHost, name, argc, argv);        \r
42 }\r
43 \r
44 int Process::run(int argc, char** argv)\r
45 {\r
46         Process* process =(Process*)argv[argc];\r
47         \r
48         return process->main(argc, argv);\r
49 }\r
50 \r
51 void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
52 throw(HostNotFoundException)\r
53 {\r
54         smx_process_t nativeCurrentProcess = NULL;\r
55         nativeProcess = xbt_new0(s_smx_process_t, 1);\r
56         smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);\r
57         smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());\r
58         \r
59         argv = (char**)realloc(argc + 1, sizeo(char*));\r
60         \r
61         argv[argc] = (char*)this;\r
62         \r
63         \r
64         // TODO throw HostNotFoundException if host is NULL\r
65         \r
66         // Simulator Data\r
67         simdata->smx_host = nativeHost;\r
68         simdata->mutex = NULL;\r
69         simdata->cond = NULL;\r
70         simdata->argc = argc;\r
71         simdata->argv = argv;\r
72         simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);\r
73         \r
74         /* Process structure */\r
75         nativeProcess->name = xbt_strdup(name);\r
76         nativeProcess->simdata = simdata;\r
77         \r
78         // Set process data\r
79         nativeProcess->data = NULL;\r
80         \r
81         // Set process properties\r
82         simdata->properties = NULL;\r
83         \r
84         xbt_swag_insert(nativeProcess, nativeHost->simdata->process_list);\r
85         \r
86         /* fix current_process, about which xbt_context_start mocks around */\r
87         nativeCurrentProcess = simix_global->current_process;\r
88         xbt_context_start(nativeProcess->simdata->context);\r
89         simix_global->current_process = nativeCurrentProcess;\r
90         \r
91         xbt_swag_insert(nativeProcess, simix_global->process_list);\r
92         DEBUG2("Inserting %s(%s) in the to_run list", nativeProcess->name, nativeHost->name);\r
93         xbt_swag_insert(nativeProcess, simix_global->process_to_run);\r
94 }\r
95 \r
96 \r
97 int Process::killAll(int resetPID) \r
98 {\r
99     return MSG_process_killall(resetPID);\r
100 }\r
101 \r
102 void Process::suspend(void)\r
103 throw(NativeException)\r
104 {\r
105     if(MSG_OK != MSG_process_suspend(nativeProcess)) \r
106     {\r
107         // TODO throw NativeException.\r
108     }\r
109 }\r
110 \r
111 void Process::resume(void) \r
112 throw(NativeException)\r
113 {\r
114         if(MSG_OK != MSG_process_resume(nativeProcess))\r
115         {\r
116                 // TODO throw NativeException.\r
117         }\r
118 }\r
119 \r
120 \r
121 bool Process::isSuspended(void)\r
122 {\r
123    return (bool)MSG_process_is_suspended(nativeProcess);\r
124 }  \r
125 \r
126 \r
127 Host& Process::getHost(void) \r
128 throw(NativeException) \r
129 {\r
130   m_host_t nativeHost = MSG_process_get_host(nativeProcess);\r
131         \r
132   if(!nativeHost->data) \r
133   {\r
134     // TODO throw NativeException.\r
135     return NULL;\r
136   }\r
137 \r
138   // return the reference to the Host object\r
139   return (*((Host*)nativeHost->data));\r
140\r
141 \r
142 Process& Process::fromPID(int PID) \r
143 throw(ProcessNotFoundException, NativeException)\r
144 {\r
145         Process* process = NULL;\r
146         m_process_t nativeProcess = MSG_process_from_PID(PID);\r
147         \r
148         \r
149         if(!process) \r
150         {\r
151                 throw ProcessNotFoundException;\r
152                 return NULL;\r
153         }\r
154         \r
155         process = Process::fromNativeProcess(nativeProcess);\r
156                 \r
157         if(!process) \r
158         {\r
159                 //      TODO throw NativeException\r
160                 return NULL;\r
161         }\r
162         \r
163         return (*process);   \r
164 }\r
165 \r
166 // TODO implement this method\r
167 Process& Process::fromNativeProcess(m_process_t nativeProcess)\r
168 {\r
169         \r
170 }\r
171 \r
172 int Process::getPID(void)\r
173 {\r
174     return MSG_process_get_PID(nativeProcess);\r
175 }\r
176 \r
177 int Process::getPPID(void)\r
178 {\r
179         return MSG_process_get_PPID(nativeProcess);\r
180 }\r
181 \r
182 \r
183 Process& Process::currentProcess(void)\r
184 throw(NativeException)\r
185 {\r
186         Process* currentProcess = NULL;\r
187     m_process_t currentNativeProcess = MSG_process_self();\r
188 \r
189 \r
190         if(!currentNativeProcess) \r
191         {\r
192         // TODO throw NativeException\r
193         }\r
194         \r
195         currentProcess = Process::fromNativeProcess(currentNativeProcess);\r
196                 \r
197         if(!currentProcess) \r
198         {\r
199                 //      TODO throw NativeException\r
200                 return NULL;\r
201         }\r
202         \r
203         return (*currentProcess);  \r
204 }\r
205 \r
206 int Process::currentProcessPID(void)\r
207 {\r
208          return MSG_process_self_PID();\r
209 }\r
210 \r
211 \r
212 int Process::currentProcessPPID(void)\r
213 {\r
214         return MSG_process_self_PPID();\r
215 }\r
216 \r
217 void Process::migrate(const Host& rHost)\r
218 throw(NativeException)\r
219 {\r
220         if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost))\r
221         {\r
222                 // TODO throw NativeException\r
223         }\r
224         \r
225 }\r
226 \r
227 void Process::sleep(double seconds)\r
228 throw(NativeException)\r
229 {\r
230         if(MSG_OK != MSG_process_sleep(seconds))\r
231         {\r
232                 // TODO throw NativeException\r
233         }\r
234         \r
235 }\r
236 \r
237 void Process::putTask(const Host& rHost, int channel, const Task& rTask)\r
238 throw( NativeException)\r
239 {\r
240         if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0))\r
241         {\r
242                 // TODO throw NativeException\r
243         }\r
244 }\r
245 \r
246 void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout) \r
247 throw(NativeException)\r
248 {\r
249         if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout))\r
250         {\r
251                 // TODO throw NativeException\r
252         }\r
253 }\r
254 \r
255 Task& Process::getTask(int channel) \r
256 throw(NativeException)\r
257 {\r
258         m_task_t nativeTask = NULL;\r
259         \r
260         \r
261         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL)) \r
262         {\r
263                 // TODO throw NativeException\r
264                 return NULL;\r
265         }\r
266         \r
267         return (*((Task*)(nativeTask->data)));\r
268 }\r
269 \r
270 Task& Process::getTask(int channel, double timeout) \r
271 throw(NativeException)\r
272 {\r
273         m_task_t nativeTask = NULL;\r
274         \r
275         \r
276         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL)) \r
277         {\r
278                 // TODO throw NativeException\r
279                 return NULL;\r
280         }\r
281         \r
282         return (*((Task*)(nativeTask->data)));\r
283 }\r
284 \r
285 Task& Process::getTask(int channel, const Host& rHost) \r
286 throw(NativeException)\r
287 {\r
288         m_task_t nativeTask = NULL;\r
289         \r
290         \r
291         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost)) \r
292         {\r
293                 // TODO throw NativeException\r
294                 return NULL;\r
295         }\r
296         \r
297         return (*((Task*)(nativeTask->data)));\r
298 }\r
299 \r
300 Task& Process::getTask(int channel, double timeout, const Host& rHost)\r
301 throw(NativeException)\r
302 {\r
303         m_task_t nativeTask = NULL;\r
304         \r
305         \r
306         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost)) \r
307         {\r
308                 // TODO throw NativeException\r
309                 return NULL;\r
310         }\r
311         \r
312         return (*((Task*)(nativeTask->data)));\r
313 }\r
314 \r
315 void Process::sendTask(const char* alias, const Task& rTask, double timeout) \r
316 throw(NativeException)\r
317 {\r
318         if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout))\r
319         {\r
320                 // TODO throw NativeException\r
321         }\r
322                 \r
323 }\r
324 \r
325 void Process::sendTask(const char* alias, const Task& rTask) \r
326 throw(NativeException)\r
327 {\r
328         if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0))\r
329         {\r
330                 // TODO throw NativeException\r
331         }\r
332 }\r
333 \r
334 void Process::sendTask(const Task& rTask) \r
335 throw(NativeException)\r
336 {\r
337         MSG_error_t rv;\r
338         \r
339         char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
340         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
341         \r
342         rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0);\r
343         \r
344         free(alias);\r
345         \r
346         if(MSG_OK != rv)\r
347         {\r
348                 // TODO throw NativeException\r
349         }\r
350 }\r
351 \r
352 void Process::sendTask(const Task& rTask, double timeout) \r
353 throw(NativeException)\r
354 {\r
355         MSG_error_t rv;\r
356         \r
357         char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
358         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
359         \r
360         rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout);\r
361         \r
362         free(alias);\r
363         \r
364         if(MSG_OK != rv)\r
365         {\r
366                 // TODO throw NativeException\r
367         }\r
368 }\r
369 \r
370 Task& Process::receiveTask(const char* alias) \r
371 throw(NativeException)\r
372 {\r
373         \r
374         m_task_t nativeTask = NULL;\r
375         \r
376         if (MSG_OK !=  MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL)) \r
377         {\r
378                 // TODO throw NativeException\r
379                 return NULL;\r
380         }\r
381 \r
382         return (*((Task*)nativeTask->data));\r
383 }\r
384 \r
385 \r
386 Task& Process::receiveTask(void) \r
387 throw(NativeException)\r
388 {\r
389         m_task_t nativeTask = NULL;\r
390         \r
391         char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
392         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
393         \r
394         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);\r
395         \r
396         free(alias);\r
397         \r
398         if(MSG_OK !=  rv) \r
399         {\r
400                 //TODO throw NativeException\r
401                 return NULL;\r
402         }\r
403 \r
404         return (*((Task*)nativeTask->data));\r
405 }\r
406 \r
407 \r
408 Task& Process::receiveTask(const char* alias, double timeout) \r
409 throw(NativeException)\r
410 {\r
411         m_task_t nativeTask = NULL;\r
412         \r
413         if(MSG_OK !=  MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) \r
414         {\r
415                 //TODO throw NativeException\r
416                 return NULL;\r
417         }\r
418 \r
419         return (*((Task*)nativeTask->data));\r
420 }\r
421 \r
422 \r
423 Task& Process::receiveTask(double timeout) \r
424 throw(NativeException)\r
425 {\r
426         m_task_t nativeTask = NULL;\r
427         \r
428         char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
429         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
430         \r
431         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, NULL);\r
432         \r
433         free(alias);\r
434         \r
435         if(MSG_OK !=  rv) \r
436         {\r
437                 //TODO throw NativeException\r
438                 return NULL;\r
439         }\r
440 \r
441         return (*((Task*)nativeTask->data));\r
442 }\r
443 \r
444 \r
445 Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost) \r
446 throw(NativeException)\r
447 {\r
448         m_task_t nativeTask = NULL;\r
449         \r
450         if(MSG_OK !=  MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) \r
451         {\r
452                 //TODO throw NativeException\r
453                 return NULL;\r
454         }\r
455 \r
456         return (*((Task*)nativeTask->data));\r
457 }\r
458 \r
459 \r
460 Task& Process::receiveTask(double timeout, const Host& rHost) \r
461 throw(NativeException)\r
462 {\r
463         m_task_t nativeTask = NULL;\r
464         \r
465         char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
466         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
467         \r
468         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost);\r
469         \r
470         free(alias);\r
471         \r
472         if(MSG_OK !=  rv) \r
473         {\r
474                 //TODO throw NativeException\r
475                 return NULL;\r
476         }\r
477 \r
478         return (*((Task*)nativeTask->data));\r
479 }\r
480 \r
481 \r
482 Task& Process::receiveTask(const char* alias, const Host& rHost) \r
483 throw(NativeException)\r
484 {\r
485         \r
486         m_task_t nativeTask = NULL;\r
487         \r
488 \r
489         if(MSG_OK !=   MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) \r
490         {\r
491                 //TODO throw NativeException\r
492                 return NULL;\r
493         }\r
494 \r
495         return (*((Task*)nativeTask->data));\r
496 }\r
497 \r
498 Task& Process::receiveTask(const Host& rHost) \r
499 throw(NativeException)\r
500 {\r
501         m_task_t nativeTask = NULL;\r
502         \r
503         char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
504         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
505         \r
506         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost);\r
507         \r
508         free(alias);\r
509         \r
510         if(MSG_OK !=  rv) \r
511         {\r
512                 //TODO throw NativeException\r
513                 return NULL;\r
514         }\r
515 \r
516         return (*((Task*)nativeTask->data));\r
517 }\r
518 \r
519 const char* Process::getName(void) const\r
520 {\r
521         return nativeProcess->name;\r
522 }\r
523 \r
524 \r
525 }