Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make it compile when SMPI is disabled
[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 #ifdef HAVE_SMPI
277        if(smpi_privatize_global_variables){
278          if( parent_process->pid != 0){
279            SIMIX_segment_index_set(process, parent_process->segment_index);
280          } else {
281            SIMIX_segment_index_set(process, process->pid - 1);
282          }
283        }
284 #endif
285      } else {
286        process->ppid = -1;
287      }
288
289     /* Process data for auto-restart */
290     process->auto_restart = auto_restart;
291     process->code = code;
292     process->argc = argc;
293     process->argv = argv;
294
295
296     XBT_VERB("Create context %s", process->name);
297     process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
298
299     process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
300     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
301
302     if(MC_is_active()){
303       MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
304     }
305
306     /* Add properties */
307     process->properties = properties;
308
309     /* Add the process to it's host process list */
310     xbt_swag_insert(process, sg_host_simix(host)->process_list);
311
312     XBT_DEBUG("Start context '%s'", process->name);
313
314     /* Now insert it in the global process list and in the process to run list */
315     xbt_swag_insert(process, simix_global->process_list);
316     XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
317     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
318
319     if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
320       XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
321           sg_host_get_name(process->host), kill_time);
322       process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
323     }
324   }
325   return process;
326 }
327
328 /**
329  * \brief Executes the processes from simix_global->process_to_run.
330  *
331  * The processes of simix_global->process_to_run are run (in parallel if
332  * possible).  On exit, simix_global->process_to_run is empty, and
333  * simix_global->process_that_ran contains the list of processes that just ran.
334  * The two lists are swapped so, be careful when using them before and after a
335  * call to this function.
336  */
337 void SIMIX_process_runall(void)
338 {
339   SIMIX_context_runall();
340
341   xbt_dynar_t tmp = simix_global->process_that_ran;
342   simix_global->process_that_ran = simix_global->process_to_run;
343   simix_global->process_to_run = tmp;
344   xbt_dynar_reset(simix_global->process_to_run);
345 }
346
347 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
348   SIMIX_process_kill(process, simcall->issuer);
349 }
350 /**
351  * \brief Internal function to kill a SIMIX process.
352  *
353  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
354  * or directly for SIMIX internal purposes.
355  *
356  * \param process poor victim
357  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
358  */
359 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
360
361   XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
362
363   process->context->iwannadie = 1;
364   process->blocked = 0;
365   process->suspended = 0;
366   process->doexception = 0;
367
368   /* destroy the blocking synchro if any */
369   if (process->waiting_synchro) {
370
371     switch (process->waiting_synchro->type) {
372
373     case SIMIX_SYNC_EXECUTE:
374     case SIMIX_SYNC_PARALLEL_EXECUTE:
375       SIMIX_process_execution_destroy(process->waiting_synchro);
376       break;
377
378     case SIMIX_SYNC_COMMUNICATE:
379       xbt_fifo_remove(process->comms, process->waiting_synchro);
380       SIMIX_comm_cancel(process->waiting_synchro);
381       xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
382       SIMIX_comm_destroy(process->waiting_synchro);
383       break;
384
385     case SIMIX_SYNC_SLEEP:
386       SIMIX_process_sleep_destroy(process->waiting_synchro);
387       break;
388
389     case SIMIX_SYNC_JOIN:
390       SIMIX_process_sleep_destroy(process->waiting_synchro);
391       break;
392
393     case SIMIX_SYNC_SYNCHRO:
394       SIMIX_synchro_stop_waiting(process, &process->simcall);
395       SIMIX_synchro_destroy(process->waiting_synchro);
396       break;
397
398     case SIMIX_SYNC_IO:
399       SIMIX_io_destroy(process->waiting_synchro);
400       break;
401
402     }
403
404     process->waiting_synchro = NULL;
405   }
406   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
407     XBT_DEBUG("Inserting %s in the to_run list", process->name);
408     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
409   }
410
411 }
412
413 /** @brief Ask another process to raise the given exception
414  *
415  * @param cat category of exception
416  * @param value value associated to the exception
417  * @param msg string information associated to the exception
418  */
419 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
420   SMX_EXCEPTION(process, cat, value, msg);
421
422   if (process->suspended)
423     SIMIX_process_resume(process,SIMIX_process_self());
424
425   /* cancel the blocking synchro if any */
426   if (process->waiting_synchro) {
427
428     switch (process->waiting_synchro->type) {
429
430     case SIMIX_SYNC_EXECUTE:
431     case SIMIX_SYNC_PARALLEL_EXECUTE:
432       SIMIX_process_execution_cancel(process->waiting_synchro);
433       break;
434
435     case SIMIX_SYNC_COMMUNICATE:
436       xbt_fifo_remove(process->comms, process->waiting_synchro);
437       SIMIX_comm_cancel(process->waiting_synchro);
438       break;
439
440     case SIMIX_SYNC_SLEEP:
441     case SIMIX_SYNC_JOIN:
442       SIMIX_process_sleep_destroy(process->waiting_synchro);
443       if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
444         XBT_DEBUG("Inserting %s in the to_run list", process->name);
445         xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
446       }
447       break;
448
449     case SIMIX_SYNC_SYNCHRO:
450       SIMIX_synchro_stop_waiting(process, &process->simcall);
451       break;
452
453     case SIMIX_SYNC_IO:
454       SIMIX_io_destroy(process->waiting_synchro);
455       break;
456
457     }
458   }
459   process->waiting_synchro = NULL;
460
461 }
462
463 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
464   SIMIX_process_killall(simcall->issuer, reset_pid);
465 }
466 /**
467  * \brief Kills all running processes.
468  * \param issuer this one will not be killed
469  */
470 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
471 {
472   smx_process_t p = NULL;
473
474   while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
475     if (p != issuer) {
476       SIMIX_process_kill(p,issuer);
477     }
478   }
479
480   if (reset_pid > 0)
481     simix_process_maxpid = reset_pid;
482
483   SIMIX_context_runall();
484
485   SIMIX_process_empty_trash();
486 }
487
488 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
489 {
490   process->new_host = dest;
491 }
492 void SIMIX_process_change_host(smx_process_t process,
493              sg_host_t dest)
494 {
495   xbt_assert((process != NULL), "Invalid parameters");
496   xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
497   process->host = dest;
498   xbt_swag_insert(process, sg_host_simix(dest)->process_list);
499 }
500
501
502 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
503 {
504   smx_synchro_t sync_suspend =
505       SIMIX_process_suspend(process, simcall->issuer);
506
507   if (process != simcall->issuer) {
508     SIMIX_simcall_answer(simcall);
509   } else {
510     xbt_fifo_push(sync_suspend->simcalls, simcall);
511     process->waiting_synchro = sync_suspend;
512     SIMIX_host_execution_suspend(process->waiting_synchro);
513   }
514   /* If we are suspending ourselves, then just do not finish the simcall now */
515 }
516
517 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
518 {
519   xbt_assert((process != NULL), "Invalid parameters");
520
521   if (process->suspended) {
522     XBT_DEBUG("Process '%s' is already suspended", process->name);
523     return NULL;
524   }
525
526   process->suspended = 1;
527
528   /* If we are suspending another process, and it is waiting on a sync,
529      suspend its synchronization. */
530   if (process != issuer) {
531
532     if (process->waiting_synchro) {
533
534       switch (process->waiting_synchro->type) {
535
536         case SIMIX_SYNC_EXECUTE:
537         case SIMIX_SYNC_PARALLEL_EXECUTE:
538           SIMIX_host_execution_suspend(process->waiting_synchro);
539           break;
540
541         case SIMIX_SYNC_COMMUNICATE:
542           SIMIX_comm_suspend(process->waiting_synchro);
543           break;
544
545         case SIMIX_SYNC_SLEEP:
546           SIMIX_process_sleep_suspend(process->waiting_synchro);
547           break;
548
549         case SIMIX_SYNC_SYNCHRO:
550           /* Suspension is delayed to when the process is rescheduled. */
551           break;
552
553         default:
554           xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
555               (int)process->waiting_synchro->type);
556       }
557       return NULL;
558     } else {
559       /* Suspension is delayed to when the process is rescheduled. */
560       return NULL;
561     }
562   } else {
563     /* FIXME: computation size is zero. Is it okay that bound is zero ? */
564     return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
565   }
566 }
567
568 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
569   SIMIX_process_resume(process, simcall->issuer);
570 }
571
572 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
573 {
574   XBT_IN("process = %p, issuer = %p", process, issuer);
575
576   if(process->context->iwannadie) {
577     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
578     return;
579   }
580
581   if(!process->suspended) return;
582   process->suspended = 0;
583
584   /* If we are resuming another process, resume the synchronization it was waiting for
585      if any. Otherwise add it to the list of process to run in the next round. */
586   if (process != issuer) {
587
588     if (process->waiting_synchro) {
589
590       switch (process->waiting_synchro->type) {
591
592         case SIMIX_SYNC_EXECUTE:
593         case SIMIX_SYNC_PARALLEL_EXECUTE:
594           SIMIX_host_execution_resume(process->waiting_synchro);
595           break;
596
597         case SIMIX_SYNC_COMMUNICATE:
598           SIMIX_comm_resume(process->waiting_synchro);
599           break;
600
601         case SIMIX_SYNC_SLEEP:
602           SIMIX_process_sleep_resume(process->waiting_synchro);
603           break;
604
605         case SIMIX_SYNC_SYNCHRO:
606           /* I cannot resume it now. This is delayed to when the process is rescheduled at
607            * the end of the synchro. */
608           break;
609
610         default:
611           xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
612               (int)process->waiting_synchro->type);
613       }
614     }
615   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
616
617   XBT_OUT();
618 }
619
620 int SIMIX_process_get_maxpid(void) {
621   return simix_process_maxpid;
622 }
623
624 int SIMIX_process_count(void)
625 {
626   return xbt_swag_size(simix_global->process_list);
627 }
628
629 int SIMIX_process_get_PID(smx_process_t self){
630   if (self == NULL)
631     return 0;
632   else
633     return self->pid;
634 }
635
636 int SIMIX_process_get_PPID(smx_process_t self){
637   if (self == NULL)
638     return 0;
639   else
640     return self->ppid;
641 }
642
643 void* SIMIX_process_self_get_data(smx_process_t self)
644 {
645   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
646
647   if (!self) {
648     return NULL;
649   }
650   return SIMIX_process_get_data(self);
651 }
652
653 void SIMIX_process_self_set_data(smx_process_t self, void *data)
654 {
655   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
656
657   SIMIX_process_set_data(self, data);
658 }
659
660 void* SIMIX_process_get_data(smx_process_t process)
661 {
662   return process->data;
663 }
664
665 void SIMIX_process_set_data(smx_process_t process, void *data)
666 {
667   process->data = data;
668 }
669
670 sg_host_t SIMIX_process_get_host(smx_process_t process)
671 {
672   return process->host;
673 }
674
675 xbt_main_func_t SIMIX_process_get_code(void){
676   return SIMIX_process_self()->code;
677 }
678
679 /* needs to be public and without simcall because it is called
680    by exceptions and logging events */
681 const char* SIMIX_process_self_get_name(void) {
682
683   smx_process_t process = SIMIX_process_self();
684   if (process == NULL || process == simix_global->maestro_process)
685     return "";
686
687   return SIMIX_process_get_name(process);
688 }
689
690 const char* SIMIX_process_get_name(smx_process_t process)
691 {
692   return process->name;
693 }
694
695 smx_process_t SIMIX_process_get_by_name(const char* name)
696 {
697   smx_process_t proc;
698
699   xbt_swag_foreach(proc, simix_global->process_list)
700   {
701     if(!strcmp(name, proc->name))
702       return proc;
703   }
704   return NULL;
705 }
706
707 int SIMIX_process_is_suspended(smx_process_t process)
708 {
709   return process->suspended;
710 }
711
712 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
713 {
714   return process->properties;
715 }
716
717 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
718 {
719   smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
720   xbt_fifo_push(sync->simcalls, simcall);
721   simcall->issuer->waiting_synchro = sync;
722 }
723
724 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
725   if (sync->sleep.surf_sleep) {
726     surf_action_cancel(sync->sleep.surf_sleep);
727
728     smx_simcall_t simcall;
729     while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
730       simcall_process_sleep__set__result(simcall, SIMIX_DONE);
731       simcall->issuer->waiting_synchro = NULL;
732       if (simcall->issuer->suspended) {
733         XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
734         simcall->issuer->suspended = 0;
735         simcall_HANDLER_process_suspend(simcall, simcall->issuer);
736       } else {
737         SIMIX_simcall_answer(simcall);
738       }
739     }
740     surf_action_unref(sync->sleep.surf_sleep);
741     sync->sleep.surf_sleep = NULL;
742   }
743   xbt_mallocator_release(simix_global->synchro_mallocator, sync);
744   return 0;
745 }
746
747 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
748 {
749   smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
750   res->type = SIMIX_SYNC_JOIN;
751   SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
752   return res;
753 }
754
755 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
756 {
757   if (MC_is_active() || MC_record_replay_is_active()) {
758     MC_process_clock_add(simcall->issuer, duration);
759     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
760     SIMIX_simcall_answer(simcall);
761     return;
762   }
763   smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
764   xbt_fifo_push(sync->simcalls, simcall);
765   simcall->issuer->waiting_synchro = sync;
766 }
767
768 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
769 {
770   sg_host_t host = process->host;
771
772   /* check if the host is active */
773   if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
774     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
775            sg_host_get_name(host));
776   }
777
778   smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
779   synchro->type = SIMIX_SYNC_SLEEP;
780   synchro->name = NULL;
781   synchro->category = NULL;
782
783   synchro->sleep.host = host;
784   synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
785
786   surf_action_set_data(synchro->sleep.surf_sleep, synchro);
787   XBT_DEBUG("Create sleep synchronization %p", synchro);
788
789   return synchro;
790 }
791
792 void SIMIX_post_process_sleep(smx_synchro_t synchro)
793 {
794   smx_simcall_t simcall;
795   e_smx_state_t state;
796   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
797
798   while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
799
800     switch(surf_action_get_state(synchro->sleep.surf_sleep)){
801       case SURF_ACTION_FAILED:
802         simcall->issuer->context->iwannadie = 1;
803         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
804         state = SIMIX_SRC_HOST_FAILURE;
805         break;
806
807       case SURF_ACTION_DONE:
808         state = SIMIX_DONE;
809         break;
810
811       default:
812         THROW_IMPOSSIBLE;
813         break;
814     }
815     if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
816       simcall->issuer->context->iwannadie = 1;
817     }
818     simcall_process_sleep__set__result(simcall, state);
819     simcall->issuer->waiting_synchro = NULL;
820     if (simcall->issuer->suspended) {
821       XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
822       simcall->issuer->suspended = 0;
823       simcall_HANDLER_process_suspend(simcall, simcall->issuer);
824     } else {
825       SIMIX_simcall_answer(simcall);
826     }
827   }
828
829   SIMIX_process_sleep_destroy(synchro);
830 }
831
832 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
833 {
834   XBT_DEBUG("Destroy synchro %p", synchro);
835   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
836
837   if (synchro->sleep.surf_sleep) {
838     surf_action_unref(synchro->sleep.surf_sleep);
839     synchro->sleep.surf_sleep = NULL;
840   }
841   if (synchro->type == SIMIX_SYNC_SLEEP)
842     xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
843 }
844
845 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
846 {
847   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
848   surf_action_suspend(synchro->sleep.surf_sleep);
849 }
850
851 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
852 {
853   XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
854   xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
855   surf_action_resume(synchro->sleep.surf_sleep);
856 }
857
858 /**
859  * \brief Calling this function makes the process to yield.
860  *
861  * Only the current process can call this function, giving back the control to
862  * maestro.
863  *
864  * \param self the current process
865  */
866 void SIMIX_process_yield(smx_process_t self)
867 {
868   XBT_DEBUG("Yield process '%s'", self->name);
869
870   /* Go into sleep and return control to maestro */
871   SIMIX_context_suspend(self->context);
872
873   /* Ok, maestro returned control to us */
874   XBT_DEBUG("Control returned to me: '%s'", self->name);
875
876   if (self->new_host) {
877     SIMIX_process_change_host(self, self->new_host);
878     self->new_host = NULL;
879   }
880
881   if (self->context->iwannadie){
882     XBT_DEBUG("I wanna die!");
883     SIMIX_process_stop(self);
884   }
885
886   if (self->suspended) {
887     XBT_DEBUG("Hey! I'm suspended.");
888     xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
889     self->suspended = 0;
890     SIMIX_process_suspend(self, self);
891   }
892
893   if (self->doexception) {
894     XBT_DEBUG("Wait, maestro left me an exception");
895     self->doexception = 0;
896     SMX_THROW();
897   }
898
899   if(SMPI_switch_data_segment && self->segment_index != -1){
900     SMPI_switch_data_segment(self->segment_index);
901   }
902 }
903
904 /* callback: context fetching */
905 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
906 {
907   return SIMIX_process_self()->running_ctx;
908 }
909
910 /* callback: termination */
911 void SIMIX_process_exception_terminate(xbt_ex_t * e)
912 {
913   xbt_ex_display(e);
914   xbt_abort();
915 }
916
917 smx_context_t SIMIX_process_get_context(smx_process_t p) {
918   return p->context;
919 }
920
921 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
922   p->context = c;
923 }
924
925 /**
926  * \brief Returns the list of processes to run.
927  */
928 xbt_dynar_t SIMIX_process_get_runnable(void)
929 {
930   return simix_global->process_to_run;
931 }
932
933 /**
934  * \brief Returns the process from PID.
935  */
936 smx_process_t SIMIX_process_from_PID(int PID)
937 {
938   smx_process_t proc;
939   xbt_swag_foreach(proc, simix_global->process_list) {
940    if (proc->pid == (unsigned long) PID)
941     return proc;
942   }
943   return NULL;
944 }
945
946 /** @brief returns a dynar containg all currently existing processes */
947 xbt_dynar_t SIMIX_processes_as_dynar(void) {
948   smx_process_t proc;
949   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
950   xbt_swag_foreach(proc, simix_global->process_list) {
951     xbt_dynar_push(res,&proc);
952   }
953   return res;
954 }
955
956
957 void SIMIX_process_on_exit_runall(smx_process_t process) {
958   s_smx_process_exit_fun_t exit_fun;
959   smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
960                                          SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
961   while (!xbt_dynar_is_empty(process->on_exit)) {
962     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
963     (exit_fun.fun)((void*)exit_status, exit_fun.arg);
964   }
965 }
966
967 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
968   xbt_assert(process, "current process not found: are you in maestro context ?");
969
970   if (!process->on_exit) {
971     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
972   }
973
974   s_smx_process_exit_fun_t exit_fun = {fun, data};
975
976   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
977 }
978
979 /**
980  * \brief Sets the auto-restart status of the process.
981  * If set to 1, the process will be automatically restarted when its host
982  * comes back.
983  */
984 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
985   process->auto_restart = auto_restart;
986 }
987
988 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
989   return SIMIX_process_restart(process, simcall->issuer);
990 }
991 /** @brief Restart a process, starting it again from the beginning. */
992 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
993   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
994   //retrieve the arguments of the old process
995   //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
996   s_smx_process_arg_t arg;
997   arg.code = process->code;
998   arg.hostname = sg_host_get_name(process->host);
999   arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1000   arg.argc = process->argc;
1001   arg.data = process->data;
1002   int i;
1003   arg.argv = xbt_new(char*,process->argc + 1);
1004   for (i = 0; i < arg.argc; i++) {
1005     arg.argv[i] = xbt_strdup(process->argv[i]);
1006   }
1007   arg.argv[process->argc] = NULL;
1008   arg.properties = NULL;
1009   arg.auto_restart = process->auto_restart;
1010   //kill the old process
1011   SIMIX_process_kill(process,issuer);
1012   //start the new process
1013   smx_process_t new_process;
1014   if (simix_global->create_process_function) {
1015     new_process = simix_global->create_process_function(
1016                                           arg.argv[0],
1017                                           arg.code,
1018                                           arg.data,
1019                                           arg.hostname,
1020                                           arg.kill_time,
1021                                           arg.argc,
1022                                           arg.argv,
1023                                           arg.properties,
1024                                           arg.auto_restart,
1025                                           NULL);
1026   } else {
1027     new_process = simcall_process_create(
1028                            arg.argv[0],
1029                            arg.code,
1030                            arg.data,
1031                            arg.hostname,
1032                            arg.kill_time,
1033                            arg.argc,
1034                            arg.argv,
1035                            arg.properties,
1036                            arg.auto_restart);
1037
1038   }
1039   return new_process;
1040 }
1041
1042 void SIMIX_segment_index_set(smx_process_t proc, int index){
1043   proc->segment_index = index;
1044 }