Logo AND Algorithmique Numérique Distribuée

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