Logo AND Algorithmique Numérique Distribuée

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