Logo AND Algorithmique Numérique Distribuée

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