Logo AND Algorithmique Numérique Distribuée

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