Logo AND Algorithmique Numérique Distribuée

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