1 /* Copyright (c) 2007-2017. 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. */
12 #include <boost/range/algorithm.hpp>
15 #include "xbt/functional.hpp"
17 #include "xbt/sysdep.h"
19 #include "simgrid/s4u/Host.hpp"
23 #include "smx_private.hpp"
24 #include "src/kernel/activity/SleepImpl.hpp"
25 #include "src/kernel/activity/SynchroIo.hpp"
26 #include "src/kernel/activity/SynchroRaw.hpp"
27 #include "src/mc/mc_replay.h"
28 #include "src/mc/remote/Client.hpp"
29 #include "src/msg/msg_private.hpp"
30 #include "src/surf/cpu_interface.hpp"
31 #include "src/surf/surf_interface.hpp"
34 #include "src/smpi/include/private.hpp"
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
39 unsigned long simix_process_maxpid = 0;
42 * \brief Returns the current agent.
44 * This functions returns the currently running SIMIX process.
46 * \return The SIMIX process
48 smx_actor_t SIMIX_process_self()
50 smx_context_t self_context = SIMIX_context_self();
52 return (self_context != nullptr) ? self_context->process() : nullptr;
56 * \brief Returns whether a process has pending asynchronous communications.
57 * \return true if there are asynchronous communications in this process
59 int SIMIX_process_has_pending_comms(smx_actor_t process) {
61 return process->comms.size() > 0;
65 * \brief Moves a process to the list of processes to destroy.
67 void SIMIX_process_cleanup(smx_actor_t process)
69 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->name.c_str(), process,
70 process->waiting_synchro.get());
72 process->finished = true;
73 SIMIX_process_on_exit_runall(process);
75 /* Unregister from the kill timer if any */
76 if (process->kill_timer != nullptr) {
77 SIMIX_timer_remove(process->kill_timer);
78 process->kill_timer = nullptr;
81 xbt_os_mutex_acquire(simix_global->mutex);
83 /* cancel non-blocking communications */
84 while (not process->comms.empty()) {
85 smx_activity_t synchro = process->comms.front();
86 process->comms.pop_front();
87 simgrid::kernel::activity::CommImplPtr comm =
88 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
90 /* make sure no one will finish the comm after this process is destroyed,
91 * because src_proc or dst_proc would be an invalid pointer */
93 if (comm->src_proc == process) {
94 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
95 comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
96 comm->src_proc = nullptr;
98 } else if (comm->dst_proc == process) {
99 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state,
100 comm->src_proc, comm->dst_proc);
101 comm->dst_proc = nullptr;
103 if (comm->detached && comm->src_proc != nullptr) {
104 /* the comm will be freed right now, remove it from the sender */
105 comm->src_proc->comms.remove(comm);
108 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
113 XBT_DEBUG("%p should not be run anymore",process);
114 simix_global->process_list.erase(process->pid);
116 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
117 xbt_swag_insert(process, simix_global->process_to_destroy);
118 process->context->iwannadie = 0;
120 xbt_os_mutex_release(simix_global->mutex);
126 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
128 void SIMIX_process_empty_trash()
130 smx_actor_t process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
133 XBT_DEBUG("Getting rid of %p",process);
134 intrusive_ptr_release(process);
135 process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
142 ActorImpl::~ActorImpl()
144 delete this->context;
147 static int dying_daemon(void* 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());
160 /** This process will be terminated automatically when the last non-daemon process finishes */
161 void ActorImpl::daemonize()
165 simix_global->daemons.push_back(this);
166 SIMIX_process_on_exit(this, dying_daemon, this);
170 simgrid::s4u::Actor* ActorImpl::restart()
172 XBT_DEBUG("Restarting process %s on %s", getCname(), host->getCname());
174 // retrieve the arguments of the old process
175 // FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
176 simgrid::simix::ProcessArg arg;
180 arg.kill_time = SIMIX_timer_get_date(kill_timer);
182 arg.properties = nullptr;
183 arg.auto_restart = auto_restart;
185 // kill the old process
186 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
188 // start the new process
189 ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
190 arg.properties.get(), nullptr);
191 if (arg.kill_time >= 0)
192 simcall_process_set_kill_time(actor, arg.kill_time);
193 if (arg.auto_restart)
194 actor->auto_restart = arg.auto_restart;
196 return actor->ciface();
199 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
202 XBT_DEBUG("Actor '%s' is already suspended", name.c_str());
208 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
209 if (this != issuer) {
211 waiting_synchro->suspend();
212 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
216 return SIMIX_execution_start(this, "suspend", 0.0, 1.0, 0.0);
220 void ActorImpl::resume()
222 XBT_IN("process = %p", this);
224 if (context->iwannadie) {
225 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
233 /* resume the synchronization that was blocking the resumed actor. */
235 waiting_synchro->resume();
240 smx_activity_t ActorImpl::sleep(double duration)
243 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->getCname());
245 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
246 synchro->host = host;
247 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
248 synchro->surf_sleep->setData(synchro);
249 XBT_DEBUG("Create sleep synchronization %p", synchro);
254 void create_maestro(std::function<void()> code)
256 smx_actor_t maestro = nullptr;
257 /* Create maestro process and initialize it */
258 maestro = new simgrid::simix::ActorImpl();
259 maestro->pid = simix_process_maxpid++;
261 maestro->userdata = nullptr;
264 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
266 if (not simix_global)
267 xbt_die("simix is not initialized, please call MSG_init first");
268 maestro->context = simix_global->context_factory->create_maestro(code, maestro);
271 maestro->simcall.issuer = maestro;
272 simix_global->maestro_process = maestro;
278 /** @brief Creates and runs the maestro process */
279 void SIMIX_maestro_create(void (*code)(void*), void* data)
281 simgrid::simix::create_maestro(std::bind(code, data));
285 * \brief Internal function to create a process.
287 * This function actually creates the process.
288 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
289 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
291 * \return the process created
293 smx_actor_t SIMIX_process_create(const char* name, std::function<void()> code, void* data, simgrid::s4u::Host* host,
294 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
297 XBT_DEBUG("Start process %s on host '%s'", name, host->getCname());
300 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->getCname());
304 smx_actor_t process = new simgrid::simix::ActorImpl();
306 xbt_assert(code && host != nullptr, "Invalid parameters");
308 process->pid = simix_process_maxpid++;
309 process->name = simgrid::xbt::string(name);
310 process->host = host;
311 process->userdata = data;
312 process->simcall.issuer = process;
314 if (parent_process != nullptr) {
315 process->ppid = parent_process->pid;
316 /* SMPI process have their own data segment and each other inherit from their father */
318 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
319 if (parent_process->pid != 0) {
320 process->segment_index = parent_process->segment_index;
322 process->segment_index = process->pid - 1;
328 process->code = code;
330 XBT_VERB("Create context %s", process->name.c_str());
331 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
334 if (properties != nullptr)
335 for (auto const& kv : *properties)
336 process->setProperty(kv.first, kv.second);
338 /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
339 if (host->extension<simgrid::simix::Host>() == nullptr)
340 host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
342 /* Add the process to its host process list */
343 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
345 XBT_DEBUG("Start context '%s'", process->name.c_str());
347 /* Now insert it in the global process list and in the process to run list */
348 simix_global->process_list[process->pid] = process;
349 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->getCname(), host->getCname());
350 simix_global->process_to_run.push_back(process);
351 intrusive_ptr_add_ref(process);
353 /* Tracing the process creation */
354 TRACE_msg_process_create(process->getCname(), process->pid, process->host);
359 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
360 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
362 // This is mostly a copy/paste from SIMIX_process_new(),
363 // it'd be nice to share some code between those two functions.
365 sg_host_t host = sg_host_by_name(hostname);
366 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
369 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
373 smx_actor_t process = new simgrid::simix::ActorImpl();
375 process->pid = simix_process_maxpid++;
376 process->name = std::string(name);
377 process->host = host;
378 process->userdata = data;
379 process->simcall.issuer = process;
381 if (parent_process != nullptr) {
382 process->ppid = parent_process->pid;
383 /* SMPI process have their own data segment and each other inherit from their father */
385 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
386 if (parent_process->pid != 0) {
387 process->segment_index = parent_process->segment_index;
389 process->segment_index = process->pid - 1;
395 /* Process data for auto-restart */
396 process->code = nullptr;
398 XBT_VERB("Create context %s", process->name.c_str());
399 if (not simix_global)
400 xbt_die("simix is not initialized, please call MSG_init first");
401 process->context = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
404 if (properties != nullptr)
405 for (auto const& kv : *properties)
406 process->setProperty(kv.first, kv.second);
408 /* Add the process to it's host process list */
409 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
411 /* Now insert it in the global process list and in the process to run list */
412 simix_global->process_list[process->pid] = process;
413 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->getCname(), host->getCname());
414 simix_global->process_to_run.push_back(process);
416 /* Tracing the process creation */
417 TRACE_msg_process_create(process->getCname(), process->pid, process->host);
419 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
421 xbt_die("Not a suitable context");
423 context->attach_start();
427 void SIMIX_process_detach()
429 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
431 xbt_die("Not a suitable context");
433 auto process = context->process();
434 simix_global->cleanup_process_function(process);
435 context->attach_stop();
440 * \brief Executes the processes from simix_global->process_to_run.
442 * The processes of simix_global->process_to_run are run (in parallel if
443 * possible). On exit, simix_global->process_to_run is empty, and
444 * simix_global->process_that_ran contains the list of processes that just ran.
445 * The two lists are swapped so, be careful when using them before and after a
446 * call to this function.
448 void SIMIX_process_runall()
450 SIMIX_context_runall();
452 simix_global->process_to_run.swap(simix_global->process_that_ran);
453 simix_global->process_to_run.clear();
457 * \brief Internal function to kill a SIMIX process.
459 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
460 * or directly for SIMIX internal purposes.
462 * \param process poor victim
463 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
465 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
467 XBT_DEBUG("Killing process %s@%s", process->getCname(), process->host->getCname());
469 process->context->iwannadie = 1;
470 process->blocked = 0;
471 process->suspended = 0;
472 process->exception = nullptr;
474 /* destroy the blocking synchro if any */
475 if (process->waiting_synchro != nullptr) {
477 simgrid::kernel::activity::ExecImplPtr exec =
478 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
479 simgrid::kernel::activity::CommImplPtr comm =
480 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
481 simgrid::kernel::activity::SleepImplPtr sleep =
482 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
483 simgrid::kernel::activity::RawImplPtr raw =
484 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
485 simgrid::kernel::activity::IoImplPtr io =
486 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
488 if (exec != nullptr) {
490 } else if (comm != nullptr) {
491 process->comms.remove(process->waiting_synchro);
493 // Remove first occurrence of &process->simcall:
494 auto i = boost::range::find(process->waiting_synchro->simcalls, &process->simcall);
495 if (i != process->waiting_synchro->simcalls.end())
496 process->waiting_synchro->simcalls.remove(&process->simcall);
497 } else if (sleep != nullptr) {
498 SIMIX_process_sleep_destroy(process->waiting_synchro);
500 } else if (raw != nullptr) {
501 SIMIX_synchro_stop_waiting(process, &process->simcall);
503 } else if (io != nullptr) {
504 SIMIX_io_destroy(process->waiting_synchro);
506 xbt_die("Unknown type of activity");
509 process->waiting_synchro = nullptr;
511 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
512 end(simix_global->process_to_run) &&
514 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
515 simix_global->process_to_run.push_back(process);
519 /** @brief Ask another process to raise the given exception
521 * @param process The process that should raise that exception
522 * @param cat category of exception
523 * @param value value associated to the exception
524 * @param msg string information associated to the exception
526 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
527 SMX_EXCEPTION(process, cat, value, msg);
529 if (process->suspended)
532 /* cancel the blocking synchro if any */
533 if (process->waiting_synchro) {
535 simgrid::kernel::activity::ExecImplPtr exec =
536 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
537 if (exec != nullptr && exec->surf_exec)
538 exec->surf_exec->cancel();
540 simgrid::kernel::activity::CommImplPtr comm =
541 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
542 if (comm != nullptr) {
543 process->comms.remove(comm);
547 simgrid::kernel::activity::SleepImplPtr sleep =
548 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
549 if (sleep != nullptr) {
550 SIMIX_process_sleep_destroy(process->waiting_synchro);
551 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
552 end(simix_global->process_to_run) &&
553 process != SIMIX_process_self()) {
554 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
555 simix_global->process_to_run.push_back(process);
559 simgrid::kernel::activity::RawImplPtr raw =
560 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
561 if (raw != nullptr) {
562 SIMIX_synchro_stop_waiting(process, &process->simcall);
565 simgrid::kernel::activity::IoImplPtr io =
566 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
568 SIMIX_io_destroy(process->waiting_synchro);
571 process->waiting_synchro = nullptr;
575 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
576 SIMIX_process_killall(simcall->issuer, reset_pid);
579 * \brief Kills all running processes.
580 * \param issuer this one will not be killed
582 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
584 for (auto const& kv : simix_global->process_list)
585 if (kv.second != issuer)
586 SIMIX_process_kill(kv.second, issuer);
589 simix_process_maxpid = reset_pid;
592 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
594 xbt_assert((process != nullptr), "Invalid parameters");
595 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
596 process->host = dest;
597 xbt_swag_insert(process, dest->extension<simgrid::simix::Host>()->process_list);
600 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
602 smx_activity_t sync_suspend = process->suspend(simcall->issuer);
604 if (process != simcall->issuer) {
605 SIMIX_simcall_answer(simcall);
607 sync_suspend->simcalls.push_back(simcall);
608 process->waiting_synchro = sync_suspend;
609 process->waiting_synchro->suspend();
611 /* If we are suspending ourselves, then just do not finish the simcall now */
614 int SIMIX_process_get_maxpid() {
615 return simix_process_maxpid;
618 int SIMIX_process_count()
620 return simix_global->process_list.size();
623 void* SIMIX_process_self_get_data()
625 smx_actor_t self = SIMIX_process_self();
630 return self->getUserData();
633 void SIMIX_process_self_set_data(void *data)
635 SIMIX_process_self()->setUserData(data);
639 /* needs to be public and without simcall because it is called
640 by exceptions and logging events */
641 const char* SIMIX_process_self_get_name() {
643 smx_actor_t process = SIMIX_process_self();
644 if (process == nullptr || process == simix_global->maestro_process)
647 return process->name.c_str();
650 smx_actor_t SIMIX_process_get_by_name(const char* name)
652 for (auto const& kv : simix_global->process_list)
653 if (kv.second->name == name)
658 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
660 if (process->finished) {
661 // The joined process is already finished, just wake up the issuer process right away
662 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
663 SIMIX_simcall_answer(simcall);
666 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
667 sync->simcalls.push_back(simcall);
668 simcall->issuer->waiting_synchro = sync;
671 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
673 smx_activity_t res = issuer->sleep(timeout);
674 intrusive_ptr_add_ref(res.get());
675 SIMIX_process_on_exit(process,
676 [](void*, void* arg) {
677 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
678 if (sleep->surf_sleep)
679 sleep->surf_sleep->finish(simgrid::surf::Action::State::done);
680 intrusive_ptr_release(sleep);
687 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
689 if (MC_is_active() || MC_record_replay_is_active()) {
690 MC_process_clock_add(simcall->issuer, duration);
691 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
692 SIMIX_simcall_answer(simcall);
695 smx_activity_t sync = simcall->issuer->sleep(duration);
696 sync->simcalls.push_back(simcall);
697 simcall->issuer->waiting_synchro = sync;
700 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
702 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
703 simgrid::kernel::activity::SleepImplPtr sleep =
704 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
706 if (sleep->surf_sleep) {
707 sleep->surf_sleep->unref();
708 sleep->surf_sleep = nullptr;
713 * \brief Calling this function makes the process to yield.
715 * Only the current process can call this function, giving back the control to maestro.
717 * \param self the current process
719 void SIMIX_process_yield(smx_actor_t self)
721 XBT_DEBUG("Yield actor '%s'", self->getCname());
723 /* Go into sleep and return control to maestro */
724 self->context->suspend();
726 /* Ok, maestro returned control to us */
727 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
729 if (self->new_host) {
730 SIMIX_process_change_host(self, self->new_host);
731 self->new_host = nullptr;
734 if (self->context->iwannadie){
735 XBT_DEBUG("I wanna die!");
736 self->finished = true;
737 /* execute the on_exit functions */
738 SIMIX_process_on_exit_runall(self);
739 /* Add the process to the list of process to restart, only if the host is down */
740 if (self->auto_restart && self->host->isOff()) {
741 SIMIX_host_add_auto_restart_process(self->host, self->getCname(), self->code, self->userdata,
742 SIMIX_timer_get_date(self->kill_timer), self->getProperties(),
745 XBT_DEBUG("Process %s@%s is dead", self->getCname(), self->host->getCname());
746 self->context->stop();
749 if (self->suspended) {
750 XBT_DEBUG("Hey! I'm suspended.");
751 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
756 if (self->exception != nullptr) {
757 XBT_DEBUG("Wait, maestro left me an exception");
758 std::exception_ptr exception = std::move(self->exception);
759 self->exception = nullptr;
760 std::rethrow_exception(std::move(exception));
763 if(SMPI_switch_data_segment && self->segment_index != -1){
764 SMPI_switch_data_segment(self->segment_index);
768 /* callback: termination */
769 void SIMIX_process_exception_terminate(xbt_ex_t * e)
775 /** @brief Returns the list of processes to run. */
776 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
778 return simix_global->process_to_run;
781 /** @brief Returns the process from PID. */
782 smx_actor_t SIMIX_process_from_PID(aid_t PID)
784 auto process = simix_global->process_list.find(PID);
785 return process == simix_global->process_list.end() ? nullptr : process->second;
788 void SIMIX_process_on_exit_runall(smx_actor_t process) {
789 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
790 while (not process->on_exit.empty()) {
791 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
792 process->on_exit.pop_back();
793 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
797 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
798 xbt_assert(process, "current process not found: are you in maestro context ?");
800 s_smx_process_exit_fun_t exit_fun = {fun, data};
802 process->on_exit.push_back(exit_fun);
806 * \brief Sets the auto-restart status of the process.
807 * If set to 1, the process will be automatically restarted when its host comes back.
809 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
810 process->auto_restart = auto_restart;
813 /** @brief Restart a process, starting it again from the beginning. */
815 * \ingroup simix_process_management
816 * \brief Creates and runs a new SIMIX process.
818 * The structure and the corresponding thread are created and put in the list of ready processes.
820 * \param name a name for the process. It is for user-level information and can be nullptr.
821 * \param code the main function of the process
822 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
824 * It can be retrieved with the function \ref simcall_process_get_data.
825 * \param host where the new agent is executed.
826 * \param kill_time time when the process is killed
827 * \param argc first argument passed to \a code
828 * \param argv second argument passed to \a code
829 * \param properties the properties of the process
830 * \param auto_restart either it is autorestarting or not.
832 extern "C" smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host,
833 int argc, char** argv, std::map<std::string, std::string>* properties)
837 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
838 for (int i = 0; i != argc; ++i)
841 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
845 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
846 std::map<std::string, std::string>* properties)
850 smx_actor_t self = SIMIX_process_self();
851 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
852 return SIMIX_process_create(name, std::move(code), data, host, properties, self);