Logo AND Algorithmique Numérique Distribuée

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