1 /* Copyright (c) 2007-2018. 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 "smx_private.hpp"
8 #include "src/kernel/activity/IoImpl.hpp"
9 #include "src/kernel/activity/SleepImpl.hpp"
10 #include "src/kernel/activity/SynchroRaw.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/mc/remote/Client.hpp"
13 #include "src/simix/smx_host_private.hpp"
14 #include "src/simix/smx_io_private.hpp"
15 #include "src/simix/smx_synchro_private.hpp"
16 #include "src/surf/cpu_interface.hpp"
20 #include "src/smpi/include/private.hpp"
23 #include <boost/range/algorithm.hpp>
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
27 unsigned long simix_process_maxpid = 0;
30 * \brief Returns the current agent.
32 * This functions returns the currently running SIMIX process.
34 * \return The SIMIX process
36 smx_actor_t SIMIX_process_self()
38 smx_context_t self_context = SIMIX_context_self();
40 return (self_context != nullptr) ? self_context->process() : nullptr;
44 * \brief Returns whether a process has pending asynchronous communications.
45 * \return true if there are asynchronous communications in this process
47 int SIMIX_process_has_pending_comms(smx_actor_t process) {
49 return process->comms.size() > 0;
53 * \brief Moves a process to the list of processes to destroy.
55 void SIMIX_process_cleanup(smx_actor_t process)
57 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->name.c_str(), process,
58 process->waiting_synchro.get());
60 process->finished = true;
61 SIMIX_process_on_exit_runall(process);
63 /* Unregister from the kill timer if any */
64 if (process->kill_timer != nullptr) {
65 SIMIX_timer_remove(process->kill_timer);
66 process->kill_timer = nullptr;
69 xbt_os_mutex_acquire(simix_global->mutex);
71 /* cancel non-blocking communications */
72 while (not process->comms.empty()) {
73 smx_activity_t synchro = process->comms.front();
74 process->comms.pop_front();
75 simgrid::kernel::activity::CommImplPtr comm =
76 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
78 /* make sure no one will finish the comm after this process is destroyed,
79 * because src_proc or dst_proc would be an invalid pointer */
81 if (comm->src_proc == process) {
82 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
83 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
84 comm->src_proc = nullptr;
86 } else if (comm->dst_proc == process) {
87 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
88 comm->src_proc, comm->dst_proc);
89 comm->dst_proc = nullptr;
91 if (comm->detached && comm->src_proc != nullptr) {
92 /* the comm will be freed right now, remove it from the sender */
93 comm->src_proc->comms.remove(comm);
96 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
101 XBT_DEBUG("%p should not be run anymore",process);
102 simix_global->process_list.erase(process->pid);
103 if (process->host && process->host_process_list_hook.is_linked())
104 simgrid::xbt::intrusive_erase(process->host->extension<simgrid::simix::Host>()->process_list, *process);
105 if (not process->smx_destroy_list_hook.is_linked()) {
107 xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
109 simix_global->process_to_destroy.push_back(*process);
111 process->context->iwannadie = 0;
113 xbt_os_mutex_release(simix_global->mutex);
119 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
121 void SIMIX_process_empty_trash()
123 while (not simix_global->process_to_destroy.empty()) {
124 smx_actor_t process = &simix_global->process_to_destroy.front();
125 simix_global->process_to_destroy.pop_front();
126 XBT_DEBUG("Getting rid of %p",process);
127 intrusive_ptr_release(process);
130 xbt_dynar_reset(simix_global->dead_actors_vector);
139 ActorImpl::~ActorImpl()
141 delete this->context;
144 static void dying_daemon(int /*exit_status*/, void* data)
146 std::vector<ActorImpl*>* vect = &simix_global->daemons;
148 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
149 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
151 /* Don't move the whole content since we don't really care about the order */
152 std::swap(*it, vect->back());
156 /** This process will be terminated automatically when the last non-daemon process finishes */
157 void ActorImpl::daemonize()
161 simix_global->daemons.push_back(this);
162 SIMIX_process_on_exit(this, dying_daemon, this);
166 simgrid::s4u::Actor* ActorImpl::restart()
168 XBT_DEBUG("Restarting process %s on %s", get_cname(), host->get_cname());
170 // retrieve the arguments of the old process
171 // FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
172 simgrid::kernel::actor::ProcessArg arg;
176 arg.kill_time = SIMIX_timer_get_date(kill_timer);
178 arg.properties = nullptr;
179 arg.auto_restart = auto_restart;
181 // kill the old process
182 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
184 // start the new process
185 ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
186 arg.properties.get(), nullptr);
187 if (arg.kill_time >= 0)
188 simcall_process_set_kill_time(actor, arg.kill_time);
189 if (arg.auto_restart)
190 actor->auto_restart = arg.auto_restart;
192 return actor->ciface();
195 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
198 XBT_DEBUG("Actor '%s' is already suspended", name.c_str());
204 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
205 if (this != issuer) {
207 waiting_synchro->suspend();
208 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
212 return SIMIX_execution_start("suspend", 0.0, 1.0, 0.0, this->host);
216 void ActorImpl::resume()
218 XBT_IN("process = %p", this);
220 if (context->iwannadie) {
221 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
229 /* resume the synchronization that was blocking the resumed actor. */
231 waiting_synchro->resume();
236 smx_activity_t ActorImpl::sleep(double duration)
239 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->get_cname());
241 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
242 synchro->host = host;
243 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
244 synchro->surf_sleep->set_data(synchro);
245 XBT_DEBUG("Create sleep synchronization %p", synchro);
250 void create_maestro(std::function<void()> code)
252 smx_actor_t maestro = nullptr;
253 /* Create maestro process and initialize it */
254 maestro = new simgrid::kernel::actor::ActorImpl();
255 maestro->pid = simix_process_maxpid++;
257 maestro->setUserData(nullptr);
260 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
262 if (not simix_global)
263 xbt_die("simix is not initialized, please call MSG_init first");
264 maestro->context = simix_global->context_factory->create_maestro(code, maestro);
267 maestro->simcall.issuer = maestro;
268 simix_global->maestro_process = maestro;
275 /** @brief Creates and runs the maestro process */
276 void SIMIX_maestro_create(void (*code)(void*), void* data)
278 simgrid::kernel::actor::create_maestro(std::bind(code, data));
282 * \brief Internal function to create a process.
284 * This function actually creates the process.
285 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
286 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
288 * \return the process created
290 smx_actor_t SIMIX_process_create(const char* name, std::function<void()> code, void* data, simgrid::s4u::Host* host,
291 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
294 XBT_DEBUG("Start process %s on host '%s'", name, host->get_cname());
296 if (host->is_off()) {
297 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->get_cname());
301 smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
303 xbt_assert(code && host != nullptr, "Invalid parameters");
305 process->pid = simix_process_maxpid++;
306 process->name = simgrid::xbt::string(name);
307 process->host = host;
308 process->setUserData(data);
309 process->simcall.issuer = process;
311 if (parent_process != nullptr) {
312 process->ppid = parent_process->pid;
315 process->code = code;
317 XBT_VERB("Create context %s", process->name.c_str());
318 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
321 if (properties != nullptr)
322 for (auto const& kv : *properties)
323 process->set_property(kv.first, kv.second);
325 /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
326 if (host->extension<simgrid::simix::Host>() == nullptr)
327 host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
329 /* Add the process to its host's process list */
330 host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
332 XBT_DEBUG("Start context '%s'", process->name.c_str());
334 /* Now insert it in the global process list and in the process to run list */
335 simix_global->process_list[process->pid] = process;
336 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
337 simix_global->process_to_run.push_back(process);
338 intrusive_ptr_add_ref(process);
340 /* The onCreation() signal must be delayed until there, where the pid and everything is set */
341 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
342 simgrid::s4u::Actor::on_creation(tmp);
347 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
348 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
350 // This is mostly a copy/paste from SIMIX_process_new(),
351 // it'd be nice to share some code between those two functions.
353 sg_host_t host = sg_host_by_name(hostname);
354 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
356 if (host->is_off()) {
357 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
361 smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
363 process->pid = simix_process_maxpid++;
364 process->name = std::string(name);
365 process->host = host;
366 process->setUserData(data);
367 process->simcall.issuer = process;
369 if (parent_process != nullptr) {
370 process->ppid = parent_process->pid;
373 /* Process data for auto-restart */
374 process->code = nullptr;
376 XBT_VERB("Create context %s", process->name.c_str());
377 if (not simix_global)
378 xbt_die("simix is not initialized, please call MSG_init first");
379 process->context = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
382 if (properties != nullptr)
383 for (auto const& kv : *properties)
384 process->set_property(kv.first, kv.second);
386 /* Add the process to it's host process list */
387 host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
389 /* Now insert it in the global process list and in the process to run list */
390 simix_global->process_list[process->pid] = process;
391 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
392 simix_global->process_to_run.push_back(process);
393 intrusive_ptr_add_ref(process);
396 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
398 xbt_die("Not a suitable context");
400 context->attach_start();
402 /* The onCreation() signal must be delayed until there, where the pid and everything is set */
403 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
404 simgrid::s4u::Actor::on_creation(tmp);
409 void SIMIX_process_detach()
411 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
413 xbt_die("Not a suitable context");
415 auto process = context->process();
416 simix_global->cleanup_process_function(process);
417 context->attach_stop();
421 * \brief Executes the processes from simix_global->process_to_run.
423 * The processes of simix_global->process_to_run are run (in parallel if
424 * possible). On exit, simix_global->process_to_run is empty, and
425 * simix_global->process_that_ran contains the list of processes that just ran.
426 * The two lists are swapped so, be careful when using them before and after a
427 * call to this function.
429 void SIMIX_process_runall()
431 SIMIX_context_runall();
433 simix_global->process_to_run.swap(simix_global->process_that_ran);
434 simix_global->process_to_run.clear();
438 * \brief Internal function to kill a SIMIX process.
440 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
441 * or directly for SIMIX internal purposes.
443 * \param process poor victim
444 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
446 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
448 if (process->finished) {
449 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->get_cname(),
450 process->host->get_cname());
454 XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", issuer->get_cname(), issuer->host->get_cname(),
455 process->get_cname(), process->host->get_cname());
457 process->context->iwannadie = 1;
458 process->blocked = 0;
459 process->suspended = 0;
460 process->exception = nullptr;
462 /* destroy the blocking synchro if any */
463 if (process->waiting_synchro != nullptr) {
465 simgrid::kernel::activity::ExecImplPtr exec =
466 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
467 simgrid::kernel::activity::CommImplPtr comm =
468 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
469 simgrid::kernel::activity::SleepImplPtr sleep =
470 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
471 simgrid::kernel::activity::RawImplPtr raw =
472 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
473 simgrid::kernel::activity::IoImplPtr io =
474 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
476 if (exec != nullptr) {
477 if (exec->surf_action_) {
478 exec->surf_action_->cancel();
479 exec->surf_action_->unref();
480 exec->surf_action_ = nullptr;
482 } else if (comm != nullptr) {
483 process->comms.remove(process->waiting_synchro);
485 // Remove first occurrence of &process->simcall:
486 auto i = boost::range::find(process->waiting_synchro->simcalls_, &process->simcall);
487 if (i != process->waiting_synchro->simcalls_.end())
488 process->waiting_synchro->simcalls_.remove(&process->simcall);
489 } else if (sleep != nullptr) {
490 SIMIX_process_sleep_destroy(process->waiting_synchro);
492 } else if (raw != nullptr) {
493 SIMIX_synchro_stop_waiting(process, &process->simcall);
495 } else if (io != nullptr) {
496 SIMIX_io_destroy(process->waiting_synchro);
498 xbt_die("Unknown type of activity");
501 process->waiting_synchro = nullptr;
503 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
504 end(simix_global->process_to_run) &&
506 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
507 simix_global->process_to_run.push_back(process);
511 /** @brief Ask another process to raise the given exception
513 * @param process The process that should raise that exception
514 * @param cat category of exception
515 * @param value value associated to the exception
516 * @param msg string information associated to the exception
518 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
519 SMX_EXCEPTION(process, cat, value, msg);
521 if (process->suspended)
524 /* cancel the blocking synchro if any */
525 if (process->waiting_synchro) {
527 simgrid::kernel::activity::ExecImplPtr exec =
528 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
529 if (exec != nullptr && exec->surf_action_)
530 exec->surf_action_->cancel();
532 simgrid::kernel::activity::CommImplPtr comm =
533 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
534 if (comm != nullptr) {
535 process->comms.remove(comm);
539 simgrid::kernel::activity::SleepImplPtr sleep =
540 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
541 if (sleep != nullptr) {
542 SIMIX_process_sleep_destroy(process->waiting_synchro);
543 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
544 end(simix_global->process_to_run) &&
545 process != SIMIX_process_self()) {
546 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
547 simix_global->process_to_run.push_back(process);
551 simgrid::kernel::activity::RawImplPtr raw =
552 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
553 if (raw != nullptr) {
554 SIMIX_synchro_stop_waiting(process, &process->simcall);
557 simgrid::kernel::activity::IoImplPtr io =
558 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
560 SIMIX_io_destroy(process->waiting_synchro);
563 process->waiting_synchro = nullptr;
568 * \brief Kills all running processes.
569 * \param issuer this one will not be killed
571 void SIMIX_process_killall(smx_actor_t issuer)
573 for (auto const& kv : simix_global->process_list)
574 if (kv.second != issuer)
575 SIMIX_process_kill(kv.second, issuer);
578 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
580 xbt_assert((actor != nullptr), "Invalid parameters");
581 simgrid::xbt::intrusive_erase(actor->host->extension<simgrid::simix::Host>()->process_list, *actor);
583 dest->extension<simgrid::simix::Host>()->process_list.push_back(*actor);
586 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
588 smx_activity_t sync_suspend = process->suspend(simcall->issuer);
590 if (process != simcall->issuer) {
591 SIMIX_simcall_answer(simcall);
593 sync_suspend->simcalls_.push_back(simcall);
594 process->waiting_synchro = sync_suspend;
595 process->waiting_synchro->suspend();
597 /* If we are suspending ourselves, then just do not finish the simcall now */
600 int SIMIX_process_get_maxpid() {
601 return simix_process_maxpid;
604 int SIMIX_process_count()
606 return simix_global->process_list.size();
609 void* SIMIX_process_self_get_data()
611 smx_actor_t self = SIMIX_process_self();
616 return self->getUserData();
619 void SIMIX_process_self_set_data(void *data)
621 SIMIX_process_self()->setUserData(data);
625 /* needs to be public and without simcall because it is called
626 by exceptions and logging events */
627 const char* SIMIX_process_self_get_name() {
629 smx_actor_t process = SIMIX_process_self();
630 if (process == nullptr || process == simix_global->maestro_process)
633 return process->name.c_str();
636 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
638 if (process->finished) {
639 // The joined process is already finished, just wake up the issuer process right away
640 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
641 SIMIX_simcall_answer(simcall);
644 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
645 sync->simcalls_.push_back(simcall);
646 simcall->issuer->waiting_synchro = sync;
649 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
651 smx_activity_t res = issuer->sleep(timeout);
652 intrusive_ptr_add_ref(res.get());
653 SIMIX_process_on_exit(process,
655 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
656 if (sleep->surf_sleep)
657 sleep->surf_sleep->finish(simgrid::kernel::resource::Action::State::FINISHED);
658 intrusive_ptr_release(sleep);
664 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
666 if (MC_is_active() || MC_record_replay_is_active()) {
667 MC_process_clock_add(simcall->issuer, duration);
668 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
669 SIMIX_simcall_answer(simcall);
672 smx_activity_t sync = simcall->issuer->sleep(duration);
673 sync->simcalls_.push_back(simcall);
674 simcall->issuer->waiting_synchro = sync;
677 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
679 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
680 simgrid::kernel::activity::SleepImplPtr sleep =
681 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
683 if (sleep->surf_sleep) {
684 sleep->surf_sleep->unref();
685 sleep->surf_sleep = nullptr;
690 * \brief Calling this function makes the process to yield.
692 * Only the current process can call this function, giving back the control to maestro.
694 * \param self the current process
696 void SIMIX_process_yield(smx_actor_t self)
698 XBT_DEBUG("Yield actor '%s'", self->get_cname());
700 /* Go into sleep and return control to maestro */
701 self->context->suspend();
703 /* Ok, maestro returned control to us */
704 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
706 if (self->context->iwannadie){
707 XBT_DEBUG("I wanna die!");
708 self->finished = true;
709 /* execute the on_exit functions */
710 SIMIX_process_on_exit_runall(self);
711 /* Add the process to the list of process to restart, only if the host is down */
712 if (self->auto_restart && self->host->is_off()) {
713 SIMIX_host_add_auto_restart_process(self->host, self->get_cname(), self->code, self->getUserData(),
714 SIMIX_timer_get_date(self->kill_timer), self->get_properties(),
717 XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host->get_cname());
718 self->context->stop();
721 if (self->suspended) {
722 XBT_DEBUG("Hey! I'm suspended.");
723 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
728 if (self->exception != nullptr) {
729 XBT_DEBUG("Wait, maestro left me an exception");
730 std::exception_ptr exception = std::move(self->exception);
731 self->exception = nullptr;
732 std::rethrow_exception(std::move(exception));
735 if (SMPI_switch_data_segment && not self->finished) {
736 SMPI_switch_data_segment(self->iface());
740 /** @brief Returns the list of processes to run. */
741 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
743 return simix_global->process_to_run;
746 /** @brief Returns the process from PID. */
747 smx_actor_t SIMIX_process_from_PID(aid_t PID)
749 auto process = simix_global->process_list.find(PID);
750 return process == simix_global->process_list.end() ? nullptr : process->second;
753 void SIMIX_process_on_exit_runall(smx_actor_t process) {
754 simgrid::s4u::Actor::on_destruction(process->iface());
755 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
756 while (not process->on_exit.empty()) {
757 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
758 process->on_exit.pop_back();
759 (exit_fun.fun)(exit_status, exit_fun.arg);
763 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void* data)
765 SIMIX_process_on_exit(process, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
768 void SIMIX_process_on_exit(smx_actor_t process, std::function<void(int, void*)> fun, void* data)
770 xbt_assert(process, "current process not found: are you in maestro context ?");
772 process->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
776 * \brief Sets the auto-restart status of the process.
777 * If set to 1, the process will be automatically restarted when its host comes back.
779 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
780 process->auto_restart = auto_restart;
783 /** @brief Restart a process, starting it again from the beginning. */
785 * \ingroup simix_process_management
786 * \brief Creates and runs a new SIMIX process.
788 * The structure and the corresponding thread are created and put in the list of ready processes.
790 * \param name a name for the process. It is for user-level information and can be nullptr.
791 * \param code the main function of the process
792 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
794 * It can be retrieved with the method ActorImpl::getUserData().
795 * \param host where the new agent is executed.
796 * \param argc first argument passed to \a code
797 * \param argv second argument passed to \a code
798 * \param properties the properties of the process
800 smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
801 char** argv, std::unordered_map<std::string, std::string>* properties)
805 auto wrapped_code = simgrid::xbt::wrap_main(code, argc, argv);
806 for (int i = 0; i != argc; ++i)
809 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
813 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
814 std::unordered_map<std::string, std::string>* properties)
818 smx_actor_t self = SIMIX_process_self();
819 return simgrid::simix::simcall([name, code, data, host, properties, self] {
820 return SIMIX_process_create(name, std::move(code), data, host, properties, self);