Logo AND Algorithmique Numérique Distribuée

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