Logo AND Algorithmique Numérique Distribuée

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