Logo AND Algorithmique Numérique Distribuée

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