Logo AND Algorithmique Numérique Distribuée

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