Logo AND Algorithmique Numérique Distribuée

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