Logo AND Algorithmique Numérique Distribuée

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