Logo AND Algorithmique Numérique Distribuée

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