Logo AND Algorithmique Numérique Distribuée

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