Logo AND Algorithmique Numérique Distribuée

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