Logo AND Algorithmique Numérique Distribuée

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