1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
7 #include "simgrid/Exception.hpp"
8 #include "smx_private.hpp"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/kernel/activity/IoImpl.hpp"
12 #include "src/kernel/activity/SleepImpl.hpp"
13 #include "src/kernel/activity/SynchroRaw.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/mc/remote/Client.hpp"
16 #include "src/simix/smx_host_private.hpp"
17 #include "src/simix/smx_io_private.hpp"
18 #include "src/simix/smx_synchro_private.hpp"
19 #include "src/surf/HostImpl.hpp"
20 #include "src/surf/cpu_interface.hpp"
22 #include <boost/range/algorithm.hpp>
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
26 static unsigned long simix_process_maxpid = 0;
29 * @brief Returns the current agent.
31 * This functions returns the currently running SIMIX process.
33 * @return The SIMIX process
35 smx_actor_t SIMIX_process_self()
37 smx_context_t self_context = simgrid::kernel::context::Context::self();
39 return (self_context != nullptr) ? self_context->get_actor() : nullptr;
43 * @brief Returns whether a process has pending asynchronous communications.
44 * @return true if there are asynchronous communications in this process
46 int SIMIX_process_has_pending_comms(smx_actor_t process) {
48 return process->comms.size() > 0;
52 * @brief Moves a process to the list of processes to destroy.
54 void SIMIX_process_cleanup(smx_actor_t process)
56 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->get_cname(), process,
57 process->waiting_synchro.get());
59 /* Unregister from the kill timer if any */
60 if (process->kill_timer != nullptr) {
61 SIMIX_timer_remove(process->kill_timer);
62 process->kill_timer = nullptr;
65 simix_global->mutex.lock();
67 /* cancel non-blocking communications */
68 while (not process->comms.empty()) {
69 smx_activity_t synchro = process->comms.front();
70 process->comms.pop_front();
71 simgrid::kernel::activity::CommImplPtr comm =
72 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
74 /* make sure no one will finish the comm after this process is destroyed,
75 * because src_proc or dst_proc would be an invalid pointer */
77 if (comm->src_proc == process) {
78 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
79 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
80 comm->src_proc = nullptr;
82 } else if (comm->dst_proc == process) {
83 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
84 comm->src_proc, comm->dst_proc);
85 comm->dst_proc = nullptr;
87 if (comm->detached && comm->src_proc != nullptr) {
88 /* the comm will be freed right now, remove it from the sender */
89 comm->src_proc->comms.remove(comm);
92 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
97 XBT_DEBUG("%s@%s(%ld) should not run anymore", process->get_cname(), process->iface()->get_host()->get_cname(),
99 simix_global->process_list.erase(process->pid_);
100 if (process->host_ && process->host_process_list_hook.is_linked())
101 simgrid::xbt::intrusive_erase(process->host_->pimpl_->process_list_, *process);
102 if (not process->smx_destroy_list_hook.is_linked()) {
104 xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
106 simix_global->process_to_destroy.push_back(*process);
108 process->context_->iwannadie = false;
110 simix_global->mutex.unlock();
116 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
118 void SIMIX_process_empty_trash()
120 while (not simix_global->process_to_destroy.empty()) {
121 smx_actor_t process = &simix_global->process_to_destroy.front();
122 simix_global->process_to_destroy.pop_front();
123 XBT_DEBUG("Getting rid of %p",process);
124 intrusive_ptr_release(process);
127 xbt_dynar_reset(simix_global->dead_actors_vector);
136 ActorImpl::ActorImpl(simgrid::xbt::string name, simgrid::s4u::Host* host) : name_(name), host_(host), piface_(this)
138 pid_ = simix_process_maxpid++;
139 simcall.issuer = this;
142 ActorImpl::~ActorImpl()
144 delete this->context_;
147 static void dying_daemon(int /*exit_status*/, void* data)
149 std::vector<ActorImpl*>* vect = &simix_global->daemons;
151 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
152 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
154 /* Don't move the whole content since we don't really care about the order */
155 std::swap(*it, vect->back());
159 /** This process will be terminated automatically when the last non-daemon process finishes */
160 void ActorImpl::daemonize()
164 simix_global->daemons.push_back(this);
165 SIMIX_process_on_exit(this, dying_daemon, this);
169 simgrid::s4u::Actor* ActorImpl::restart()
171 XBT_DEBUG("Restarting process %s on %s", get_cname(), host_->get_cname());
173 // retrieve the arguments of the old process
174 simgrid::kernel::actor::ProcessArg arg = ProcessArg(host_, this);
176 // kill the old process
177 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
179 // start the new process
181 SIMIX_process_create(arg.name, std::move(arg.code), arg.data, arg.host, arg.properties.get(), nullptr);
182 simcall_process_set_kill_time(actor, arg.kill_time);
183 actor->set_auto_restart(arg.auto_restart);
185 return actor->ciface();
188 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
191 XBT_DEBUG("Actor '%s' is already suspended", get_cname());
197 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
198 if (this != issuer) {
200 waiting_synchro->suspend();
201 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
205 return SIMIX_execution_start("suspend", "", 0.0, 1.0, 0.0, this->host_);
209 void ActorImpl::resume()
211 XBT_IN("process = %p", this);
213 if (context_->iwannadie) {
214 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
222 /* resume the synchronization that was blocking the resumed actor. */
224 waiting_synchro->resume();
229 smx_activity_t ActorImpl::sleep(double duration)
232 throw_exception(std::make_exception_ptr(simgrid::HostFailureException(
233 XBT_THROW_POINT, std::string("Host ") + std::string(host_->get_cname()) + " failed, you cannot sleep there.")));
235 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
236 synchro->host = host_;
237 synchro->surf_sleep = host_->pimpl_cpu->sleep(duration);
238 synchro->surf_sleep->set_data(synchro);
239 XBT_DEBUG("Create sleep synchronization %p", synchro);
244 void ActorImpl::throw_exception(std::exception_ptr e)
251 /* cancel the blocking synchro if any */
252 if (waiting_synchro) {
254 simgrid::kernel::activity::ExecImplPtr exec =
255 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(waiting_synchro);
256 if (exec != nullptr && exec->surf_action_)
257 exec->surf_action_->cancel();
259 simgrid::kernel::activity::CommImplPtr comm =
260 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(waiting_synchro);
261 if (comm != nullptr) {
266 simgrid::kernel::activity::SleepImplPtr sleep =
267 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(waiting_synchro);
268 if (sleep != nullptr) {
269 SIMIX_process_sleep_destroy(waiting_synchro);
270 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), this) ==
271 end(simix_global->process_to_run) &&
272 this != SIMIX_process_self()) {
273 XBT_DEBUG("Inserting %s in the to_run list", get_cname());
274 simix_global->process_to_run.push_back(this);
278 simgrid::kernel::activity::RawImplPtr raw =
279 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(waiting_synchro);
280 if (raw != nullptr) {
281 SIMIX_synchro_stop_waiting(this, &simcall);
284 simgrid::kernel::activity::IoImplPtr io =
285 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(waiting_synchro);
290 waiting_synchro = nullptr;
293 void create_maestro(simgrid::simix::ActorCode code)
295 /* Create maestro process and initialize it */
296 smx_actor_t maestro = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(""), /*host*/ nullptr);
299 maestro->context_ = SIMIX_context_new(simgrid::simix::ActorCode(), nullptr, maestro);
301 maestro->context_ = simix_global->context_factory->create_maestro(code, maestro);
304 maestro->simcall.issuer = maestro;
305 simix_global->maestro_process = maestro;
313 * @brief Internal function to create a process.
315 * This function actually creates the process.
316 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
317 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
319 * @return the process created
321 smx_actor_t SIMIX_process_create(std::string name, simgrid::simix::ActorCode code, void* data, simgrid::s4u::Host* host,
322 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
325 XBT_DEBUG("Start actor %s@'%s'", name.c_str(), host->get_cname());
327 if (host->is_off()) {
328 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name.c_str(), host->get_cname());
332 smx_actor_t process = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
334 xbt_assert(code && host != nullptr, "Invalid parameters");
336 process->set_user_data(data);
337 process->code = code;
339 if (parent_process != nullptr)
340 process->ppid_ = parent_process->pid_;
342 XBT_VERB("Create context %s", process->get_cname());
343 process->context_ = SIMIX_context_new(std::move(code), &SIMIX_process_cleanup, process);
346 if (properties != nullptr)
347 for (auto const& kv : *properties)
348 process->set_property(kv.first, kv.second);
350 /* Add the process to its host's process list */
351 host->pimpl_->process_list_.push_back(*process);
353 XBT_DEBUG("Start context '%s'", process->get_cname());
355 /* Now insert it in the global process list and in the process to run list */
356 simix_global->process_list[process->pid_] = process;
357 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
358 simix_global->process_to_run.push_back(process);
359 intrusive_ptr_add_ref(process);
361 /* The on_creation() signal must be delayed until there, where the pid and everything is set */
362 simgrid::s4u::Actor::on_creation(process->iface());
367 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
368 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
370 // This is mostly a copy/paste from SIMIX_process_new(),
371 // it'd be nice to share some code between those two functions.
373 sg_host_t host = sg_host_by_name(hostname);
374 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
376 if (host->is_off()) {
377 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
381 smx_actor_t actor = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
383 actor->set_user_data(data);
384 actor->code = nullptr;
386 if (parent_process != nullptr)
387 actor->ppid_ = parent_process->pid_;
389 XBT_VERB("Create context %s", actor->get_cname());
390 xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first");
391 actor->context_ = simix_global->context_factory->attach(&SIMIX_process_cleanup, actor);
394 if (properties != nullptr)
395 for (auto const& kv : *properties)
396 actor->set_property(kv.first, kv.second);
398 /* Add the process to it's host process list */
399 host->pimpl_->process_list_.push_back(*actor);
401 /* Now insert it in the global process list and in the process to run list */
402 simix_global->process_list[actor->pid_] = actor;
403 XBT_DEBUG("Inserting %s(%s) in the to_run list", actor->get_cname(), host->get_cname());
404 simix_global->process_to_run.push_back(actor);
405 intrusive_ptr_add_ref(actor);
407 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(actor->context_);
408 xbt_assert(nullptr != context, "Not a suitable context");
409 context->attach_start();
411 /* The on_creation() signal must be delayed until there, where the pid and everything is set */
412 simgrid::s4u::ActorPtr tmp = actor->iface(); // Passing this directly to on_creation will lead to crashes
413 simgrid::s4u::Actor::on_creation(tmp);
418 void SIMIX_process_detach()
420 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(simgrid::kernel::context::Context::self());
421 if (context == nullptr)
422 xbt_die("Not a suitable context");
424 SIMIX_process_cleanup(context->get_actor());
425 context->attach_stop();
429 * @brief Executes the processes from simix_global->process_to_run.
431 * The processes of simix_global->process_to_run are run (in parallel if
432 * possible). On exit, simix_global->process_to_run is empty, and
433 * simix_global->process_that_ran contains the list of processes that just ran.
434 * The two lists are swapped so, be careful when using them before and after a
435 * call to this function.
437 void SIMIX_process_runall()
439 SIMIX_context_runall();
441 simix_global->process_to_run.swap(simix_global->process_that_ran);
442 simix_global->process_to_run.clear();
446 * @brief Internal function to kill a SIMIX process.
448 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
449 * or directly for SIMIX internal purposes.
451 * @param actor poor victim
452 * @param issuer the actor which has sent the PROCESS_KILL. Important to not schedule twice the same actor.
454 void SIMIX_process_kill(smx_actor_t actor, smx_actor_t issuer)
457 if (actor->finished_) {
458 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", actor->get_cname(),
459 actor->host_->get_cname());
463 XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", issuer == nullptr ? "(null)" : issuer->get_cname(),
464 (issuer == nullptr || issuer->host_ == nullptr ? "(null)" : issuer->host_->get_cname()), actor->get_cname(),
465 actor->host_->get_cname());
467 actor->context_->iwannadie = true;
468 actor->blocked_ = false;
469 actor->suspended_ = false;
470 actor->exception = nullptr;
472 // Forcefully kill the actor if its host is turned off. Not an HostFailureException because you should not survive that
473 if (actor->host_->is_off())
474 actor->throw_exception(std::make_exception_ptr(simgrid::kernel::context::StopRequest("host failed")));
476 /* destroy the blocking synchro if any */
477 if (actor->waiting_synchro != nullptr) {
479 simgrid::kernel::activity::ExecImplPtr exec =
480 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(actor->waiting_synchro);
481 simgrid::kernel::activity::CommImplPtr comm =
482 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(actor->waiting_synchro);
483 simgrid::kernel::activity::SleepImplPtr sleep =
484 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(actor->waiting_synchro);
485 simgrid::kernel::activity::RawImplPtr raw =
486 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(actor->waiting_synchro);
487 simgrid::kernel::activity::IoImplPtr io =
488 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(actor->waiting_synchro);
490 if (exec != nullptr) {
491 if (exec->surf_action_) {
492 exec->surf_action_->cancel();
493 exec->surf_action_->unref();
494 exec->surf_action_ = nullptr;
496 } else if (comm != nullptr) {
497 actor->comms.remove(actor->waiting_synchro);
499 // Remove first occurrence of &process->simcall:
500 auto i = boost::range::find(actor->waiting_synchro->simcalls_, &actor->simcall);
501 if (i != actor->waiting_synchro->simcalls_.end())
502 actor->waiting_synchro->simcalls_.remove(&actor->simcall);
503 } else if (sleep != nullptr) {
504 if (sleep->surf_sleep)
505 sleep->surf_sleep->cancel();
507 } else if (raw != nullptr) {
508 SIMIX_synchro_stop_waiting(actor, &actor->simcall);
510 } else if (io != nullptr) {
513 simgrid::kernel::activity::ActivityImplPtr activity = actor->waiting_synchro;
514 xbt_die("Activity %s is of unknown type %s", activity->name_.c_str(),
515 simgrid::xbt::demangle(typeid(activity).name()).get());
518 actor->waiting_synchro = nullptr;
520 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), actor) ==
521 end(simix_global->process_to_run) &&
523 XBT_DEBUG("Inserting %s in the to_run list", actor->get_cname());
524 simix_global->process_to_run.push_back(actor);
528 /** @deprecated When this function gets removed, also remove the xbt_ex class, that is only there to help users to
530 void SIMIX_process_throw(smx_actor_t actor, xbt_errcat_t cat, int value, const char* msg)
532 SMX_EXCEPTION(actor, cat, value, msg);
534 if (actor->suspended_)
537 /* cancel the blocking synchro if any */
538 if (actor->waiting_synchro) {
540 simgrid::kernel::activity::ExecImplPtr exec =
541 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(actor->waiting_synchro);
542 if (exec != nullptr && exec->surf_action_)
543 exec->surf_action_->cancel();
545 simgrid::kernel::activity::CommImplPtr comm =
546 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(actor->waiting_synchro);
547 if (comm != nullptr) {
548 actor->comms.remove(comm);
552 simgrid::kernel::activity::SleepImplPtr sleep =
553 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(actor->waiting_synchro);
554 if (sleep != nullptr) {
555 SIMIX_process_sleep_destroy(actor->waiting_synchro);
556 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), actor) ==
557 end(simix_global->process_to_run) &&
558 actor != SIMIX_process_self()) {
559 XBT_DEBUG("Inserting %s in the to_run list", actor->get_cname());
560 simix_global->process_to_run.push_back(actor);
564 simgrid::kernel::activity::RawImplPtr raw =
565 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(actor->waiting_synchro);
566 if (raw != nullptr) {
567 SIMIX_synchro_stop_waiting(actor, &actor->simcall);
570 simgrid::kernel::activity::IoImplPtr io =
571 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(actor->waiting_synchro);
576 actor->waiting_synchro = nullptr;
580 * @brief Kills all running processes.
581 * @param issuer this one will not be killed
583 void SIMIX_process_killall(smx_actor_t issuer)
585 for (auto const& kv : simix_global->process_list)
586 if (kv.second != issuer)
587 SIMIX_process_kill(kv.second, issuer);
590 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
592 xbt_assert((actor != nullptr), "Invalid parameters");
593 simgrid::xbt::intrusive_erase(actor->host_->pimpl_->process_list_, *actor);
595 dest->pimpl_->process_list_.push_back(*actor);
598 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t actor)
600 smx_activity_t sync_suspend = actor->suspend(simcall->issuer);
602 if (actor != simcall->issuer) {
603 SIMIX_simcall_answer(simcall);
605 sync_suspend->simcalls_.push_back(simcall);
606 actor->waiting_synchro = sync_suspend;
607 actor->waiting_synchro->suspend();
609 /* If we are suspending ourselves, then just do not finish the simcall now */
612 int SIMIX_process_get_maxpid() {
613 return simix_process_maxpid;
616 int SIMIX_process_count()
618 return simix_global->process_list.size();
621 void* SIMIX_process_self_get_data()
623 smx_actor_t self = SIMIX_process_self();
625 if (self == nullptr) {
628 return self->get_user_data();
631 void SIMIX_process_self_set_data(void *data)
633 SIMIX_process_self()->set_user_data(data);
637 /* needs to be public and without simcall because it is called
638 by exceptions and logging events */
639 const char* SIMIX_process_self_get_name() {
641 smx_actor_t process = SIMIX_process_self();
642 if (process == nullptr || process == simix_global->maestro_process)
645 return process->get_cname();
648 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
650 if (process->finished_) {
651 // The joined process is already finished, just wake up the issuer process right away
652 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
653 SIMIX_simcall_answer(simcall);
656 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
657 sync->simcalls_.push_back(simcall);
658 simcall->issuer->waiting_synchro = sync;
661 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
663 smx_activity_t res = issuer->sleep(timeout);
664 intrusive_ptr_add_ref(res.get());
665 SIMIX_process_on_exit(process,
667 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
668 if (sleep->surf_sleep)
669 sleep->surf_sleep->finish(simgrid::kernel::resource::Action::State::FINISHED);
670 intrusive_ptr_release(sleep);
676 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
678 if (MC_is_active() || MC_record_replay_is_active()) {
679 MC_process_clock_add(simcall->issuer, duration);
680 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
681 SIMIX_simcall_answer(simcall);
684 smx_activity_t sync = simcall->issuer->sleep(duration);
685 sync->simcalls_.push_back(simcall);
686 simcall->issuer->waiting_synchro = sync;
689 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
691 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
692 simgrid::kernel::activity::SleepImplPtr sleep =
693 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
695 if (sleep->surf_sleep) {
696 sleep->surf_sleep->unref();
697 sleep->surf_sleep = nullptr;
702 * @brief Calling this function makes the process to yield.
704 * Only the current process can call this function, giving back the control to maestro.
706 * @param self the current process
708 void SIMIX_process_yield(smx_actor_t self)
710 XBT_DEBUG("Yield actor '%s'", self->get_cname());
712 /* Go into sleep and return control to maestro */
713 self->context_->suspend();
715 /* Ok, maestro returned control to us */
716 XBT_DEBUG("Control returned to me: '%s'", self->get_cname());
718 if (self->context_->iwannadie) {
720 XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host_->get_cname());
721 // throw simgrid::kernel::context::StopRequest(); Does not seem to properly kill the actor
722 self->context_->stop();
726 if (self->suspended_) {
727 XBT_DEBUG("Hey! I'm suspended.");
728 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
729 self->suspended_ = false;
733 if (self->exception != nullptr) {
734 XBT_DEBUG("Wait, maestro left me an exception");
735 std::exception_ptr exception = std::move(self->exception);
736 self->exception = nullptr;
737 std::rethrow_exception(std::move(exception));
740 if (SMPI_switch_data_segment && not self->finished_) {
741 SMPI_switch_data_segment(self->iface());
745 /** @brief Returns the list of processes to run. */
746 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
748 return simix_global->process_to_run;
751 /** @brief Returns the process from PID. */
752 smx_actor_t SIMIX_process_from_PID(aid_t PID)
754 auto actor = simix_global->process_list.find(PID);
755 return actor == simix_global->process_list.end() ? nullptr : actor->second;
758 void SIMIX_process_on_exit_runall(smx_actor_t actor)
760 simgrid::s4u::Actor::on_destruction(actor->iface());
761 smx_process_exit_status_t exit_status = (actor->context_->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
762 while (not actor->on_exit.empty()) {
763 s_smx_process_exit_fun_t exit_fun = actor->on_exit.back();
764 actor->on_exit.pop_back();
765 (exit_fun.fun)(exit_status, exit_fun.arg);
769 void SIMIX_process_on_exit(smx_actor_t actor, int_f_pvoid_pvoid_t fun, void* data)
771 SIMIX_process_on_exit(actor, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
774 void SIMIX_process_on_exit(smx_actor_t actor, std::function<void(int, void*)> fun, void* data)
776 xbt_assert(actor, "current process not found: are you in maestro context ?");
778 actor->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
781 /** @brief Restart a process, starting it again from the beginning. */
783 * @ingroup simix_process_management
784 * @brief Creates and runs a new SIMIX process.
786 * The structure and the corresponding thread are created and put in the list of ready processes.
788 * @param name a name for the process. It is for user-level information and can be nullptr.
789 * @param code the main function of the process
790 * @param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
792 * It can be retrieved with the method ActorImpl::getUserData().
793 * @param host where the new agent is executed.
794 * @param properties the properties of the process
796 smx_actor_t simcall_process_create(std::string name, simgrid::simix::ActorCode code, void* data, sg_host_t host,
797 std::unordered_map<std::string, std::string>* properties)
799 smx_actor_t self = SIMIX_process_self();
800 return simgrid::simix::simcall([name, code, data, host, properties, self] {
801 return SIMIX_process_create(name, std::move(code), data, host, properties, self);