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"
18 #include "xbt/utility.hpp"
20 #include "simgrid/s4u/Host.hpp"
24 #include "smx_private.hpp"
25 #include "src/kernel/activity/SleepImpl.hpp"
26 #include "src/kernel/activity/SynchroIo.hpp"
27 #include "src/kernel/activity/SynchroRaw.hpp"
28 #include "src/mc/mc_replay.hpp"
29 #include "src/mc/remote/Client.hpp"
30 #include "src/msg/msg_private.hpp"
31 #include "src/surf/cpu_interface.hpp"
32 #include "src/surf/surf_interface.hpp"
35 #include "src/smpi/include/private.hpp"
38 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
40 unsigned long simix_process_maxpid = 0;
43 * \brief Returns the current agent.
45 * This functions returns the currently running SIMIX process.
47 * \return The SIMIX process
49 smx_actor_t SIMIX_process_self()
51 smx_context_t self_context = SIMIX_context_self();
53 return (self_context != nullptr) ? self_context->process() : nullptr;
57 * \brief Returns whether a process has pending asynchronous communications.
58 * \return true if there are asynchronous communications in this process
60 int SIMIX_process_has_pending_comms(smx_actor_t process) {
62 return process->comms.size() > 0;
66 * \brief Moves a process to the list of processes to destroy.
68 void SIMIX_process_cleanup(smx_actor_t process)
70 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->name.c_str(), process,
71 process->waiting_synchro.get());
73 process->finished = true;
74 SIMIX_process_on_exit_runall(process);
76 /* Unregister from the kill timer if any */
77 if (process->kill_timer != nullptr) {
78 SIMIX_timer_remove(process->kill_timer);
79 process->kill_timer = nullptr;
82 xbt_os_mutex_acquire(simix_global->mutex);
84 /* cancel non-blocking communications */
85 while (not process->comms.empty()) {
86 smx_activity_t synchro = process->comms.front();
87 process->comms.pop_front();
88 simgrid::kernel::activity::CommImplPtr comm =
89 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
91 /* make sure no one will finish the comm after this process is destroyed,
92 * because src_proc or dst_proc would be an invalid pointer */
94 if (comm->src_proc == process) {
95 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
96 comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
97 comm->src_proc = nullptr;
99 } else if (comm->dst_proc == process) {
100 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state,
101 comm->src_proc, comm->dst_proc);
102 comm->dst_proc = nullptr;
104 if (comm->detached && comm->src_proc != nullptr) {
105 /* the comm will be freed right now, remove it from the sender */
106 comm->src_proc->comms.remove(comm);
109 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
114 XBT_DEBUG("%p should not be run anymore",process);
115 simix_global->process_list.erase(process->pid);
116 if (process->host && process->host_process_list_hook.is_linked())
117 simgrid::xbt::intrusive_erase(process->host->extension<simgrid::simix::Host>()->process_list, *process);
118 if (not process->smx_destroy_list_hook.is_linked()) {
120 xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
122 simix_global->process_to_destroy.push_back(*process);
124 process->context->iwannadie = 0;
126 xbt_os_mutex_release(simix_global->mutex);
132 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
134 void SIMIX_process_empty_trash()
136 while (not simix_global->process_to_destroy.empty()) {
137 smx_actor_t process = &simix_global->process_to_destroy.front();
138 simix_global->process_to_destroy.pop_front();
139 XBT_DEBUG("Getting rid of %p",process);
140 intrusive_ptr_release(process);
143 xbt_dynar_reset(simix_global->dead_actors_vector);
150 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onCreation; // TODO cheinrich is this the right location here?
154 ActorImpl::~ActorImpl()
156 delete this->context;
159 static int dying_daemon(void* exit_status, void* data)
161 std::vector<ActorImpl*>* vect = &simix_global->daemons;
163 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
164 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
166 /* Don't move the whole content since we don't really care about the order */
167 std::swap(*it, vect->back());
172 /** This process will be terminated automatically when the last non-daemon process finishes */
173 void ActorImpl::daemonize()
177 simix_global->daemons.push_back(this);
178 SIMIX_process_on_exit(this, dying_daemon, this);
182 simgrid::s4u::Actor* ActorImpl::restart()
184 XBT_DEBUG("Restarting process %s on %s", getCname(), host->getCname());
186 // retrieve the arguments of the old process
187 // FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
188 simgrid::simix::ProcessArg arg;
192 arg.kill_time = SIMIX_timer_get_date(kill_timer);
194 arg.properties = nullptr;
195 arg.auto_restart = auto_restart;
197 // kill the old process
198 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
200 // start the new process
201 ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
202 arg.properties.get(), nullptr);
203 if (arg.kill_time >= 0)
204 simcall_process_set_kill_time(actor, arg.kill_time);
205 if (arg.auto_restart)
206 actor->auto_restart = arg.auto_restart;
208 return actor->ciface();
211 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
214 XBT_DEBUG("Actor '%s' is already suspended", name.c_str());
220 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
221 if (this != issuer) {
223 waiting_synchro->suspend();
224 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
228 return SIMIX_execution_start("suspend", 0.0, 1.0, 0.0, this->host);
232 void ActorImpl::resume()
234 XBT_IN("process = %p", this);
236 if (context->iwannadie) {
237 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
245 /* resume the synchronization that was blocking the resumed actor. */
247 waiting_synchro->resume();
252 smx_activity_t ActorImpl::sleep(double duration)
255 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->getCname());
257 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
258 synchro->host = host;
259 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
260 synchro->surf_sleep->setData(synchro);
261 XBT_DEBUG("Create sleep synchronization %p", synchro);
266 void create_maestro(std::function<void()> code)
268 smx_actor_t maestro = nullptr;
269 /* Create maestro process and initialize it */
270 maestro = new simgrid::simix::ActorImpl();
271 maestro->pid = simix_process_maxpid++;
273 maestro->userdata = nullptr;
276 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
278 if (not simix_global)
279 xbt_die("simix is not initialized, please call MSG_init first");
280 maestro->context = simix_global->context_factory->create_maestro(code, maestro);
283 maestro->simcall.issuer = maestro;
284 simix_global->maestro_process = maestro;
290 /** @brief Creates and runs the maestro process */
291 void SIMIX_maestro_create(void (*code)(void*), void* data)
293 simgrid::simix::create_maestro(std::bind(code, data));
297 * \brief Internal function to create a process.
299 * This function actually creates the process.
300 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
301 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
303 * \return the process created
305 smx_actor_t SIMIX_process_create(const char* name, std::function<void()> code, void* data, simgrid::s4u::Host* host,
306 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
309 XBT_DEBUG("Start process %s on host '%s'", name, host->getCname());
312 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->getCname());
316 smx_actor_t process = new simgrid::simix::ActorImpl();
317 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
318 simgrid::s4u::Actor::onCreation(tmp);
320 xbt_assert(code && host != nullptr, "Invalid parameters");
322 process->pid = simix_process_maxpid++;
323 process->name = simgrid::xbt::string(name);
324 process->host = host;
325 process->userdata = data;
326 process->simcall.issuer = process;
328 if (parent_process != nullptr) {
329 process->ppid = parent_process->pid;
330 /* SMPI process have their own data segment and each other inherit from their father */
332 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
333 if (parent_process->pid != 0) {
334 process->segment_index = parent_process->segment_index;
336 process->segment_index = process->pid - 1;
342 process->code = code;
344 XBT_VERB("Create context %s", process->name.c_str());
345 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
348 if (properties != nullptr)
349 for (auto const& kv : *properties)
350 process->setProperty(kv.first, kv.second);
352 /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
353 if (host->extension<simgrid::simix::Host>() == nullptr)
354 host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
356 /* Add the process to its host process list */
357 host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
359 XBT_DEBUG("Start context '%s'", process->name.c_str());
361 /* Now insert it in the global process list and in the process to run list */
362 simix_global->process_list[process->pid] = process;
363 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->getCname(), host->getCname());
364 simix_global->process_to_run.push_back(process);
365 intrusive_ptr_add_ref(process);
367 /* Tracing the process creation */
368 TRACE_msg_process_create(process->getName(), process->pid, process->host);
373 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
374 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
376 // This is mostly a copy/paste from SIMIX_process_new(),
377 // it'd be nice to share some code between those two functions.
379 sg_host_t host = sg_host_by_name(hostname);
380 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
383 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
387 smx_actor_t process = new simgrid::simix::ActorImpl();
389 process->pid = simix_process_maxpid++;
390 process->name = std::string(name);
391 process->host = host;
392 process->userdata = data;
393 process->simcall.issuer = process;
395 if (parent_process != nullptr) {
396 process->ppid = parent_process->pid;
397 /* SMPI process have their own data segment and each other inherit from their father */
399 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
400 if (parent_process->pid != 0) {
401 process->segment_index = parent_process->segment_index;
403 process->segment_index = process->pid - 1;
409 /* Process data for auto-restart */
410 process->code = nullptr;
412 XBT_VERB("Create context %s", process->name.c_str());
413 if (not simix_global)
414 xbt_die("simix is not initialized, please call MSG_init first");
415 process->context = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
418 if (properties != nullptr)
419 for (auto const& kv : *properties)
420 process->setProperty(kv.first, kv.second);
422 /* Add the process to it's host process list */
423 host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
425 /* Now insert it in the global process list and in the process to run list */
426 simix_global->process_list[process->pid] = process;
427 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->getCname(), host->getCname());
428 simix_global->process_to_run.push_back(process);
430 /* Tracing the process creation */
431 TRACE_msg_process_create(process->getName(), process->pid, process->host);
433 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
435 xbt_die("Not a suitable context");
437 context->attach_start();
441 void SIMIX_process_detach()
443 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
445 xbt_die("Not a suitable context");
447 auto process = context->process();
448 simix_global->cleanup_process_function(process);
449 context->attach_stop();
454 * \brief Executes the processes from simix_global->process_to_run.
456 * The processes of simix_global->process_to_run are run (in parallel if
457 * possible). On exit, simix_global->process_to_run is empty, and
458 * simix_global->process_that_ran contains the list of processes that just ran.
459 * The two lists are swapped so, be careful when using them before and after a
460 * call to this function.
462 void SIMIX_process_runall()
464 SIMIX_context_runall();
466 simix_global->process_to_run.swap(simix_global->process_that_ran);
467 simix_global->process_to_run.clear();
471 * \brief Internal function to kill a SIMIX process.
473 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
474 * or directly for SIMIX internal purposes.
476 * \param process poor victim
477 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
479 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
481 if (process->finished) {
482 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->getCname(),
483 process->host->getCname());
487 XBT_DEBUG("Killing process %s@%s", process->getCname(), process->host->getCname());
489 process->context->iwannadie = 1;
490 process->blocked = 0;
491 process->suspended = 0;
492 process->exception = nullptr;
494 /* destroy the blocking synchro if any */
495 if (process->waiting_synchro != nullptr) {
497 simgrid::kernel::activity::ExecImplPtr exec =
498 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
499 simgrid::kernel::activity::CommImplPtr comm =
500 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
501 simgrid::kernel::activity::SleepImplPtr sleep =
502 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
503 simgrid::kernel::activity::RawImplPtr raw =
504 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
505 simgrid::kernel::activity::IoImplPtr io =
506 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
508 if (exec != nullptr) {
509 if (exec->surfAction_) {
510 exec->surfAction_->cancel();
511 exec->surfAction_->unref();
512 exec->surfAction_ = nullptr;
514 } else if (comm != nullptr) {
515 process->comms.remove(process->waiting_synchro);
517 // Remove first occurrence of &process->simcall:
518 auto i = boost::range::find(process->waiting_synchro->simcalls, &process->simcall);
519 if (i != process->waiting_synchro->simcalls.end())
520 process->waiting_synchro->simcalls.remove(&process->simcall);
521 } else if (sleep != nullptr) {
522 SIMIX_process_sleep_destroy(process->waiting_synchro);
524 } else if (raw != nullptr) {
525 SIMIX_synchro_stop_waiting(process, &process->simcall);
527 } else if (io != nullptr) {
528 SIMIX_io_destroy(process->waiting_synchro);
530 xbt_die("Unknown type of activity");
533 process->waiting_synchro = nullptr;
535 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
536 end(simix_global->process_to_run) &&
538 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
539 simix_global->process_to_run.push_back(process);
543 /** @brief Ask another process to raise the given exception
545 * @param process The process that should raise that exception
546 * @param cat category of exception
547 * @param value value associated to the exception
548 * @param msg string information associated to the exception
550 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
551 SMX_EXCEPTION(process, cat, value, msg);
553 if (process->suspended)
556 /* cancel the blocking synchro if any */
557 if (process->waiting_synchro) {
559 simgrid::kernel::activity::ExecImplPtr exec =
560 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
561 if (exec != nullptr && exec->surfAction_)
562 exec->surfAction_->cancel();
564 simgrid::kernel::activity::CommImplPtr comm =
565 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
566 if (comm != nullptr) {
567 process->comms.remove(comm);
571 simgrid::kernel::activity::SleepImplPtr sleep =
572 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
573 if (sleep != nullptr) {
574 SIMIX_process_sleep_destroy(process->waiting_synchro);
575 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
576 end(simix_global->process_to_run) &&
577 process != SIMIX_process_self()) {
578 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
579 simix_global->process_to_run.push_back(process);
583 simgrid::kernel::activity::RawImplPtr raw =
584 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
585 if (raw != nullptr) {
586 SIMIX_synchro_stop_waiting(process, &process->simcall);
589 simgrid::kernel::activity::IoImplPtr io =
590 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
592 SIMIX_io_destroy(process->waiting_synchro);
595 process->waiting_synchro = nullptr;
599 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
600 SIMIX_process_killall(simcall->issuer, reset_pid);
603 * \brief Kills all running processes.
604 * \param issuer this one will not be killed
606 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
608 for (auto const& kv : simix_global->process_list)
609 if (kv.second != issuer)
610 SIMIX_process_kill(kv.second, issuer);
613 simix_process_maxpid = reset_pid;
616 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
618 xbt_assert((actor != nullptr), "Invalid parameters");
619 simgrid::xbt::intrusive_erase(actor->host->extension<simgrid::simix::Host>()->process_list, *actor);
621 dest->extension<simgrid::simix::Host>()->process_list.push_back(*actor);
624 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
626 smx_activity_t sync_suspend = process->suspend(simcall->issuer);
628 if (process != simcall->issuer) {
629 SIMIX_simcall_answer(simcall);
631 sync_suspend->simcalls.push_back(simcall);
632 process->waiting_synchro = sync_suspend;
633 process->waiting_synchro->suspend();
635 /* If we are suspending ourselves, then just do not finish the simcall now */
638 int SIMIX_process_get_maxpid() {
639 return simix_process_maxpid;
642 int SIMIX_process_count()
644 return simix_global->process_list.size();
647 void* SIMIX_process_self_get_data()
649 smx_actor_t self = SIMIX_process_self();
654 return self->getUserData();
657 void SIMIX_process_self_set_data(void *data)
659 SIMIX_process_self()->setUserData(data);
663 /* needs to be public and without simcall because it is called
664 by exceptions and logging events */
665 const char* SIMIX_process_self_get_name() {
667 smx_actor_t process = SIMIX_process_self();
668 if (process == nullptr || process == simix_global->maestro_process)
671 return process->name.c_str();
674 smx_actor_t SIMIX_process_get_by_name(const char* name)
676 for (auto const& kv : simix_global->process_list)
677 if (kv.second->name == name)
682 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
684 if (process->finished) {
685 // The joined process is already finished, just wake up the issuer process right away
686 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
687 SIMIX_simcall_answer(simcall);
690 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
691 sync->simcalls.push_back(simcall);
692 simcall->issuer->waiting_synchro = sync;
695 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
697 smx_activity_t res = issuer->sleep(timeout);
698 intrusive_ptr_add_ref(res.get());
699 SIMIX_process_on_exit(process,
700 [](void*, void* arg) {
701 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
702 if (sleep->surf_sleep)
703 sleep->surf_sleep->finish(simgrid::surf::Action::State::done);
704 intrusive_ptr_release(sleep);
711 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
713 if (MC_is_active() || MC_record_replay_is_active()) {
714 MC_process_clock_add(simcall->issuer, duration);
715 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
716 SIMIX_simcall_answer(simcall);
719 smx_activity_t sync = simcall->issuer->sleep(duration);
720 sync->simcalls.push_back(simcall);
721 simcall->issuer->waiting_synchro = sync;
724 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
726 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
727 simgrid::kernel::activity::SleepImplPtr sleep =
728 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
730 if (sleep->surf_sleep) {
731 sleep->surf_sleep->unref();
732 sleep->surf_sleep = nullptr;
737 * \brief Calling this function makes the process to yield.
739 * Only the current process can call this function, giving back the control to maestro.
741 * \param self the current process
743 void SIMIX_process_yield(smx_actor_t self)
745 XBT_DEBUG("Yield actor '%s'", self->getCname());
747 /* Go into sleep and return control to maestro */
748 self->context->suspend();
750 /* Ok, maestro returned control to us */
751 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
753 if (self->context->iwannadie){
754 XBT_DEBUG("I wanna die!");
755 self->finished = true;
756 /* execute the on_exit functions */
757 SIMIX_process_on_exit_runall(self);
758 /* Add the process to the list of process to restart, only if the host is down */
759 if (self->auto_restart && self->host->isOff()) {
760 SIMIX_host_add_auto_restart_process(self->host, self->getCname(), self->code, self->userdata,
761 SIMIX_timer_get_date(self->kill_timer), self->getProperties(),
764 XBT_DEBUG("Process %s@%s is dead", self->getCname(), self->host->getCname());
765 self->context->stop();
768 if (self->suspended) {
769 XBT_DEBUG("Hey! I'm suspended.");
770 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
775 if (self->exception != nullptr) {
776 XBT_DEBUG("Wait, maestro left me an exception");
777 std::exception_ptr exception = std::move(self->exception);
778 self->exception = nullptr;
779 std::rethrow_exception(std::move(exception));
782 if(SMPI_switch_data_segment && self->segment_index != -1){
783 SMPI_switch_data_segment(self->segment_index);
787 /* callback: termination */
788 void SIMIX_process_exception_terminate(xbt_ex_t * e)
794 /** @brief Returns the list of processes to run. */
795 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
797 return simix_global->process_to_run;
800 /** @brief Returns the process from PID. */
801 smx_actor_t SIMIX_process_from_PID(aid_t PID)
803 auto process = simix_global->process_list.find(PID);
804 return process == simix_global->process_list.end() ? nullptr : process->second;
807 void SIMIX_process_on_exit_runall(smx_actor_t process) {
808 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
809 while (not process->on_exit.empty()) {
810 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
811 process->on_exit.pop_back();
812 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
816 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
817 xbt_assert(process, "current process not found: are you in maestro context ?");
819 s_smx_process_exit_fun_t exit_fun = {fun, data};
821 process->on_exit.push_back(exit_fun);
825 * \brief Sets the auto-restart status of the process.
826 * If set to 1, the process will be automatically restarted when its host comes back.
828 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
829 process->auto_restart = auto_restart;
832 /** @brief Restart a process, starting it again from the beginning. */
834 * \ingroup simix_process_management
835 * \brief Creates and runs a new SIMIX process.
837 * The structure and the corresponding thread are created and put in the list of ready processes.
839 * \param name a name for the process. It is for user-level information and can be nullptr.
840 * \param code the main function of the process
841 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
843 * It can be retrieved with the function \ref simcall_process_get_data.
844 * \param host where the new agent is executed.
845 * \param kill_time time when the process is killed
846 * \param argc first argument passed to \a code
847 * \param argv second argument passed to \a code
848 * \param properties the properties of the process
849 * \param auto_restart either it is autorestarting or not.
851 extern "C" smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host,
852 int argc, char** argv, std::map<std::string, std::string>* properties)
856 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
857 for (int i = 0; i != argc; ++i)
860 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
864 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
865 std::map<std::string, std::string>* properties)
869 smx_actor_t self = SIMIX_process_self();
870 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
871 return SIMIX_process_create(name, std::move(code), data, host, properties, self);