Logo AND Algorithmique Numérique Distribuée

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