Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
55cd83357b2aaefacaf784ef29eedb2bdd7b8f2e
[simgrid.git] / src / simix / ActorImpl.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc/mc.h"
7 #include "smx_private.hpp"
8 #include "src/kernel/activity/IoImpl.hpp"
9 #include "src/kernel/activity/SleepImpl.hpp"
10 #include "src/kernel/activity/SynchroRaw.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/mc/remote/Client.hpp"
13 #include "src/simix/smx_host_private.hpp"
14 #include "src/simix/smx_io_private.hpp"
15 #include "src/simix/smx_synchro_private.hpp"
16 #include "src/surf/cpu_interface.hpp"
17 #include "xbt/ex.hpp"
18
19 #ifdef HAVE_SMPI
20 #include "src/smpi/include/private.hpp"
21 #endif
22
23 #include <boost/range/algorithm.hpp>
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
26
27 unsigned long simix_process_maxpid = 0;
28
29 /**
30  * \brief Returns the current agent.
31  *
32  * This functions returns the currently running SIMIX process.
33  *
34  * \return The SIMIX process
35  */
36 smx_actor_t SIMIX_process_self()
37 {
38   smx_context_t self_context = SIMIX_context_self();
39
40   return (self_context != nullptr) ? self_context->process() : nullptr;
41 }
42
43 /**
44  * \brief Returns whether a process has pending asynchronous communications.
45  * \return true if there are asynchronous communications in this process
46  */
47 int SIMIX_process_has_pending_comms(smx_actor_t process) {
48
49   return process->comms.size() > 0;
50 }
51
52 /**
53  * \brief Moves a process to the list of processes to destroy.
54  */
55 void SIMIX_process_cleanup(smx_actor_t process)
56 {
57   XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->get_cname(), process,
58             process->waiting_synchro.get());
59
60   process->finished_ = true;
61   SIMIX_process_on_exit_runall(process);
62
63   /* Unregister from the kill timer if any */
64   if (process->kill_timer != nullptr) {
65     SIMIX_timer_remove(process->kill_timer);
66     process->kill_timer = nullptr;
67   }
68
69   xbt_os_mutex_acquire(simix_global->mutex);
70
71   /* cancel non-blocking communications */
72   while (not process->comms.empty()) {
73     smx_activity_t synchro = process->comms.front();
74     process->comms.pop_front();
75     simgrid::kernel::activity::CommImplPtr comm =
76         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
77
78     /* make sure no one will finish the comm after this process is destroyed,
79      * because src_proc or dst_proc would be an invalid pointer */
80
81     if (comm->src_proc == process) {
82       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
83                 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
84       comm->src_proc = nullptr;
85
86     } else if (comm->dst_proc == process) {
87       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
88                 comm->src_proc, comm->dst_proc);
89       comm->dst_proc = nullptr;
90
91       if (comm->detached && comm->src_proc != nullptr) {
92         /* the comm will be freed right now, remove it from the sender */
93         comm->src_proc->comms.remove(comm);
94       }
95     } else {
96       xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
97     }
98     comm->cancel();
99   }
100
101   XBT_DEBUG("%p should not be run anymore",process);
102   simix_global->process_list.erase(process->pid_);
103   if (process->host_ && process->host_process_list_hook.is_linked())
104     simgrid::xbt::intrusive_erase(process->host_->extension<simgrid::simix::Host>()->process_list, *process);
105   if (not process->smx_destroy_list_hook.is_linked()) {
106 #if SIMGRID_HAVE_MC
107     xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
108 #endif
109     simix_global->process_to_destroy.push_back(*process);
110   }
111   process->context_->iwannadie = 0;
112
113   xbt_os_mutex_release(simix_global->mutex);
114 }
115
116 /**
117  * Garbage collection
118  *
119  * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
120  */
121 void SIMIX_process_empty_trash()
122 {
123   while (not simix_global->process_to_destroy.empty()) {
124     smx_actor_t process = &simix_global->process_to_destroy.front();
125     simix_global->process_to_destroy.pop_front();
126     XBT_DEBUG("Getting rid of %p",process);
127     intrusive_ptr_release(process);
128   }
129 #if SIMGRID_HAVE_MC
130   xbt_dynar_reset(simix_global->dead_actors_vector);
131 #endif
132 }
133
134 namespace simgrid {
135
136 namespace kernel {
137 namespace actor {
138
139 ActorImpl::~ActorImpl()
140 {
141   delete this->context_;
142 }
143
144 static void dying_daemon(int /*exit_status*/, void* data)
145 {
146   std::vector<ActorImpl*>* vect = &simix_global->daemons;
147
148   auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
149   xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
150
151   /* Don't move the whole content since we don't really care about the order */
152   std::swap(*it, vect->back());
153   vect->pop_back();
154 }
155
156 /** This process will be terminated automatically when the last non-daemon process finishes */
157 void ActorImpl::daemonize()
158 {
159   if (not daemon_) {
160     daemon_ = true;
161     simix_global->daemons.push_back(this);
162     SIMIX_process_on_exit(this, dying_daemon, this);
163   }
164 }
165
166 simgrid::s4u::Actor* ActorImpl::restart()
167 {
168   XBT_DEBUG("Restarting process %s on %s", get_cname(), host_->get_cname());
169
170   // retrieve the arguments of the old process
171   // FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
172   simgrid::kernel::actor::ProcessArg arg;
173   arg.name         = name_;
174   arg.code         = code;
175   arg.host         = host_;
176   arg.kill_time    = SIMIX_timer_get_date(kill_timer);
177   arg.data         = userdata_;
178   arg.properties   = nullptr;
179   arg.auto_restart = auto_restart_;
180
181   // kill the old process
182   SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
183
184   // start the new process
185   ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
186                                                            arg.properties.get(), nullptr);
187   if (arg.kill_time >= 0)
188     simcall_process_set_kill_time(actor, arg.kill_time);
189   if (arg.auto_restart)
190     actor->auto_restart_ = arg.auto_restart;
191
192   return actor->ciface();
193 }
194
195 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
196 {
197   if (suspended_) {
198     XBT_DEBUG("Actor '%s' is already suspended", get_cname());
199     return nullptr;
200   }
201
202   suspended_ = 1;
203
204   /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
205   if (this != issuer) {
206     if (waiting_synchro)
207       waiting_synchro->suspend();
208     /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
209
210     return nullptr;
211   } else {
212     return SIMIX_execution_start("suspend", 0.0, 1.0, 0.0, this->host_);
213   }
214 }
215
216 void ActorImpl::resume()
217 {
218   XBT_IN("process = %p", this);
219
220   if (context_->iwannadie) {
221     XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
222     return;
223   }
224
225   if (not suspended_)
226     return;
227   suspended_ = 0;
228
229   /* resume the synchronization that was blocking the resumed actor. */
230   if (waiting_synchro)
231     waiting_synchro->resume();
232
233   XBT_OUT();
234 }
235
236 smx_activity_t ActorImpl::sleep(double duration)
237 {
238   if (host_->is_off())
239     THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host_->get_cname());
240
241   simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
242   synchro->host                                 = host_;
243   synchro->surf_sleep                           = host_->pimpl_cpu->sleep(duration);
244   synchro->surf_sleep->set_data(synchro);
245   XBT_DEBUG("Create sleep synchronization %p", synchro);
246
247   return synchro;
248 }
249
250 void create_maestro(simgrid::simix::ActorCode code)
251 {
252   smx_actor_t maestro = nullptr;
253   /* Create maestro process and initialize it */
254   maestro           = new simgrid::kernel::actor::ActorImpl();
255   maestro->pid_     = simix_process_maxpid++;
256   maestro->name_    = "";
257   maestro->set_user_data(nullptr);
258
259   if (not code) {
260     maestro->context_ = SIMIX_context_new(simgrid::simix::ActorCode(), nullptr, maestro);
261   } else {
262     if (not simix_global)
263       xbt_die("simix is not initialized, please call MSG_init first");
264     maestro->context_ = simix_global->context_factory->create_maestro(code, maestro);
265   }
266
267   maestro->simcall.issuer = maestro;
268   simix_global->maestro_process = maestro;
269 }
270
271 } // namespace actor
272 }
273 }
274
275 /** @brief Creates and runs the maestro process */
276 void SIMIX_maestro_create(void (*code)(void*), void* data)
277 {
278   simgrid::kernel::actor::create_maestro(std::bind(code, data));
279 }
280
281 /**
282  * \brief Internal function to create a process.
283  *
284  * This function actually creates the process.
285  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
286  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
287  *
288  * \return the process created
289  */
290 smx_actor_t SIMIX_process_create(const char* name, simgrid::simix::ActorCode code, void* data, simgrid::s4u::Host* host,
291                                  std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
292 {
293
294   XBT_DEBUG("Start process %s on host '%s'", name, host->get_cname());
295
296   if (host->is_off()) {
297     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->get_cname());
298     return nullptr;
299   }
300
301   smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
302
303   xbt_assert(code && host != nullptr, "Invalid parameters");
304   /* Process data */
305   process->pid_           = simix_process_maxpid++;
306   process->name_          = simgrid::xbt::string(name);
307   process->host_          = host;
308   process->set_user_data(data);
309   process->simcall.issuer = process;
310
311   if (parent_process != nullptr) {
312     process->ppid_ = parent_process->pid_;
313   }
314
315   process->code         = code;
316
317   XBT_VERB("Create context %s", process->get_cname());
318   process->context_ = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
319
320   /* Add properties */
321   if (properties != nullptr)
322     for (auto const& kv : *properties)
323       process->set_property(kv.first, kv.second);
324
325   /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
326   if (host->extension<simgrid::simix::Host>() == nullptr)
327     host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
328
329   /* Add the process to its host's process list */
330   host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
331
332   XBT_DEBUG("Start context '%s'", process->get_cname());
333
334   /* Now insert it in the global process list and in the process to run list */
335   simix_global->process_list[process->pid_] = process;
336   XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
337   simix_global->process_to_run.push_back(process);
338   intrusive_ptr_add_ref(process);
339
340   /* The onCreation() signal must be delayed until there, where the pid and everything is set */
341   simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
342   simgrid::s4u::Actor::on_creation(tmp);
343
344   return process;
345 }
346
347 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
348                                  std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
349 {
350   // This is mostly a copy/paste from SIMIX_process_new(),
351   // it'd be nice to share some code between those two functions.
352
353   sg_host_t host = sg_host_by_name(hostname);
354   XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
355
356   if (host->is_off()) {
357     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
358     return nullptr;
359   }
360
361   smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
362   /* Process data */
363   process->pid_  = simix_process_maxpid++;
364   process->name_ = std::string(name);
365   process->host_ = host;
366   process->set_user_data(data);
367   process->simcall.issuer = process;
368
369   if (parent_process != nullptr) {
370     process->ppid_ = parent_process->pid_;
371   }
372
373   /* Process data for auto-restart */
374   process->code = nullptr;
375
376   XBT_VERB("Create context %s", process->get_cname());
377   if (not simix_global)
378     xbt_die("simix is not initialized, please call MSG_init first");
379   process->context_ = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
380
381   /* Add properties */
382   if (properties != nullptr)
383     for (auto const& kv : *properties)
384       process->set_property(kv.first, kv.second);
385
386   /* Add the process to it's host process list */
387   host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
388
389   /* Now insert it in the global process list and in the process to run list */
390   simix_global->process_list[process->pid_] = process;
391   XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
392   simix_global->process_to_run.push_back(process);
393   intrusive_ptr_add_ref(process);
394
395   auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context_);
396   if (not context)
397     xbt_die("Not a suitable context");
398
399   context->attach_start();
400
401   /* The onCreation() signal must be delayed until there, where the pid and everything is set */
402   simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
403   simgrid::s4u::Actor::on_creation(tmp);
404
405   return process;
406 }
407
408 void SIMIX_process_detach()
409 {
410   auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
411   if (not context)
412     xbt_die("Not a suitable context");
413
414   auto process = context->process();
415   simix_global->cleanup_process_function(process);
416   context->attach_stop();
417 }
418
419 /**
420  * \brief Executes the processes from simix_global->process_to_run.
421  *
422  * The processes of simix_global->process_to_run are run (in parallel if
423  * possible).  On exit, simix_global->process_to_run is empty, and
424  * simix_global->process_that_ran contains the list of processes that just ran.
425  * The two lists are swapped so, be careful when using them before and after a
426  * call to this function.
427  */
428 void SIMIX_process_runall()
429 {
430   SIMIX_context_runall();
431
432   simix_global->process_to_run.swap(simix_global->process_that_ran);
433   simix_global->process_to_run.clear();
434 }
435
436 /**
437  * \brief Internal function to kill a SIMIX process.
438  *
439  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
440  * or directly for SIMIX internal purposes.
441  *
442  * \param process poor victim
443  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
444  */
445 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
446
447   if (process->finished_) {
448     XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->get_cname(),
449               process->host_->get_cname());
450     return;
451   }
452
453   XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", issuer->get_cname(), issuer->host_->get_cname(),
454             process->get_cname(), process->host_->get_cname());
455
456   process->context_->iwannadie = 1;
457   process->blocked_           = 0;
458   process->suspended_         = 0;
459   process->exception = nullptr;
460
461   /* destroy the blocking synchro if any */
462   if (process->waiting_synchro != nullptr) {
463
464     simgrid::kernel::activity::ExecImplPtr exec =
465         boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
466     simgrid::kernel::activity::CommImplPtr comm =
467         boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
468     simgrid::kernel::activity::SleepImplPtr sleep =
469         boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
470     simgrid::kernel::activity::RawImplPtr raw =
471         boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
472     simgrid::kernel::activity::IoImplPtr io =
473         boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
474
475     if (exec != nullptr) {
476       if (exec->surf_action_) {
477         exec->surf_action_->cancel();
478         exec->surf_action_->unref();
479         exec->surf_action_ = nullptr;
480       }
481     } else if (comm != nullptr) {
482       process->comms.remove(process->waiting_synchro);
483       comm->cancel();
484       // Remove first occurrence of &process->simcall:
485       auto i = boost::range::find(process->waiting_synchro->simcalls_, &process->simcall);
486       if (i != process->waiting_synchro->simcalls_.end())
487         process->waiting_synchro->simcalls_.remove(&process->simcall);
488     } else if (sleep != nullptr) {
489       SIMIX_process_sleep_destroy(process->waiting_synchro);
490
491     } else if (raw != nullptr) {
492       SIMIX_synchro_stop_waiting(process, &process->simcall);
493
494     } else if (io != nullptr) {
495       SIMIX_io_destroy(process->waiting_synchro);
496     } else {
497       xbt_die("Unknown type of activity");
498     }
499
500     process->waiting_synchro = nullptr;
501   }
502   if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
503           end(simix_global->process_to_run) &&
504       process != issuer) {
505     XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
506     simix_global->process_to_run.push_back(process);
507   }
508 }
509
510 /** @brief Ask another process to raise the given exception
511  *
512  * @param process The process that should raise that exception
513  * @param cat category of exception
514  * @param value value associated to the exception
515  * @param msg string information associated to the exception
516  */
517 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
518   SMX_EXCEPTION(process, cat, value, msg);
519
520   if (process->suspended_)
521     process->resume();
522
523   /* cancel the blocking synchro if any */
524   if (process->waiting_synchro) {
525
526     simgrid::kernel::activity::ExecImplPtr exec =
527         boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
528     if (exec != nullptr && exec->surf_action_)
529       exec->surf_action_->cancel();
530
531     simgrid::kernel::activity::CommImplPtr comm =
532         boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
533     if (comm != nullptr) {
534       process->comms.remove(comm);
535       comm->cancel();
536     }
537
538     simgrid::kernel::activity::SleepImplPtr sleep =
539         boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
540     if (sleep != nullptr) {
541       SIMIX_process_sleep_destroy(process->waiting_synchro);
542       if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
543               end(simix_global->process_to_run) &&
544           process != SIMIX_process_self()) {
545         XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
546         simix_global->process_to_run.push_back(process);
547       }
548     }
549
550     simgrid::kernel::activity::RawImplPtr raw =
551         boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
552     if (raw != nullptr) {
553       SIMIX_synchro_stop_waiting(process, &process->simcall);
554     }
555
556     simgrid::kernel::activity::IoImplPtr io =
557         boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
558     if (io != nullptr) {
559       SIMIX_io_destroy(process->waiting_synchro);
560     }
561   }
562   process->waiting_synchro = nullptr;
563
564 }
565
566 /**
567  * \brief Kills all running processes.
568  * \param issuer this one will not be killed
569  */
570 void SIMIX_process_killall(smx_actor_t issuer)
571 {
572   for (auto const& kv : simix_global->process_list)
573     if (kv.second != issuer)
574       SIMIX_process_kill(kv.second, issuer);
575 }
576
577 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
578 {
579   xbt_assert((actor != nullptr), "Invalid parameters");
580   simgrid::xbt::intrusive_erase(actor->host_->extension<simgrid::simix::Host>()->process_list, *actor);
581   actor->host_ = dest;
582   dest->extension<simgrid::simix::Host>()->process_list.push_back(*actor);
583 }
584
585 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
586 {
587   smx_activity_t sync_suspend = process->suspend(simcall->issuer);
588
589   if (process != simcall->issuer) {
590     SIMIX_simcall_answer(simcall);
591   } else {
592     sync_suspend->simcalls_.push_back(simcall);
593     process->waiting_synchro = sync_suspend;
594     process->waiting_synchro->suspend();
595   }
596   /* If we are suspending ourselves, then just do not finish the simcall now */
597 }
598
599 int SIMIX_process_get_maxpid() {
600   return simix_process_maxpid;
601 }
602
603 int SIMIX_process_count()
604 {
605   return simix_global->process_list.size();
606 }
607
608 void* SIMIX_process_self_get_data()
609 {
610   smx_actor_t self = SIMIX_process_self();
611
612   if (not self) {
613     return nullptr;
614   }
615   return self->get_user_data();
616 }
617
618 void SIMIX_process_self_set_data(void *data)
619 {
620   SIMIX_process_self()->set_user_data(data);
621 }
622
623
624 /* needs to be public and without simcall because it is called
625    by exceptions and logging events */
626 const char* SIMIX_process_self_get_name() {
627
628   smx_actor_t process = SIMIX_process_self();
629   if (process == nullptr || process == simix_global->maestro_process)
630     return "maestro";
631
632   return process->get_cname();
633 }
634
635 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
636 {
637   if (process->finished_) {
638     // The joined process is already finished, just wake up the issuer process right away
639     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
640     SIMIX_simcall_answer(simcall);
641     return;
642   }
643   smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
644   sync->simcalls_.push_back(simcall);
645   simcall->issuer->waiting_synchro = sync;
646 }
647
648 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
649 {
650   smx_activity_t res = issuer->sleep(timeout);
651   intrusive_ptr_add_ref(res.get());
652   SIMIX_process_on_exit(process,
653                         [](int, void* arg) {
654                           auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
655                           if (sleep->surf_sleep)
656                             sleep->surf_sleep->finish(simgrid::kernel::resource::Action::State::FINISHED);
657                           intrusive_ptr_release(sleep);
658                         },
659                         res.get());
660   return res;
661 }
662
663 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
664 {
665   if (MC_is_active() || MC_record_replay_is_active()) {
666     MC_process_clock_add(simcall->issuer, duration);
667     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
668     SIMIX_simcall_answer(simcall);
669     return;
670   }
671   smx_activity_t sync = simcall->issuer->sleep(duration);
672   sync->simcalls_.push_back(simcall);
673   simcall->issuer->waiting_synchro = sync;
674 }
675
676 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
677 {
678   XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
679   simgrid::kernel::activity::SleepImplPtr sleep =
680       boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
681
682   if (sleep->surf_sleep) {
683     sleep->surf_sleep->unref();
684     sleep->surf_sleep = nullptr;
685   }
686 }
687
688 /**
689  * \brief Calling this function makes the process to yield.
690  *
691  * Only the current process can call this function, giving back the control to maestro.
692  *
693  * \param self the current process
694  */
695 void SIMIX_process_yield(smx_actor_t self)
696 {
697   XBT_DEBUG("Yield actor '%s'", self->get_cname());
698
699   /* Go into sleep and return control to maestro */
700   self->context_->suspend();
701
702   /* Ok, maestro returned control to us */
703   XBT_DEBUG("Control returned to me: '%s'", self->get_cname());
704
705   if (self->context_->iwannadie) {
706     XBT_DEBUG("I wanna die!");
707     self->finished_ = true;
708     /* execute the on_exit functions */
709     SIMIX_process_on_exit_runall(self);
710     /* Add the process to the list of process to restart, only if the host is down */
711     if (self->auto_restart_ && self->host_->is_off()) {
712       SIMIX_host_add_auto_restart_process(self->host_, self->get_cname(), self->code, self->get_user_data(),
713                                           SIMIX_timer_get_date(self->kill_timer), self->get_properties(),
714                                           self->auto_restart_);
715     }
716     XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host_->get_cname());
717     self->context_->stop();
718   }
719
720   if (self->suspended_) {
721     XBT_DEBUG("Hey! I'm suspended.");
722     xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
723     self->suspended_ = 0;
724     self->suspend(self);
725   }
726
727   if (self->exception != nullptr) {
728     XBT_DEBUG("Wait, maestro left me an exception");
729     std::exception_ptr exception = std::move(self->exception);
730     self->exception = nullptr;
731     std::rethrow_exception(std::move(exception));
732   }
733
734   if (SMPI_switch_data_segment && not self->finished_) {
735     SMPI_switch_data_segment(self->iface());
736   }
737 }
738
739 /** @brief Returns the list of processes to run. */
740 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
741 {
742   return simix_global->process_to_run;
743 }
744
745 /** @brief Returns the process from PID. */
746 smx_actor_t SIMIX_process_from_PID(aid_t PID)
747 {
748   auto process = simix_global->process_list.find(PID);
749   return process == simix_global->process_list.end() ? nullptr : process->second;
750 }
751
752 void SIMIX_process_on_exit_runall(smx_actor_t process) {
753   simgrid::s4u::Actor::on_destruction(process->iface());
754   smx_process_exit_status_t exit_status = (process->context_->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
755   while (not process->on_exit.empty()) {
756     s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
757     process->on_exit.pop_back();
758     (exit_fun.fun)(exit_status, exit_fun.arg);
759   }
760 }
761
762 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void* data)
763 {
764   SIMIX_process_on_exit(process, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
765 }
766
767 void SIMIX_process_on_exit(smx_actor_t process, std::function<void(int, void*)> fun, void* data)
768 {
769   xbt_assert(process, "current process not found: are you in maestro context ?");
770
771   process->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
772 }
773
774 /** @brief Restart a process, starting it again from the beginning. */
775 /**
776  * \ingroup simix_process_management
777  * \brief Creates and runs a new SIMIX process.
778  *
779  * The structure and the corresponding thread are created and put in the list of ready processes.
780  *
781  * \param name a name for the process. It is for user-level information and can be nullptr.
782  * \param code the main function of the process
783  * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
784  * be nullptr.
785  * It can be retrieved with the method ActorImpl::getUserData().
786  * \param host where the new agent is executed.
787  * \param argc first argument passed to \a code
788  * \param argv second argument passed to \a code
789  * \param properties the properties of the process
790  */
791 smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
792                                    char** argv, std::unordered_map<std::string, std::string>* properties)
793 {
794   if (name == nullptr)
795     name = "";
796   auto wrapped_code = simgrid::xbt::wrap_main(code, argc, argv);
797   for (int i = 0; i != argc; ++i)
798     xbt_free(argv[i]);
799   xbt_free(argv);
800   smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
801   return res;
802 }
803
804 smx_actor_t simcall_process_create(const char* name, simgrid::simix::ActorCode code, void* data, sg_host_t host,
805                                    std::unordered_map<std::string, std::string>* properties)
806 {
807   if (name == nullptr)
808     name = "";
809   smx_actor_t self = SIMIX_process_self();
810   return simgrid::simix::simcall([name, code, data, host, properties, self] {
811     return SIMIX_process_create(name, std::move(code), data, host, properties, self);
812   });
813 }