Logo AND Algorithmique Numérique Distribuée

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