Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' into surf++
[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     /* FIXME: computation size is zero. Is it okay that bound is zero ? */
495     return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
496   }
497 }
498
499 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
500   SIMIX_process_resume(process, simcall->issuer);
501 }
502
503 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
504 {
505   xbt_assert((process != NULL), "Invalid parameters");
506
507   XBT_IN("process = %p, issuer = %p", process, issuer);
508
509   if(process->context->iwannadie) {
510     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
511     return;
512   }
513
514   if(!process->suspended) return;
515   process->suspended = 0;
516
517   /* If we are resuming another process, resume the action it was waiting for
518      if any. Otherwise add it to the list of process to run in the next round. */
519   if (process != issuer) {
520
521     if (process->waiting_action) {
522
523       switch (process->waiting_action->type) {
524
525         case SIMIX_ACTION_EXECUTE:          
526         case SIMIX_ACTION_PARALLEL_EXECUTE:
527           SIMIX_host_execution_resume(process->waiting_action);
528           break;
529
530         case SIMIX_ACTION_COMMUNICATE:
531           SIMIX_comm_resume(process->waiting_action);
532           break;
533
534         case SIMIX_ACTION_SLEEP:
535           SIMIX_process_sleep_resume(process->waiting_action);
536           break;
537
538         case SIMIX_ACTION_SYNCHRO:
539           /* I cannot resume it now. This is delayed to when the process is rescheduled at
540            * the end of the synchro. */
541           break;
542
543         default:
544           xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
545               (int)process->waiting_action->type);
546       }
547     }
548   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
549
550   XBT_OUT();
551 }
552
553 int SIMIX_process_get_maxpid(void) {
554   return simix_process_maxpid;
555 }
556
557 int SIMIX_pre_process_count(smx_simcall_t simcall){
558   return SIMIX_process_count();
559 }
560 int SIMIX_process_count(void)
561 {
562   return xbt_swag_size(simix_global->process_list);
563 }
564
565 int SIMIX_pre_process_get_PID(smx_simcall_t simcall, smx_process_t self){
566    return SIMIX_process_get_PID(self);
567 }
568
569 int SIMIX_process_get_PID(smx_process_t self){
570   if (self == NULL)
571     return 0;
572   else
573     return self->pid;
574 }
575
576 int SIMIX_pre_process_get_PPID(smx_simcall_t simcall, smx_process_t self){
577   return SIMIX_process_get_PPID(self);
578 }
579
580 int SIMIX_process_get_PPID(smx_process_t self){
581   if (self == NULL)
582     return 0;
583   else
584     return self->ppid;
585 }
586
587 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
588   return SIMIX_process_self_get_data(self);     
589 }
590
591 void* SIMIX_process_self_get_data(smx_process_t self)
592 {
593   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
594
595   if (!self) {
596     return NULL;
597   }
598   return SIMIX_process_get_data(self);
599 }
600
601 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
602                                 void *data){
603   SIMIX_process_set_data(process, data);
604 }
605 void SIMIX_process_self_set_data(smx_process_t self, void *data)
606 {
607   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
608
609   SIMIX_process_set_data(self, data);
610 }
611
612 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
613   return SIMIX_process_get_data(process);
614 }
615 void* SIMIX_process_get_data(smx_process_t process)
616 {
617   return process->data;
618 }
619
620 void SIMIX_process_set_data(smx_process_t process, void *data)
621 {
622   process->data = data;
623 }
624
625 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
626   return SIMIX_process_get_host(process);
627 }
628 smx_host_t SIMIX_process_get_host(smx_process_t process)
629 {
630   return process->smx_host;
631 }
632
633 /* needs to be public and without simcall because it is called
634    by exceptions and logging events */
635 const char* SIMIX_process_self_get_name(void) {
636
637   smx_process_t process = SIMIX_process_self();
638   if (process == NULL || process == simix_global->maestro_process)
639     return "";
640
641   return SIMIX_process_get_name(process);
642 }
643
644 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
645   return SIMIX_process_get_name(process);
646 }
647 const char* SIMIX_process_get_name(smx_process_t process)
648 {
649   return process->name;
650 }
651
652 smx_process_t SIMIX_process_get_by_name(const char* name)
653 {
654   smx_process_t proc;
655
656   xbt_swag_foreach(proc, simix_global->process_list)
657   {
658     if(!strcmp(name, proc->name))
659       return proc;
660   }
661   return NULL;
662 }
663
664 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
665   return SIMIX_process_is_suspended(process);
666 }
667 int SIMIX_process_is_suspended(smx_process_t process)
668 {
669   return process->suspended;
670 }
671
672 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
673   return SIMIX_process_get_properties(process);
674 }
675 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
676 {
677   return process->properties;
678 }
679
680 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
681 {
682   if (MC_is_active()) {
683     MC_process_clock_add(simcall->issuer, duration);
684     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
685     SIMIX_simcall_answer(simcall);
686     return;
687   }
688   smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
689   xbt_fifo_push(action->simcalls, simcall);
690   simcall->issuer->waiting_action = action;
691 }
692
693 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
694 {
695   smx_action_t action;
696   smx_host_t host = process->smx_host;
697
698   /* check if the host is active */
699   if (surf_resource_get_state(surf_workstation_resource_priv(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_sleep(host, duration);
714
715   surf_action_set_data(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   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
727
728   while ((simcall = xbt_fifo_shift(action->simcalls))) {
729
730     switch(surf_action_get_state(action->sleep.surf_sleep)){
731       case SURF_ACTION_FAILED:
732         simcall->issuer->context->iwannadie = 1;
733         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
734         state = SIMIX_SRC_HOST_FAILURE;
735         break;
736
737       case SURF_ACTION_DONE:
738         state = SIMIX_DONE;
739         break;
740
741       default:
742         THROW_IMPOSSIBLE;
743         break;
744     }
745     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
746       simcall->issuer->context->iwannadie = 1;
747     }
748     simcall_process_sleep__set__result(simcall, state);
749     simcall->issuer->waiting_action = NULL;
750     SIMIX_simcall_answer(simcall);
751
752   }
753   SIMIX_process_sleep_destroy(action);
754 }
755
756 void SIMIX_process_sleep_destroy(smx_action_t action)
757 {
758   XBT_DEBUG("Destroy action %p", action);
759   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
760
761   if (action->sleep.surf_sleep)
762     surf_action_unref(action->sleep.surf_sleep);
763   xbt_mallocator_release(simix_global->action_mallocator, action);
764 }
765
766 void SIMIX_process_sleep_suspend(smx_action_t action)
767 {
768   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
769   surf_action_suspend(action->sleep.surf_sleep);
770 }
771
772 void SIMIX_process_sleep_resume(smx_action_t action)
773 {
774   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
775   surf_action_resume(action->sleep.surf_sleep);
776 }
777
778 /** 
779  * \brief Calling this function makes the process to yield.
780  *
781  * Only the current process can call this function, giving back the control to
782  * maestro.
783  *
784  * \param self the current process
785  */
786 void SIMIX_process_yield(smx_process_t self)
787 {
788   XBT_DEBUG("Yield process '%s'", self->name);
789
790   /* Go into sleep and return control to maestro */
791   SIMIX_context_suspend(self->context);
792
793   /* Ok, maestro returned control to us */
794   XBT_DEBUG("Control returned to me: '%s'", self->name);
795
796   if (self->new_host) {
797     SIMIX_process_change_host(self, self->new_host);
798     self->new_host = NULL;
799   }
800
801   if (self->context->iwannadie){
802     XBT_DEBUG("I wanna die!");
803     SIMIX_process_stop(self);
804   }
805
806   if(self->suspended) {
807     xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
808     self->suspended = 0;
809     SIMIX_process_suspend(self,self);
810   }
811
812   if (self->doexception) {
813     XBT_DEBUG("Wait, maestro left me an exception");
814     self->doexception = 0;
815     SMX_THROW();
816   }
817
818 }
819
820 /* callback: context fetching */
821 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
822 {
823   return SIMIX_process_self()->running_ctx;
824 }
825
826 /* callback: termination */
827 void SIMIX_process_exception_terminate(xbt_ex_t * e)
828 {
829   xbt_ex_display(e);
830   xbt_abort();
831 }
832
833 smx_context_t SIMIX_process_get_context(smx_process_t p) {
834   return p->context;
835 }
836
837 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
838   p->context = c;
839 }
840
841 /**
842  * \brief Returns the list of processes to run.
843  */
844 xbt_dynar_t SIMIX_process_get_runnable(void)
845 {
846   return simix_global->process_to_run;
847 }
848
849 /**
850  * \brief Returns the process from PID.
851  */
852 smx_process_t SIMIX_process_from_PID(int PID)
853 {
854   smx_process_t proc;
855   xbt_swag_foreach(proc, simix_global->process_list)
856   {
857    if(proc->pid == PID)
858    return proc;
859   }
860   return NULL;
861 }
862
863 /** @brief returns a dynar containg all currently existing processes */
864 xbt_dynar_t SIMIX_processes_as_dynar(void) {
865   smx_process_t proc;
866   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
867   xbt_swag_foreach(proc, simix_global->process_list) {
868     xbt_dynar_push(res,&proc);
869   }
870   return res;
871 }
872
873
874 void SIMIX_process_on_exit_runall(smx_process_t process) {
875   s_smx_process_exit_fun_t exit_fun;
876
877   while (!xbt_dynar_is_empty(process->on_exit)) {
878     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
879     (exit_fun.fun)(exit_fun.arg);
880   }
881 }
882
883 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
884                                int_f_pvoid_t fun, void *data) {
885   SIMIX_process_on_exit(process, fun, data);
886 }
887
888 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
889   xbt_assert(process, "current process not found: are you in maestro context ?");
890
891   if (!process->on_exit) {
892     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
893   }
894
895   s_smx_process_exit_fun_t exit_fun = {fun, data};
896
897   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
898 }
899
900 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
901                                         int auto_restart) {
902   SIMIX_process_auto_restart_set(process, auto_restart);        
903 }
904 /**
905  * \brief Sets the auto-restart status of the process.
906  * If set to 1, the process will be automatically restarted when its host
907  * comes back.
908  */
909 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
910   process->auto_restart = auto_restart;
911 }
912
913 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
914   return SIMIX_process_restart(process, simcall->issuer);       
915 }
916 /**
917  * \brief Restart a process.
918  * Restart a process, starting it again from the beginning.
919  */
920 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
921   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
922   //retrieve the arguments of the old process
923   //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
924   s_smx_process_arg_t arg;
925   arg.code = process->code;
926   arg.hostname = sg_host_name(process->smx_host);
927   arg.kill_time = process->kill_time;
928   arg.argc = process->argc;
929   arg.data = process->data;
930   int i;
931   arg.argv = xbt_new(char*,process->argc + 1);
932   for (i = 0; i < arg.argc; i++) {
933     arg.argv[i] = xbt_strdup(process->argv[i]);
934   }
935   arg.argv[process->argc] = NULL;
936   arg.properties = NULL;
937   arg.auto_restart = process->auto_restart;
938   //kill the old process
939   SIMIX_process_kill(process,issuer);
940   //start the new process
941   smx_process_t new_process;
942   if (simix_global->create_process_function) {
943     simix_global->create_process_function(&new_process,
944                                           arg.argv[0],
945                                           arg.code,
946                                           arg.data,
947                                           arg.hostname,
948                                           arg.kill_time,
949                                           arg.argc,
950                                           arg.argv,
951                                           arg.properties,
952                                           arg.auto_restart);
953   }
954   else {
955     simcall_process_create(&new_process,
956                                           arg.argv[0],
957                                           arg.code,
958                                           arg.data,
959                                           arg.hostname,
960                                           arg.kill_time,
961                                           arg.argc,
962                                           arg.argv,
963                                           arg.properties,
964                                           arg.auto_restart);
965
966   }
967   return new_process;
968 }