Logo AND Algorithmique Numérique Distribuée

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