Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some new classes of CPP version of Msg
[simgrid.git] / src / cxx / Process.cxx
1 #include <Process.hpp>\r
2 \r
3 #include <MsgException.hpp>\r
4 #include <NullPointerException.hpp>\r
5 #include <HostNotFoundException.hpp>\r
6 #include <ProcessNotFoundException.hpp>\r
7 #include <InvalidArgumentException.hpp>\r
8 #include <BadAllocException.hpp>\r
9 #include <LogicException.hpp>\r
10 \r
11 \r
12 #include <ApplicationHandler.hpp>\r
13 #include <Host.hpp>\r
14 #include <Task.hpp>\r
15 \r
16 #include <stdlib.h>\r
17 #include <stdio.h>\r
18 \r
19 #include <msg/msg.h>\r
20 #include <msg/private.h>\r
21 #include <msg/mailbox.h>\r
22 \r
23 \r
24 namespace SimGrid\r
25 {\r
26         namespace Msg\r
27         {\r
28 \r
29                 MSG_IMPLEMENT_DYNAMIC(Process, Object);\r
30 \r
31                 // Default constructor.\r
32                 Process::Process()\r
33                 {\r
34                         this->nativeProcess = NULL;\r
35                 }\r
36                 \r
37                 Process::Process(const char* hostName, const char* name)\r
38                 throw(InvalidArgumentException, HostNotFoundException)\r
39                 {\r
40                         // check the parameters\r
41                         \r
42                         if(!name)\r
43                                 throw NullPointerException("name");\r
44                                 \r
45                         if(!hostName)\r
46                                 throw NullPointerException("hostName");\r
47                         \r
48                         Host host = Host::getByName(hostName);\r
49                                 \r
50                         create(host, name, 0, NULL);    \r
51                 }\r
52                 \r
53                 Process::Process(const Host& rHost, const char* name)\r
54                 throw(NullPointerException)\r
55                 {\r
56                         if(!name)\r
57                                 throw NullPointerException("name");\r
58                                 \r
59                         create(rHost, name, 0, NULL);   \r
60                 }\r
61                 \r
62                 Process::Process(const Host& rHost, const char* name, int argc, char** argv)\r
63                 throw(NullPointerException, InvalidArgumentException, LogicException)\r
64                 {\r
65                         \r
66                         // check the parameters\r
67                         \r
68                         if(!name)\r
69                                 throw NullPointerException("name");\r
70                                 \r
71                         if(argc < 0)\r
72                                 throw InvalidArgumentException("argc (must be positive)");\r
73                                 \r
74                         if(!argc && argv)\r
75                                 throw LogicException("argv is not NULL but argc is zero");\r
76                         \r
77                         if(argc && !argv)\r
78                                 throw LogicException("argv is NULL but argc is not zero");\r
79                         \r
80                         create(rHost, name, argc, argv);        \r
81                 }\r
82                 \r
83                 Process::Process(const char* hostName, const char* name, int argc, char** argv)\r
84                 throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException)\r
85                 {\r
86                         // check the parameters\r
87                         \r
88                         if(!name)\r
89                                 throw NullPointerException("name");\r
90                                 \r
91                         if(!hostName)\r
92                                 throw NullPointerException("hostName");\r
93                                 \r
94                         if(argc < 0)\r
95                                 throw InvalidArgumentException("argc (must be positive)");\r
96                                 \r
97                         if(!argc && argv)\r
98                                 throw LogicException("argv is not NULL but argc is zero");\r
99                         \r
100                         if(argc && !argv)\r
101                                 throw LogicException("argv is NULL but argc is not zero");\r
102                                 \r
103                         Host host = Host::getByName(hostName);\r
104                                 \r
105                         create(host, name, argc, argv); \r
106                 }\r
107                 \r
108                 int Process::killAll(int resetPID) \r
109                 {\r
110                     return MSG_process_killall(resetPID);\r
111                 }\r
112                 \r
113                 void Process::suspend(void)\r
114                 throw(MsgException)\r
115                 {\r
116                     if(MSG_OK != MSG_process_suspend(nativeProcess)) \r
117                         throw MsgException("MSG_process_suspend() failed");\r
118                 }\r
119                 \r
120                 void Process::resume(void) \r
121                 throw(MsgException)\r
122                 {\r
123                         if(MSG_OK != MSG_process_resume(nativeProcess))\r
124                                 throw MsgException("MSG_process_resume() failed");\r
125                 }\r
126                 \r
127                 int Process::isSuspended(void)\r
128                 {\r
129                    return MSG_process_is_suspended(nativeProcess);\r
130                 }  \r
131                 \r
132                 Host& Process::getHost(void) \r
133                 {\r
134                   m_host_t nativeHost = MSG_process_get_host(nativeProcess);\r
135                         \r
136                   // return the reference to the Host object\r
137                   return (*((Host*)nativeHost->data));\r
138                 }\r
139                 \r
140                 Process& Process::fromPID(int PID) \r
141                 throw(ProcessNotFoundException, InvalidArgumentException, MsgException)\r
142                 {\r
143                         // check the parameters\r
144                         \r
145                         if(PID < 1)\r
146                                 throw InvalidArgumentException("PID (the PID of the process to retrieve is not less than 1)");\r
147                                 \r
148                         Process* process = NULL;\r
149                         m_process_t nativeProcess = MSG_process_from_PID(PID);\r
150                         \r
151                         if(!nativeProcess) \r
152                                 throw ProcessNotFoundException(PID);\r
153                         \r
154                         process = Process::fromNativeProcess(nativeProcess);\r
155                                 \r
156                         if(!process) \r
157                                 throw MsgException("Process::fromNativeProcess() failed");\r
158                         \r
159                         return (*process);   \r
160                 } \r
161                 \r
162                 int Process::getPID(void)\r
163                 {\r
164                     return MSG_process_get_PID(nativeProcess);\r
165                 }\r
166                 \r
167                 int Process::getPPID(void)\r
168                 {\r
169                         return MSG_process_get_PPID(nativeProcess);\r
170                 }\r
171                 \r
172                 const char* Process::getName(void) const\r
173                 {\r
174                         return nativeProcess->name;\r
175                 }\r
176                 \r
177                 Process& Process::currentProcess(void)\r
178                 throw(MsgException)\r
179                 {\r
180                         Process* currentProcess = NULL;\r
181                     m_process_t currentNativeProcess = MSG_process_self();\r
182                 \r
183                 \r
184                         if(!currentNativeProcess) \r
185                                 throw MsgException("MSG_process_self() failed");\r
186                         \r
187                         currentProcess = Process::fromNativeProcess(currentNativeProcess);\r
188                                 \r
189                         if(!currentProcess) \r
190                                 throw MsgException("Process::fromNativeProcess() failed");\r
191                         \r
192                         return (*currentProcess);  \r
193                 }\r
194                 \r
195                 int Process::currentProcessPID(void)\r
196                 {\r
197                          return MSG_process_self_PID();\r
198                 }\r
199                 \r
200                 \r
201                 int Process::currentProcessPPID(void)\r
202                 {\r
203                         return MSG_process_self_PPID();\r
204                 }\r
205                 \r
206                 void Process::migrate(const Host& rHost)\r
207                 throw(MsgException)\r
208                 {\r
209                         if(MSG_OK != MSG_process_change_host(rHost.nativeHost))\r
210                                 throw MsgException("MSG_process_change_host()");\r
211                         \r
212                 }\r
213                 \r
214                 void Process::sleep(double seconds)\r
215                 throw(InvalidArgumentException, MsgException)\r
216                 {\r
217                         // check the parameters.\r
218                         if(seconds <= 0)\r
219                                 throw InvalidArgumentException("seconds (must not be less or equals to zero");\r
220                                 \r
221                         if(MSG_OK != MSG_process_sleep(seconds))\r
222                                 throw MsgException("MSG_process_sleep()");\r
223                         \r
224                 }\r
225                 \r
226                 void Process::putTask(const Host& rHost, int channel, const Task& rTask)\r
227                 throw(InvalidArgumentException, MsgException)\r
228                 {\r
229                         // check the parameters\r
230                         \r
231                         if(channel < 0)\r
232                                 throw InvalidArgumentException("channel (must not be negative)");\r
233                                 \r
234                         if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0))\r
235                                 throw MsgException("MSG_task_put_with_timeout()");\r
236                 }\r
237                 \r
238                 void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout) \r
239                 throw(InvalidArgumentException, MsgException)\r
240                 {\r
241                         // check the parameters\r
242                         if(channel < 0)\r
243                                 throw InvalidArgumentException("channel (must not be negative)");\r
244                                 \r
245                         if(timeout < 0 && timeout != -1.0)\r
246                                 throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");\r
247                                 \r
248                         if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout))\r
249                                 throw MsgException("MSG_task_put_with_timeout() failed");\r
250                 }\r
251                 \r
252                 Task& Process::getTask(int channel) \r
253                 throw(InvalidArgumentException, MsgException)\r
254                 {\r
255                         // check the parameters\r
256                         \r
257                         if(channel < 0)\r
258                                 throw InvalidArgumentException("channel (must not be negative)");\r
259                         \r
260                         m_task_t nativeTask = NULL;\r
261                         \r
262                         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL)) \r
263                                 throw MsgException("MSG_task_get_ext() failed");\r
264                         \r
265                         return (*((Task*)(nativeTask->data)));\r
266                 }\r
267                 \r
268                 Task& Process::getTask(int channel, double timeout) \r
269                 throw(InvalidArgumentException, MsgException)\r
270                 {\r
271                         // check the parameters\r
272                         if(channel < 0)\r
273                                 throw InvalidArgumentException("channel (must not be negative)");\r
274                                 \r
275                         if(timeout < 0 && timeout != -1.0)\r
276                                 throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");\r
277                         \r
278                         m_task_t nativeTask = NULL;\r
279                         \r
280                         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL)) \r
281                                 throw MsgException("MSG_task_get_ext() failed");\r
282                         \r
283                         return (*((Task*)(nativeTask->data)));\r
284                 }\r
285                 \r
286                 Task& Process::getTask(int channel, const Host& rHost) \r
287                 throw(InvalidArgumentException, MsgException)\r
288                 {\r
289                         // check the parameters\r
290                         if(channel < 0)\r
291                                 throw InvalidArgumentException("channel (must not be negative)");\r
292                                 \r
293                         m_task_t nativeTask = NULL;\r
294                         \r
295                         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost)) \r
296                                 throw MsgException("MSG_task_get_ext() failed");\r
297                         \r
298                         return (*((Task*)(nativeTask->data)));\r
299                 }\r
300                 \r
301                 Task& Process::getTask(int channel, double timeout, const Host& rHost)\r
302                 throw(InvalidArgumentException, MsgException)\r
303                 {\r
304                         // check the parameters\r
305                         if(channel < 0)\r
306                                 throw InvalidArgumentException("channel (must not be negative)");\r
307                                 \r
308                         if(timeout < 0 && timeout != -1.0)\r
309                                 throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");\r
310                         \r
311                         m_task_t nativeTask = NULL;     \r
312                         \r
313                         if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost)) \r
314                                 throw MsgException("MSG_task_get_ext() failed");\r
315                         \r
316                         return (*((Task*)(nativeTask->data)));\r
317                 }\r
318                 \r
319                 void Process::sendTask(const char* alias, const Task& rTask, double timeout) \r
320                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
321                 {\r
322                         // check the parameters\r
323                         \r
324                         if(!alias)\r
325                                 throw NullPointerException("alias");\r
326                         \r
327                         if(timeout < 0 && timeout !=-1.0)\r
328                                 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
329                         \r
330                         if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout))\r
331                                 throw MsgException("MSG_task_send_with_timeout()");\r
332                                 \r
333                 }\r
334                 \r
335                 void Process::sendTask(const char* alias, const Task& rTask) \r
336                 throw(NullPointerException, MsgException)\r
337                 {\r
338                         // check the parameters\r
339                         \r
340                         if(!alias)\r
341                                 throw NullPointerException("alias");\r
342                                 \r
343                         if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0))\r
344                                 throw MsgException("MSG_task_send_with_timeout()");\r
345                 }\r
346                 \r
347                 void Process::sendTask(const Task& rTask) \r
348                 throw(BadAllocException, MsgException)\r
349                 {\r
350                         char* alias = (char*)calloc( strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
351                                 \r
352                         if(!alias)\r
353                                 throw BadAllocException("alias");\r
354                                 \r
355                         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
356                         \r
357                         MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0);\r
358                         \r
359                         free(alias);\r
360                         \r
361                         if(MSG_OK != rv)\r
362                                 throw MsgException("MSG_task_send_with_timeout()");\r
363                 }\r
364                 \r
365                 void Process::sendTask(const Task& rTask, double timeout) \r
366                 throw(BadAllocException, InvalidArgumentException, MsgException)\r
367                 {\r
368                         // check the parameters\r
369                         \r
370                         if(timeout < 0 && timeout !=-1.0)\r
371                                 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
372                         \r
373                         char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2 , sizeof(char));\r
374                                 \r
375                         if(!alias)\r
376                                 throw BadAllocException("alias");\r
377                                 \r
378                         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
379                         \r
380                         MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout);\r
381                         \r
382                         free(alias);\r
383                         \r
384                         if(MSG_OK != rv)\r
385                                 throw MsgException("MSG_task_send_with_timeout()");     \r
386                 }\r
387                 \r
388                 Task& Process::receiveTask(const char* alias) \r
389                 throw(NullPointerException, MsgException)\r
390                 {\r
391                         // check the parameters\r
392                         \r
393                         if(!alias)\r
394                                 throw NullPointerException(alias);\r
395                                 \r
396                         m_task_t nativeTask = NULL;\r
397                         \r
398                         if (MSG_OK !=  MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL)) \r
399                                 throw MsgException("MSG_task_receive_ext() failed");\r
400                 \r
401                         return (*((Task*)nativeTask->data));\r
402                 }\r
403                 \r
404                 \r
405                 Task& Process::receiveTask(void) \r
406                 throw(BadAllocException, MsgException)\r
407                 {\r
408                         \r
409                         char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
410                         \r
411                         if(!alias)\r
412                                 throw BadAllocException("alias");       \r
413                         \r
414                         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
415                         \r
416                         m_task_t nativeTask = NULL;\r
417                         \r
418                         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);\r
419                         \r
420                         free(alias);\r
421                         \r
422                         if(MSG_OK !=  rv) \r
423                                 throw MsgException("MSG_task_receive_ext() failed");    \r
424                 \r
425                         return (*((Task*)nativeTask->data));\r
426                 }\r
427                 \r
428                 \r
429                 Task& Process::receiveTask(const char* alias, double timeout) \r
430                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
431                 {\r
432                         // check the parameters\r
433                         \r
434                         if(!alias)\r
435                                 throw NullPointerException("alias");\r
436                                 \r
437                         if(timeout < 0 && timeout !=-1.0)\r
438                                 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
439                                 \r
440                         m_task_t nativeTask = NULL;\r
441                         \r
442                         if(MSG_OK !=  MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) \r
443                                 throw MsgException("MSG_task_receive_ext() failed");            \r
444                 \r
445                         return (*((Task*)nativeTask->data));\r
446                 }\r
447                 \r
448                 \r
449                 Task& Process::receiveTask(double timeout) \r
450                 throw(InvalidArgumentException, BadAllocException, MsgException)\r
451                 {\r
452                         // check the parameters\r
453                         \r
454                         if(timeout < 0 && timeout !=-1.0)\r
455                                 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
456                                 \r
457                         \r
458                         char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2,  sizeof(char));\r
459                         \r
460                         if(!alias)\r
461                                 throw BadAllocException("alias");\r
462                         \r
463                         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
464                         \r
465                         m_task_t nativeTask = NULL;\r
466                         \r
467                         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, NULL);\r
468                         \r
469                         free(alias);\r
470                         \r
471                         if(MSG_OK !=  rv) \r
472                                 throw MsgException("MSG_task_receive_ext() failed");    \r
473                 \r
474                         return (*((Task*)nativeTask->data));\r
475                 }\r
476                 \r
477                 \r
478                 Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost) \r
479                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
480                 {\r
481                         // check the parameters\r
482                         \r
483                         if(!alias)\r
484                                 throw NullPointerException("alias");\r
485                         \r
486                         if(timeout < 0 && timeout !=-1.0)\r
487                                 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
488                                 \r
489                         m_task_t nativeTask = NULL;\r
490                         \r
491                         if(MSG_OK !=  MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) \r
492                                 throw MsgException("MSG_task_receive_ext() failed");\r
493                 \r
494                         return (*((Task*)nativeTask->data));\r
495                 }\r
496                 \r
497                 \r
498                 Task& Process::receiveTask(double timeout, const Host& rHost) \r
499                 throw(BadAllocException, InvalidArgumentException, MsgException)\r
500                 {\r
501                         // check the parameters\r
502                         \r
503                         if(timeout < 0 && timeout !=-1.0)\r
504                                 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");\r
505                         \r
506                         char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
507                         \r
508                         if(!alias)\r
509                                 throw BadAllocException("alias");\r
510                                 \r
511                         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
512                         \r
513                         m_task_t nativeTask = NULL;\r
514                         \r
515                         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost);\r
516                         \r
517                         free(alias);\r
518                         \r
519                         if(MSG_OK !=  rv) \r
520                                 throw MsgException("MSG_task_receive_ext() failed");\r
521                 \r
522                         return (*((Task*)nativeTask->data));\r
523                 }\r
524                 \r
525                 \r
526                 Task& Process::receiveTask(const char* alias, const Host& rHost) \r
527                 throw(NullPointerException, MsgException)\r
528                 {\r
529                         \r
530                         // check the parameters\r
531                         \r
532                         if(!alias)\r
533                                 throw NullPointerException("alias");\r
534                         \r
535                         m_task_t nativeTask = NULL;\r
536                         \r
537                         if(MSG_OK !=   MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) \r
538                                 throw MsgException("MSG_task_receive_ext() failed");\r
539                 \r
540                         return (*((Task*)nativeTask->data));\r
541                 }\r
542                 \r
543                 Task& Process::receiveTask(const Host& rHost) \r
544                 throw(BadAllocException, MsgException)\r
545                 {\r
546                         char* alias = (char*)calloc(strlen(Host::currentHost().getName()) + strlen(nativeProcess->name) + 2, sizeof(char));\r
547                         \r
548                         if(!alias)\r
549                                 throw BadAllocException("alias");\r
550                         \r
551                         sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
552                         \r
553                         m_task_t nativeTask = NULL;\r
554                         \r
555                         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost);\r
556                         \r
557                         free(alias);\r
558                         \r
559                         if(MSG_OK !=  rv) \r
560                                 throw MsgException("MSG_task_receive_ext() failed");\r
561                 \r
562                         return (*((Task*)nativeTask->data));\r
563                 }\r
564                 \r
565                 /*void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
566                 throw(HostNotFoundException)\r
567                 {\r
568                         smx_process_t nativeCurrentProcess = NULL;\r
569                         \r
570                         // allocate the native process\r
571                         this->nativeProcess = xbt_new0(s_smx_process_t, 1);\r
572                         \r
573                         // allocate the simulation data of the native process\r
574                         smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);\r
575                         \r
576                         // try to retrieve the host where to createt the process from its name\r
577                         smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());\r
578                         \r
579                         if(!nativeHost)\r
580                                 throw HostNotFoundException(rHost.getName());\r
581                         \r
582                         // realloc the list of the argument to add the pointer to this process instance at the end\r
583                         argv = (char**)realloc(argc + 1, sizeof(char*));\r
584                         \r
585                         // add the pointer to this instance at the end of the list of the arguments of the process\r
586                         // so the static method Process::run() (passed as argument of the MSG function xbt_context_new())\r
587                         // can retrieve the concerned process object by the run\r
588                         // so Process::run() can call the method main() of the good process\r
589                         // for more detail see Process::run() method\r
590                         argv[argc] = (char*)this;\r
591                         \r
592                         // Simulator Data\r
593                         simdata->smx_host = nativeHost;\r
594                         simdata->mutex = NULL;\r
595                         simdata->cond = NULL;\r
596                         simdata->argc = argc;\r
597                         simdata->argv = argv;\r
598                         \r
599                         // create the context of the process.\r
600                         simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);\r
601                         \r
602                         // Process structure \r
603                         this->nativeProcess->name = xbt_strdup(name);\r
604                         this->nativeProcess->simdata = simdata;\r
605                         \r
606                         // Set process data\r
607                         this->nativeProcess->data = NULL;\r
608                         \r
609                         // Set process properties\r
610                         simdata->properties = NULL;\r
611                         \r
612                         xbt_swag_insert(this->nativeProcess, nativeHost->simdata->process_list);\r
613                         \r
614                         // fix current_process, about which xbt_context_start mocks around \r
615                         nativeCurrentProcess = simix_global->current_process;\r
616                         xbt_context_start(this->nativeProcess->simdata->context);\r
617                         simix_global->current_process = nativeCurrentProcess;\r
618                         \r
619                         xbt_swag_insert(this->nativeProcess, simix_global->process_list);\r
620                         DEBUG2("Inserting %s(%s) in the to_run list", this->nativeProcess->name, nativeHost->name);\r
621                         xbt_swag_insert(this->nativeProcess, simix_global->process_to_run);\r
622                 }*/\r
623 \r
624                 void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
625                 throw(InvalidArgumentException)\r
626                 {\r
627                         char alias[MAX_ALIAS_NAME + 1] = {0};\r
628                         msg_mailbox_t mailbox;\r
629                         \r
630                         \r
631                         // try to retrieve the host where to createt the process from its name\r
632                         m_host_t nativeHost = rHost.nativeHost;\r
633                         \r
634                         if(!nativeHost)\r
635                                 throw InvalidArgumentException("rHost"); \r
636                         \r
637                         /* allocate the data of the simulation */\r
638                         this->nativeProcess = xbt_new0(s_m_process_t,1);\r
639                         this->nativeProcess->simdata = xbt_new0(s_simdata_process_t,1);\r
640                         this->nativeProcess->name = _strdup(name);\r
641                         this->nativeProcess->simdata->m_host = nativeHost;\r
642                         this->nativeProcess->simdata->PID = msg_global->PID++;\r
643                         \r
644                         // realloc the list of the argument to add the pointer to this process instance at the end\r
645                         if(argc)\r
646                                 argv = (char**)realloc(argv , (argc + 1) * sizeof(char*));\r
647                         else\r
648                                 argv = (char**)calloc(1 ,sizeof(char*));\r
649                         \r
650                         // add the pointer to this instance at the end of the list of the arguments of the process\r
651                         // so the static method Process::run() (passed as argument of the MSG function xbt_context_new())\r
652                         // can retrieve the concerned process object by the run\r
653                         // so Process::run() can call the method main() of the good process\r
654                         // for more detail see Process::run() method\r
655                         argv[argc] = (char*)this;\r
656 \r
657                         this->nativeProcess->simdata->argc = argc;\r
658                         this->nativeProcess->simdata->argv = argv;\r
659                         \r
660                         this->nativeProcess->simdata->s_process = SIMIX_process_create(\r
661                                                                         this->nativeProcess->name,\r
662                                                                         Process::run, \r
663                                                                         (void*)this->nativeProcess,\r
664                                                                         nativeHost->name, \r
665                                                                         argc,\r
666                                                                         argv, \r
667                                                                         NULL);\r
668                         \r
669                         if (SIMIX_process_self()) \r
670                         {/* someone created me */\r
671                                 this->nativeProcess->simdata->PPID = MSG_process_get_PID((m_process_t)SIMIX_process_self()->data);\r
672                         } \r
673                         else \r
674                         {\r
675                                 this->nativeProcess->simdata->PPID = -1;\r
676                         }\r
677                         \r
678                         this->nativeProcess->simdata->last_errno = MSG_OK;\r
679                         \r
680                         /* add the process to the list of the processes of the simulation */\r
681                         xbt_fifo_unshift(msg_global->process_list, this->nativeProcess);\r
682                         \r
683                         sprintf(alias,"%s:%s",(this->nativeProcess->simdata->m_host->simdata->smx_host)->name,this->nativeProcess->name);\r
684                         \r
685                         mailbox = MSG_mailbox_new(alias);\r
686                         \r
687                         MSG_mailbox_set_hostname(mailbox, this->nativeProcess->simdata->m_host->simdata->smx_host->name);\r
688 \r
689                         \r
690                 }\r
691                 \r
692                 Process* Process::fromNativeProcess(m_process_t nativeProcess)\r
693                 {\r
694                         return ((Process*)(nativeProcess->simdata->argv[nativeProcess->simdata->argc]));\r
695                 }\r
696                 \r
697                 int Process::run(int argc, char** argv)\r
698                 {\r
699                         \r
700                         // the last argument of the process is the pointer to the process to run\r
701                         // for mor detail see Process::create() method\r
702                         return ((Process*)argv[argc])->main(argc, argv);\r
703                 }\r
704 \r
705                 int Process::main(int argc, char** argv)\r
706                 {\r
707                         throw LogicException("Process::main() not implemented");\r
708                 }\r
709                 \r
710         } // namespace Msg\r
711 \r
712 } // namespace SimGrid