Logo AND Algorithmique Numérique Distribuée

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