Logo AND Algorithmique Numérique Distribuée

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