Logo AND Algorithmique Numérique Distribuée

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