Logo AND Algorithmique Numérique Distribuée

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