Logo AND Algorithmique Numérique Distribuée

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