Logo AND Algorithmique Numérique Distribuée

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