Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Chasing more leaks.
[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_workstation_model->extension.
699       workstation.get_state(host) != SURF_RESOURCE_ON) {
700     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
701            sg_host_name(host));
702   }
703
704   action = xbt_mallocator_get(simix_global->action_mallocator);
705   action->type = SIMIX_ACTION_SLEEP;
706   action->name = NULL;
707 #ifdef HAVE_TRACING
708   action->category = NULL;
709 #endif
710
711   action->sleep.host = host;
712   action->sleep.surf_sleep =
713       surf_workstation_model->extension.workstation.sleep(host, duration);
714
715   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
716   XBT_DEBUG("Create sleep action %p", action);
717
718   return action;
719 }
720
721 void SIMIX_post_process_sleep(smx_action_t action)
722 {
723   smx_simcall_t simcall;
724   e_smx_state_t state;
725
726   while ((simcall = xbt_fifo_shift(action->simcalls))) {
727
728     switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
729       case SURF_ACTION_FAILED:
730         simcall->issuer->context->iwannadie = 1;
731         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
732         state = SIMIX_SRC_HOST_FAILURE;
733         break;
734
735       case SURF_ACTION_DONE:
736         state = SIMIX_DONE;
737         break;
738
739       default:
740         THROW_IMPOSSIBLE;
741         break;
742     }
743     if (surf_workstation_model->extension.
744         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
745       simcall->issuer->context->iwannadie = 1;
746     }
747     simcall_process_sleep__set__result(simcall, state);
748     simcall->issuer->waiting_action = NULL;
749     SIMIX_simcall_answer(simcall);
750
751   }
752   SIMIX_process_sleep_destroy(action);
753 }
754
755 void SIMIX_process_sleep_destroy(smx_action_t action)
756 {
757   XBT_DEBUG("Destroy action %p", action);
758   if (action->sleep.surf_sleep)
759     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
760   xbt_mallocator_release(simix_global->action_mallocator, action);
761 }
762
763 void SIMIX_process_sleep_suspend(smx_action_t action)
764 {
765   surf_workstation_model->suspend(action->sleep.surf_sleep);
766 }
767
768 void SIMIX_process_sleep_resume(smx_action_t action)
769 {
770   surf_workstation_model->resume(action->sleep.surf_sleep);
771 }
772
773 /** 
774  * \brief Calling this function makes the process to yield.
775  *
776  * Only the current process can call this function, giving back the control to
777  * maestro.
778  *
779  * \param self the current process
780  */
781 void SIMIX_process_yield(smx_process_t self)
782 {
783   XBT_DEBUG("Yield process '%s'", self->name);
784
785   /* Go into sleep and return control to maestro */
786   SIMIX_context_suspend(self->context);
787
788   /* Ok, maestro returned control to us */
789   XBT_DEBUG("Control returned to me: '%s'", self->name);
790
791   if (self->new_host) {
792     SIMIX_process_change_host(self, self->new_host);
793     self->new_host = NULL;
794   }
795
796   if (self->context->iwannadie){
797     XBT_DEBUG("I wanna die!");
798     SIMIX_process_stop(self);
799   }
800
801   if(self->suspended) {
802     xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
803     self->suspended = 0;
804     SIMIX_process_suspend(self,self);
805   }
806
807   if (self->doexception) {
808     XBT_DEBUG("Wait, maestro left me an exception");
809     self->doexception = 0;
810     SMX_THROW();
811   }
812
813 }
814
815 /* callback: context fetching */
816 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
817 {
818   return SIMIX_process_self()->running_ctx;
819 }
820
821 /* callback: termination */
822 void SIMIX_process_exception_terminate(xbt_ex_t * e)
823 {
824   xbt_ex_display(e);
825   xbt_abort();
826 }
827
828 smx_context_t SIMIX_process_get_context(smx_process_t p) {
829   return p->context;
830 }
831
832 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
833   p->context = c;
834 }
835
836 /**
837  * \brief Returns the list of processes to run.
838  */
839 xbt_dynar_t SIMIX_process_get_runnable(void)
840 {
841   return simix_global->process_to_run;
842 }
843
844 /**
845  * \brief Returns the process from PID.
846  */
847 smx_process_t SIMIX_process_from_PID(int PID)
848 {
849   smx_process_t proc;
850   xbt_swag_foreach(proc, simix_global->process_list)
851   {
852    if(proc->pid == PID)
853    return proc;
854   }
855   return NULL;
856 }
857
858 /** @brief returns a dynar containg all currently existing processes */
859 xbt_dynar_t SIMIX_processes_as_dynar(void) {
860   smx_process_t proc;
861   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
862   xbt_swag_foreach(proc, simix_global->process_list) {
863     xbt_dynar_push(res,&proc);
864   }
865   return res;
866 }
867
868
869 void SIMIX_process_on_exit_runall(smx_process_t process) {
870   s_smx_process_exit_fun_t exit_fun;
871
872   while (!xbt_dynar_is_empty(process->on_exit)) {
873     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
874     (exit_fun.fun)(exit_fun.arg);
875   }
876 }
877
878 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
879                                int_f_pvoid_t fun, void *data) {
880   SIMIX_process_on_exit(process, fun, data);
881 }
882
883 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
884   xbt_assert(process, "current process not found: are you in maestro context ?");
885
886   if (!process->on_exit) {
887     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
888   }
889
890   s_smx_process_exit_fun_t exit_fun = {fun, data};
891
892   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
893 }
894
895 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
896                                         int auto_restart) {
897   SIMIX_process_auto_restart_set(process, auto_restart);        
898 }
899 /**
900  * \brief Sets the auto-restart status of the process.
901  * If set to 1, the process will be automatically restarted when its host
902  * comes back.
903  */
904 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
905   process->auto_restart = auto_restart;
906 }
907
908 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
909   return SIMIX_process_restart(process, simcall->issuer);       
910 }
911 /**
912  * \brief Restart a process.
913  * Restart a process, starting it again from the beginning.
914  */
915 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
916   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
917   //retrieve the arguments of the old process
918   //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
919   s_smx_process_arg_t arg;
920   arg.code = process->code;
921   arg.hostname = sg_host_name(process->smx_host);
922   arg.kill_time = process->kill_time;
923   arg.argc = process->argc;
924   arg.data = process->data;
925   int i;
926   arg.argv = xbt_new(char*,process->argc + 1);
927   for (i = 0; i < arg.argc; i++) {
928     arg.argv[i] = xbt_strdup(process->argv[i]);
929   }
930   arg.argv[process->argc] = NULL;
931   arg.properties = NULL;
932   arg.auto_restart = process->auto_restart;
933   //kill the old process
934   SIMIX_process_kill(process,issuer);
935   //start the new process
936   smx_process_t new_process;
937   if (simix_global->create_process_function) {
938     simix_global->create_process_function(&new_process,
939                                           arg.argv[0],
940                                           arg.code,
941                                           arg.data,
942                                           arg.hostname,
943                                           arg.kill_time,
944                                           arg.argc,
945                                           arg.argv,
946                                           arg.properties,
947                                           arg.auto_restart);
948   }
949   else {
950     simcall_process_create(&new_process,
951                                           arg.argv[0],
952                                           arg.code,
953                                           arg.data,
954                                           arg.hostname,
955                                           arg.kill_time,
956                                           arg.argc,
957                                           arg.argv,
958                                           arg.properties,
959                                           arg.auto_restart);
960
961   }
962   return new_process;
963 }