Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / simix / smx_process.c
1 /* Copyright (c) 2007-2014. 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 "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14                                 "Logging specific to SIMIX (process)");
15
16 unsigned long simix_process_maxpid = 0;
17
18 /**
19  * \brief Returns the current agent.
20  *
21  * This functions returns the currently running SIMIX process.
22  *
23  * \return The SIMIX process
24  */
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
26 {
27   smx_context_t self_context = SIMIX_context_self();
28
29   return self_context ? SIMIX_context_get_process(self_context) : NULL;
30 }
31
32 /**
33  * \brief Returns whether a process has pending asynchronous communications.
34  * \return true if there are asynchronous communications in this process
35  */
36 int SIMIX_process_has_pending_comms(smx_process_t process) {
37
38   return xbt_fifo_size(process->comms) > 0;
39 }
40
41 void SIMIX_pre_process_cleanup(smx_simcall_t simcall, smx_process_t process) {
42   SIMIX_process_cleanup(process);
43 }
44 /**
45  * \brief Moves a process to the list of processes to destroy.
46  */
47 void SIMIX_process_cleanup(smx_process_t process)
48 {
49   XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
50       process->name, process, process->waiting_action);
51
52   SIMIX_process_on_exit_runall(process);
53
54   /* cancel non-blocking communications */
55   smx_action_t action;
56   while ((action = xbt_fifo_pop(process->comms))) {
57
58     /* make sure no one will finish the comm after this process is destroyed,
59      * because src_proc or dst_proc would be an invalid pointer */
60     SIMIX_comm_cancel(action);
61
62     if (action->comm.src_proc == process) {
63       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
64           action, action->comm.detached, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
65       action->comm.src_proc = NULL;
66
67       if (action->comm.detached) {
68          if (action->comm.refcount == 0) {
69            XBT_DEBUG("Increase the refcount before destroying it since it's detached");
70            /* I'm not supposed to destroy a detached comm from the sender side,
71             * unless there is no receiver matching the rdv */
72            action->comm.refcount++;
73            SIMIX_comm_destroy(action);
74          }
75          else {
76            XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
77          }
78       } else {
79         SIMIX_comm_destroy(action);
80       }
81     }
82     else if (action->comm.dst_proc == process){
83       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
84           action, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
85       action->comm.dst_proc = NULL;
86
87       if (action->comm.detached && action->comm.refcount == 1
88           && action->comm.src_proc != NULL) {
89         /* the comm will be freed right now, remove it from the sender */
90         xbt_fifo_remove(action->comm.src_proc->comms, action);
91       }
92       SIMIX_comm_destroy(action);
93     }
94     else {
95       xbt_die("Communication action %p is in my list but I'm not the sender "
96           "or the receiver", action);
97     }
98   }
99
100   xbt_swag_remove(process, simix_global->process_list);
101   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
102   xbt_swag_insert(process, simix_global->process_to_destroy);
103   process->context->iwannadie = 0;
104 }
105
106 /**
107  * Garbage collection
108  *
109  * Should be called some time to time to free the memory allocated for processes
110  * that have finished (or killed).
111  */
112 void SIMIX_process_empty_trash(void)
113 {
114   smx_process_t process = NULL;
115
116   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
117     SIMIX_context_free(process->context);
118
119     /* Free the exception allocated at creation time */
120     free(process->running_ctx);
121     xbt_dict_free(&process->properties);
122
123     xbt_fifo_free(process->comms);
124
125     xbt_dynar_free(&process->on_exit);
126
127     xbt_free(process->name);
128     xbt_free(process);
129   }
130 }
131
132 /**
133  * \brief Creates and runs the maestro process
134  */
135 void SIMIX_create_maestro_process()
136 {
137   smx_process_t maestro = NULL;
138
139   /* Create maestro process and intilialize it */
140   maestro = xbt_new0(s_smx_process_t, 1);
141   maestro->pid = simix_process_maxpid++;
142   maestro->ppid = -1;
143   maestro->name = (char *) "";
144   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
145   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
146   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
147   maestro->simcall.issuer = maestro;
148   simix_global->maestro_process = maestro;
149   return;
150 }
151 /**
152  * \brief Stops a process.
153  *
154  * Stops the process, execute all the registered on_exit functions,
155  * register it to the list of the process to restart if needed
156  * and stops its context.
157  */
158 void SIMIX_process_stop(smx_process_t arg) {
159   /* execute the on_exit functions */
160   SIMIX_process_on_exit_runall(arg);
161   /* Add the process to the list of process to restart, only if
162    * the host is down
163    */
164   if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
165     SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
166                                         sg_host_name(arg->smx_host),
167                                         arg->kill_time,
168                                         arg->argc,arg->argv,arg->properties,
169                                         arg->auto_restart);
170   }
171   XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
172   /* stop the context */
173   SIMIX_context_stop(arg->context);
174 }
175
176 /**
177  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
178  * This function frees the argument.
179  * \return the process created
180  */
181 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
182
183   smx_process_t process;
184   simix_global->create_process_function(&process,
185                                         args->name,
186                                         args->code,
187                                         args->data,
188                                         args->hostname,
189                                         args->kill_time,
190                                         args->argc,
191                                         args->argv,
192                                         args->properties,
193                                         args->auto_restart,
194                                         NULL);
195   xbt_free(args);
196   return process;
197 }
198
199
200 void SIMIX_pre_process_create(smx_simcall_t simcall,
201                           smx_process_t *process,
202                           const char *name,
203                           xbt_main_func_t code,
204                           void *data,
205                           const char *hostname,
206                           double kill_time,
207                           int argc, char **argv,
208                           xbt_dict_t properties,
209                           int auto_restart){
210   SIMIX_process_create(process, name, code, data, hostname,
211                        kill_time, argc, argv, properties, auto_restart,
212                        simcall->issuer);
213 }
214 /**
215  * \brief Internal function to create a process.
216  *
217  * This function actually creates the process.
218  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
219  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
220  *
221  * \return the process created
222  */
223 void SIMIX_process_create(smx_process_t *process,
224                           const char *name,
225                           xbt_main_func_t code,
226                           void *data,
227                           const char *hostname,
228                           double kill_time,
229                           int argc, char **argv,
230                           xbt_dict_t properties,
231                           int auto_restart,
232                           smx_process_t parent_process)
233 {
234   *process = NULL;
235   smx_host_t host = SIMIX_host_get_by_name(hostname);
236
237   XBT_DEBUG("Start process %s on host '%s'", name, hostname);
238
239   if (!SIMIX_host_get_state(host)) {
240     int i;
241     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
242           hostname);
243     for (i = 0; i < argc; i++)
244       xbt_free(argv[i]);
245     xbt_free(argv);
246   }
247   else {
248     *process = xbt_new0(s_smx_process_t, 1);
249
250     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
251     /* Process data */
252     (*process)->pid = simix_process_maxpid++;
253     (*process)->name = xbt_strdup(name);
254     (*process)->smx_host = host;
255     (*process)->data = data;
256     (*process)->comms = xbt_fifo_new();
257     (*process)->simcall.issuer = *process;
258
259      if (parent_process) {
260        (*process)->ppid = SIMIX_process_get_PID(parent_process);
261      } else {
262        (*process)->ppid = -1;
263      }
264
265     /* Process data for auto-restart */
266     (*process)->auto_restart = auto_restart;
267     (*process)->code = code;
268     (*process)->argc = argc;
269     (*process)->argv = argv;
270     (*process)->kill_time = kill_time;
271
272
273     XBT_VERB("Create context %s", (*process)->name);
274     (*process)->context = SIMIX_context_new(code, argc, argv,
275       simix_global->cleanup_process_function, *process);
276
277     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
278     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
279
280     if(MC_is_active()){
281       MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
282     }
283
284     /* Add properties */
285     (*process)->properties = properties;
286
287     /* Add the process to it's host process list */
288     xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
289
290     XBT_DEBUG("Start context '%s'", (*process)->name);
291
292     /* Now insert it in the global process list and in the process to run list */
293     xbt_swag_insert(*process, simix_global->process_list);
294     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
295     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
296   }
297
298   if (kill_time > SIMIX_get_clock()) {
299     if (simix_global->kill_process_function) {
300       XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
301           sg_host_name((*process)->smx_host), kill_time);
302       SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
303     }
304   }
305 }
306
307 /**
308  * \brief Executes the processes from simix_global->process_to_run.
309  *
310  * The processes of simix_global->process_to_run are run (in parallel if
311  * possible).  On exit, simix_global->process_to_run is empty, and
312  * simix_global->process_that_ran contains the list of processes that just ran.
313  * The two lists are swapped so, be careful when using them before and after a
314  * call to this function.
315  */
316 void SIMIX_process_runall(void)
317 {
318   SIMIX_context_runall();
319
320   xbt_dynar_t tmp = simix_global->process_that_ran;
321   simix_global->process_that_ran = simix_global->process_to_run;
322   simix_global->process_to_run = tmp;
323   xbt_dynar_reset(simix_global->process_to_run);
324 }
325
326 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
327   SIMIX_process_kill(process, simcall->issuer);
328 }
329 /**
330  * \brief Internal function to kill a SIMIX process.
331  *
332  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
333  * or directly for SIMIX internal purposes.
334  *
335  * \param process poor victim
336  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
337  */
338 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
339
340   XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
341
342   process->context->iwannadie = 1;
343   process->blocked = 0;
344   process->suspended = 0;
345   /* FIXME: set doexception to 0 also? */
346
347   /* destroy the blocking action if any */
348   if (process->waiting_action) {
349
350     switch (process->waiting_action->type) {
351
352     case SIMIX_ACTION_EXECUTE:
353     case SIMIX_ACTION_PARALLEL_EXECUTE:
354       SIMIX_host_execution_destroy(process->waiting_action);
355       break;
356
357     case SIMIX_ACTION_COMMUNICATE:
358       xbt_fifo_remove(process->comms, process->waiting_action);
359       SIMIX_comm_cancel(process->waiting_action);
360       SIMIX_comm_destroy(process->waiting_action);
361       break;
362
363     case SIMIX_ACTION_SLEEP:
364       SIMIX_process_sleep_destroy(process->waiting_action);
365       break;
366
367     case SIMIX_ACTION_JOIN:
368       SIMIX_process_sleep_destroy(process->waiting_action);
369       break;
370
371     case SIMIX_ACTION_SYNCHRO:
372       SIMIX_synchro_stop_waiting(process, &process->simcall);
373       SIMIX_synchro_destroy(process->waiting_action);
374       break;
375
376     case SIMIX_ACTION_IO:
377       SIMIX_io_destroy(process->waiting_action);
378       break;
379
380       /* **************************************/
381       /* TUTORIAL: New API                    */
382     case SIMIX_ACTION_NEW_API:
383       SIMIX_new_api_destroy(process->waiting_action);
384       break;
385       /* **************************************/
386
387     }
388   }
389   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
390     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
391   }
392
393 }
394
395 void SIMIX_pre_process_killall(smx_simcall_t simcall, int reset_pid) {
396   SIMIX_process_killall(simcall->issuer, reset_pid);
397 }
398 /**
399  * \brief Kills all running processes.
400  * \param issuer this one will not be killed
401  */
402 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
403 {
404   smx_process_t p = NULL;
405
406   while ((p = xbt_swag_extract(simix_global->process_list))) {
407     if (p != issuer) {
408       SIMIX_process_kill(p,issuer);
409     }
410   }
411
412   if (reset_pid > 0)
413     simix_process_maxpid = reset_pid;
414
415   SIMIX_context_runall();
416
417   SIMIX_process_empty_trash();
418 }
419
420 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
421                                    smx_host_t dest)
422 {
423   process->new_host = dest;
424 }
425 void SIMIX_process_change_host(smx_process_t process,
426              smx_host_t dest)
427 {
428   xbt_assert((process != NULL), "Invalid parameters");
429   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
430   process->smx_host = dest;
431   xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
432 }
433
434
435 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
436 {
437   smx_action_t action_suspend =
438       SIMIX_process_suspend(process, simcall->issuer);
439
440   if (process != simcall->issuer) {
441     SIMIX_simcall_answer(simcall);
442   } else {
443     xbt_fifo_push(action_suspend->simcalls, simcall);
444     process->waiting_action = action_suspend;
445     SIMIX_host_execution_suspend(process->waiting_action);
446   }
447   /* If we are suspending ourselves, then just do not finish the simcall now */
448 }
449
450 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
451 {
452   xbt_assert((process != NULL), "Invalid parameters");
453
454   if (process->suspended) {
455     XBT_DEBUG("Process '%s' is already suspended", process->name);
456     return NULL;
457   }
458
459   process->suspended = 1;
460
461   /* If we are suspending another process, and it is waiting on an action,
462      suspend its action. */
463   if (process != issuer) {
464
465     if (process->waiting_action) {
466
467       switch (process->waiting_action->type) {
468
469         case SIMIX_ACTION_EXECUTE:
470         case SIMIX_ACTION_PARALLEL_EXECUTE:
471           SIMIX_host_execution_suspend(process->waiting_action);
472           break;
473
474         case SIMIX_ACTION_COMMUNICATE:
475           SIMIX_comm_suspend(process->waiting_action);
476           break;
477
478         case SIMIX_ACTION_SLEEP:
479           SIMIX_process_sleep_suspend(process->waiting_action);
480           break;
481
482         case SIMIX_ACTION_SYNCHRO:
483           /* Suspension is delayed to when the process is rescheduled. */
484           break;
485
486         default:
487           xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
488               (int)process->waiting_action->type);
489       }
490       return NULL;
491     } else {
492       /* Suspension is delayed to when the process is rescheduled. */
493       return NULL;
494     }
495   } else {
496     /* FIXME: computation size is zero. Is it okay that bound is zero ? */
497     return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
498   }
499 }
500
501 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
502   SIMIX_process_resume(process, simcall->issuer);
503 }
504
505 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
506 {
507   xbt_assert((process != NULL), "Invalid parameters");
508
509   XBT_IN("process = %p, issuer = %p", process, issuer);
510
511   if(process->context->iwannadie) {
512     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
513     return;
514   }
515
516   if(!process->suspended) return;
517   process->suspended = 0;
518
519   /* If we are resuming another process, resume the action it was waiting for
520      if any. Otherwise add it to the list of process to run in the next round. */
521   if (process != issuer) {
522
523     if (process->waiting_action) {
524
525       switch (process->waiting_action->type) {
526
527         case SIMIX_ACTION_EXECUTE:
528         case SIMIX_ACTION_PARALLEL_EXECUTE:
529           SIMIX_host_execution_resume(process->waiting_action);
530           break;
531
532         case SIMIX_ACTION_COMMUNICATE:
533           SIMIX_comm_resume(process->waiting_action);
534           break;
535
536         case SIMIX_ACTION_SLEEP:
537           SIMIX_process_sleep_resume(process->waiting_action);
538           break;
539
540         case SIMIX_ACTION_SYNCHRO:
541           /* I cannot resume it now. This is delayed to when the process is rescheduled at
542            * the end of the synchro. */
543           break;
544
545         default:
546           xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
547               (int)process->waiting_action->type);
548       }
549     }
550   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
551
552   XBT_OUT();
553 }
554
555 int SIMIX_process_get_maxpid(void) {
556   return simix_process_maxpid;
557 }
558
559 int SIMIX_pre_process_count(smx_simcall_t simcall){
560   return SIMIX_process_count();
561 }
562 int SIMIX_process_count(void)
563 {
564   return xbt_swag_size(simix_global->process_list);
565 }
566
567 int SIMIX_pre_process_get_PID(smx_simcall_t simcall, smx_process_t self){
568    return SIMIX_process_get_PID(self);
569 }
570
571 int SIMIX_process_get_PID(smx_process_t self){
572   if (self == NULL)
573     return 0;
574   else
575     return self->pid;
576 }
577
578 int SIMIX_pre_process_get_PPID(smx_simcall_t simcall, smx_process_t self){
579   return SIMIX_process_get_PPID(self);
580 }
581
582 int SIMIX_process_get_PPID(smx_process_t self){
583   if (self == NULL)
584     return 0;
585   else
586     return self->ppid;
587 }
588
589 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
590   return SIMIX_process_self_get_data(self);
591 }
592
593 void* SIMIX_process_self_get_data(smx_process_t self)
594 {
595   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
596
597   if (!self) {
598     return NULL;
599   }
600   return SIMIX_process_get_data(self);
601 }
602
603 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
604                                 void *data){
605   SIMIX_process_set_data(process, data);
606 }
607 void SIMIX_process_self_set_data(smx_process_t self, void *data)
608 {
609   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
610
611   SIMIX_process_set_data(self, data);
612 }
613
614 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
615   return SIMIX_process_get_data(process);
616 }
617 void* SIMIX_process_get_data(smx_process_t process)
618 {
619   return process->data;
620 }
621
622 void SIMIX_process_set_data(smx_process_t process, void *data)
623 {
624   process->data = data;
625 }
626
627 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
628   return SIMIX_process_get_host(process);
629 }
630 smx_host_t SIMIX_process_get_host(smx_process_t process)
631 {
632   return process->smx_host;
633 }
634
635 /* needs to be public and without simcall because it is called
636    by exceptions and logging events */
637 const char* SIMIX_process_self_get_name(void) {
638
639   smx_process_t process = SIMIX_process_self();
640   if (process == NULL || process == simix_global->maestro_process)
641     return "";
642
643   return SIMIX_process_get_name(process);
644 }
645
646 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
647   return SIMIX_process_get_name(process);
648 }
649 const char* SIMIX_process_get_name(smx_process_t process)
650 {
651   return process->name;
652 }
653
654 smx_process_t SIMIX_process_get_by_name(const char* name)
655 {
656   smx_process_t proc;
657
658   xbt_swag_foreach(proc, simix_global->process_list)
659   {
660     if(!strcmp(name, proc->name))
661       return proc;
662   }
663   return NULL;
664 }
665
666 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
667   return SIMIX_process_is_suspended(process);
668 }
669 int SIMIX_process_is_suspended(smx_process_t process)
670 {
671   return process->suspended;
672 }
673
674 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
675   return SIMIX_process_get_properties(process);
676 }
677 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
678 {
679   return process->properties;
680 }
681
682 void SIMIX_pre_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
683 {
684   smx_action_t action = SIMIX_process_join(simcall->issuer, process, timeout);
685   xbt_fifo_push(action->simcalls, simcall);
686   simcall->issuer->waiting_action = action;
687 }
688
689 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_action_t action){
690   if (action->sleep.surf_sleep) {
691     surf_action_cancel(action->sleep.surf_sleep);
692
693     smx_simcall_t simcall;
694     while ((simcall = xbt_fifo_shift(action->simcalls))) {
695       simcall_process_sleep__set__result(simcall, SIMIX_DONE);
696       simcall->issuer->waiting_action = NULL;
697       if (simcall->issuer->suspended) {
698         XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
699         simcall->issuer->suspended = 0;
700         SIMIX_pre_process_suspend(simcall, simcall->issuer);
701       } else {
702         SIMIX_simcall_answer(simcall);
703       }
704     }
705     surf_action_unref(action->sleep.surf_sleep);
706     action->sleep.surf_sleep = NULL;
707   }
708   xbt_mallocator_release(simix_global->action_mallocator, action);
709   return 0;
710 }
711
712 smx_action_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
713 {
714   smx_action_t res = SIMIX_process_sleep(issuer, timeout);
715   res->type = SIMIX_ACTION_JOIN;
716   SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
717   return res;
718 }
719
720 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
721 {
722   if (MC_is_active()) {
723     MC_process_clock_add(simcall->issuer, duration);
724     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
725     SIMIX_simcall_answer(simcall);
726     return;
727   }
728   smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
729   xbt_fifo_push(action->simcalls, simcall);
730   simcall->issuer->waiting_action = action;
731 }
732
733 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
734 {
735   smx_action_t action;
736   smx_host_t host = process->smx_host;
737
738   /* check if the host is active */
739   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
740     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
741            sg_host_name(host));
742   }
743
744   action = xbt_mallocator_get(simix_global->action_mallocator);
745   action->type = SIMIX_ACTION_SLEEP;
746   action->name = NULL;
747 #ifdef HAVE_TRACING
748   action->category = NULL;
749 #endif
750
751   action->sleep.host = host;
752   action->sleep.surf_sleep =
753       surf_workstation_sleep(host, duration);
754
755   surf_action_set_data(action->sleep.surf_sleep, action);
756   XBT_DEBUG("Create sleep action %p", action);
757
758   return action;
759 }
760
761 void SIMIX_post_process_sleep(smx_action_t action)
762 {
763   smx_simcall_t simcall;
764   e_smx_state_t state;
765   xbt_assert(action->type == SIMIX_ACTION_SLEEP || action->type == SIMIX_ACTION_JOIN);
766
767   while ((simcall = xbt_fifo_shift(action->simcalls))) {
768
769     switch(surf_action_get_state(action->sleep.surf_sleep)){
770       case SURF_ACTION_FAILED:
771         simcall->issuer->context->iwannadie = 1;
772         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
773         state = SIMIX_SRC_HOST_FAILURE;
774         break;
775
776       case SURF_ACTION_DONE:
777         state = SIMIX_DONE;
778         break;
779
780       default:
781         THROW_IMPOSSIBLE;
782         break;
783     }
784     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
785       simcall->issuer->context->iwannadie = 1;
786     }
787     simcall_process_sleep__set__result(simcall, state);
788     simcall->issuer->waiting_action = NULL;
789     if (simcall->issuer->suspended) {
790       XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
791       simcall->issuer->suspended = 0;
792       SIMIX_pre_process_suspend(simcall, simcall->issuer);
793     } else {
794       SIMIX_simcall_answer(simcall);
795     }
796   }
797
798   SIMIX_process_sleep_destroy(action);
799 }
800
801 void SIMIX_process_sleep_destroy(smx_action_t action)
802 {
803   XBT_DEBUG("Destroy action %p", action);
804   xbt_assert(action->type == SIMIX_ACTION_SLEEP || action->type == SIMIX_ACTION_JOIN);
805
806   if (action->sleep.surf_sleep) {
807     surf_action_unref(action->sleep.surf_sleep);
808     action->sleep.surf_sleep = NULL;
809   }
810   if (action->type == SIMIX_ACTION_SLEEP)
811     xbt_mallocator_release(simix_global->action_mallocator, action);
812 }
813
814 void SIMIX_process_sleep_suspend(smx_action_t action)
815 {
816   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
817   surf_action_suspend(action->sleep.surf_sleep);
818 }
819
820 void SIMIX_process_sleep_resume(smx_action_t action)
821 {
822   XBT_DEBUG("Action state is %d on process_sleep_resume.", action->state);
823   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
824   surf_action_resume(action->sleep.surf_sleep);
825 }
826
827 /**
828  * \brief Calling this function makes the process to yield.
829  *
830  * Only the current process can call this function, giving back the control to
831  * maestro.
832  *
833  * \param self the current process
834  */
835 void SIMIX_process_yield(smx_process_t self)
836 {
837   XBT_DEBUG("Yield process '%s'", self->name);
838
839   /* Go into sleep and return control to maestro */
840   SIMIX_context_suspend(self->context);
841
842   /* Ok, maestro returned control to us */
843   XBT_DEBUG("Control returned to me: '%s'", self->name);
844
845   if (self->new_host) {
846     SIMIX_process_change_host(self, self->new_host);
847     self->new_host = NULL;
848   }
849
850   if (self->context->iwannadie){
851     XBT_DEBUG("I wanna die!");
852     SIMIX_process_stop(self);
853   }
854
855   if (self->suspended) {
856     XBT_DEBUG("Hey! I'm suspended.");
857     xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
858     self->suspended = 0;
859     SIMIX_process_suspend(self, self);
860   }
861
862   if (self->doexception) {
863     XBT_DEBUG("Wait, maestro left me an exception");
864     self->doexception = 0;
865     SMX_THROW();
866   }
867
868 }
869
870 /* callback: context fetching */
871 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
872 {
873   return SIMIX_process_self()->running_ctx;
874 }
875
876 /* callback: termination */
877 void SIMIX_process_exception_terminate(xbt_ex_t * e)
878 {
879   xbt_ex_display(e);
880   xbt_abort();
881 }
882
883 smx_context_t SIMIX_process_get_context(smx_process_t p) {
884   return p->context;
885 }
886
887 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
888   p->context = c;
889 }
890
891 /**
892  * \brief Returns the list of processes to run.
893  */
894 xbt_dynar_t SIMIX_process_get_runnable(void)
895 {
896   return simix_global->process_to_run;
897 }
898
899 /**
900  * \brief Returns the process from PID.
901  */
902 smx_process_t SIMIX_process_from_PID(int PID)
903 {
904   smx_process_t proc;
905   xbt_swag_foreach(proc, simix_global->process_list)
906   {
907    if(proc->pid == PID)
908    return proc;
909   }
910   return NULL;
911 }
912
913 /** @brief returns a dynar containg all currently existing processes */
914 xbt_dynar_t SIMIX_processes_as_dynar(void) {
915   smx_process_t proc;
916   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
917   xbt_swag_foreach(proc, simix_global->process_list) {
918     xbt_dynar_push(res,&proc);
919   }
920   return res;
921 }
922
923
924 void SIMIX_process_on_exit_runall(smx_process_t process) {
925   s_smx_process_exit_fun_t exit_fun;
926   smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
927                                          SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
928   while (!xbt_dynar_is_empty(process->on_exit)) {
929     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
930     (exit_fun.fun)((void*)exit_status, exit_fun.arg);
931   }
932 }
933
934 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
935                                int_f_pvoid_pvoid_t fun, void *data) {
936   SIMIX_process_on_exit(process, fun, data);
937 }
938
939 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
940   xbt_assert(process, "current process not found: are you in maestro context ?");
941
942   if (!process->on_exit) {
943     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
944   }
945
946   s_smx_process_exit_fun_t exit_fun = {fun, data};
947
948   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
949 }
950
951 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
952                                         int auto_restart) {
953   SIMIX_process_auto_restart_set(process, auto_restart);
954 }
955 /**
956  * \brief Sets the auto-restart status of the process.
957  * If set to 1, the process will be automatically restarted when its host
958  * comes back.
959  */
960 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
961   process->auto_restart = auto_restart;
962 }
963
964 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
965   return SIMIX_process_restart(process, simcall->issuer);
966 }
967 /**
968  * \brief Restart a process.
969  * Restart a process, starting it again from the beginning.
970  */
971 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
972   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
973   //retrieve the arguments of the old process
974   //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
975   s_smx_process_arg_t arg;
976   arg.code = process->code;
977   arg.hostname = sg_host_name(process->smx_host);
978   arg.kill_time = process->kill_time;
979   arg.argc = process->argc;
980   arg.data = process->data;
981   int i;
982   arg.argv = xbt_new(char*,process->argc + 1);
983   for (i = 0; i < arg.argc; i++) {
984     arg.argv[i] = xbt_strdup(process->argv[i]);
985   }
986   arg.argv[process->argc] = NULL;
987   arg.properties = NULL;
988   arg.auto_restart = process->auto_restart;
989   //kill the old process
990   SIMIX_process_kill(process,issuer);
991   //start the new process
992   smx_process_t new_process;
993   if (simix_global->create_process_function) {
994     simix_global->create_process_function(&new_process,
995                                           arg.argv[0],
996                                           arg.code,
997                                           arg.data,
998                                           arg.hostname,
999                                           arg.kill_time,
1000                                           arg.argc,
1001                                           arg.argv,
1002                                           arg.properties,
1003                                           arg.auto_restart,
1004                                           NULL);
1005   } else {
1006     simcall_process_create(&new_process,
1007                            arg.argv[0],
1008                            arg.code,
1009                            arg.data,
1010                            arg.hostname,
1011                            arg.kill_time,
1012                            arg.argc,
1013                            arg.argv,
1014                            arg.properties,
1015                            arg.auto_restart);
1016
1017   }
1018   return new_process;
1019 }