Logo AND Algorithmique Numérique Distribuée

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