Logo AND Algorithmique Numérique Distribuée

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