Logo AND Algorithmique Numérique Distribuée

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