Logo AND Algorithmique Numérique Distribuée

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