Logo AND Algorithmique Numérique Distribuée

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