Logo AND Algorithmique Numérique Distribuée

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