Logo AND Algorithmique Numérique Distribuée

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