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 "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 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 = SIMIX_context_self();
39 return (self_context != nullptr) ? self_context->process() : 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 process->finished_ = true;
60 SIMIX_process_on_exit_runall(process);
62 /* Unregister from the kill timer if any */
63 if (process->kill_timer != nullptr) {
64 SIMIX_timer_remove(process->kill_timer);
65 process->kill_timer = nullptr;
68 simix_global->mutex.lock();
70 /* cancel non-blocking communications */
71 while (not process->comms.empty()) {
72 smx_activity_t synchro = process->comms.front();
73 process->comms.pop_front();
74 simgrid::kernel::activity::CommImplPtr comm =
75 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
77 /* make sure no one will finish the comm after this process is destroyed,
78 * because src_proc or dst_proc would be an invalid pointer */
80 if (comm->src_proc == process) {
81 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
82 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
83 comm->src_proc = nullptr;
85 } else if (comm->dst_proc == process) {
86 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
87 comm->src_proc, comm->dst_proc);
88 comm->dst_proc = nullptr;
90 if (comm->detached && comm->src_proc != nullptr) {
91 /* the comm will be freed right now, remove it from the sender */
92 comm->src_proc->comms.remove(comm);
95 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
100 XBT_DEBUG("%s@%s(%ld) should not run anymore", process->get_cname(), process->iface()->get_host()->get_cname(),
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_->pimpl_->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 = false;
113 simix_global->mutex.unlock();
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(simgrid::xbt::string name, simgrid::s4u::Host* host) : name_(name), host_(host), piface_(this)
141 pid_ = simix_process_maxpid++;
142 simcall.issuer = this;
145 ActorImpl::~ActorImpl()
147 delete this->context_;
150 static void dying_daemon(int /*exit_status*/, void* data)
152 std::vector<ActorImpl*>* vect = &simix_global->daemons;
154 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
155 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
157 /* Don't move the whole content since we don't really care about the order */
158 std::swap(*it, vect->back());
162 /** This process will be terminated automatically when the last non-daemon process finishes */
163 void ActorImpl::daemonize()
167 simix_global->daemons.push_back(this);
168 SIMIX_process_on_exit(this, dying_daemon, this);
172 simgrid::s4u::Actor* ActorImpl::restart()
174 XBT_DEBUG("Restarting process %s on %s", get_cname(), host_->get_cname());
176 // retrieve the arguments of the old process
177 simgrid::kernel::actor::ProcessArg arg = ProcessArg(host_, this);
179 // kill the old process
180 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
182 // start the new process
184 SIMIX_process_create(arg.name, std::move(arg.code), arg.data, arg.host, arg.properties.get(), nullptr);
185 simcall_process_set_kill_time(actor, arg.kill_time);
186 actor->set_auto_restart(arg.auto_restart);
188 return actor->ciface();
191 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
194 XBT_DEBUG("Actor '%s' is already suspended", get_cname());
200 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
201 if (this != issuer) {
203 waiting_synchro->suspend();
204 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
208 return SIMIX_execution_start("suspend", "", 0.0, 1.0, 0.0, this->host_);
212 void ActorImpl::resume()
214 XBT_IN("process = %p", this);
216 if (context_->iwannadie) {
217 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
225 /* resume the synchronization that was blocking the resumed actor. */
227 waiting_synchro->resume();
232 smx_activity_t ActorImpl::sleep(double duration)
235 throw_exception(std::make_exception_ptr(simgrid::HostFailureException(
236 XBT_THROW_POINT, std::string("Host ") + std::string(host_->get_cname()) + " failed, you cannot sleep there.")));
238 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
239 synchro->host = host_;
240 synchro->surf_sleep = host_->pimpl_cpu->sleep(duration);
241 synchro->surf_sleep->set_data(synchro);
242 XBT_DEBUG("Create sleep synchronization %p", synchro);
247 void ActorImpl::throw_exception(std::exception_ptr e)
254 /* cancel the blocking synchro if any */
255 if (waiting_synchro) {
257 simgrid::kernel::activity::ExecImplPtr exec =
258 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(waiting_synchro);
259 if (exec != nullptr && exec->surf_action_)
260 exec->surf_action_->cancel();
262 simgrid::kernel::activity::CommImplPtr comm =
263 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(waiting_synchro);
264 if (comm != nullptr) {
269 simgrid::kernel::activity::SleepImplPtr sleep =
270 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(waiting_synchro);
271 if (sleep != nullptr) {
272 SIMIX_process_sleep_destroy(waiting_synchro);
273 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), this) ==
274 end(simix_global->process_to_run) &&
275 this != SIMIX_process_self()) {
276 XBT_DEBUG("Inserting %s in the to_run list", get_cname());
277 simix_global->process_to_run.push_back(this);
281 simgrid::kernel::activity::RawImplPtr raw =
282 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(waiting_synchro);
283 if (raw != nullptr) {
284 SIMIX_synchro_stop_waiting(this, &simcall);
287 simgrid::kernel::activity::IoImplPtr io =
288 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(waiting_synchro);
293 waiting_synchro = nullptr;
296 void create_maestro(simgrid::simix::ActorCode code)
298 /* Create maestro process and initialize it */
299 smx_actor_t maestro = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(""), /*host*/ nullptr);
302 maestro->context_ = SIMIX_context_new(simgrid::simix::ActorCode(), nullptr, maestro);
304 if (not simix_global)
305 xbt_die("simix is not initialized, please call MSG_init first");
306 maestro->context_ = simix_global->context_factory->create_maestro(code, maestro);
309 maestro->simcall.issuer = maestro;
310 simix_global->maestro_process = maestro;
317 /** @brief Creates and runs the maestro process */
318 void SIMIX_maestro_create(void (*code)(void*), void* data)
320 simgrid::kernel::actor::create_maestro(std::bind(code, data));
324 * @brief Internal function to create a process.
326 * This function actually creates the process.
327 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
328 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
330 * @return the process created
332 smx_actor_t SIMIX_process_create(std::string name, simgrid::simix::ActorCode code, void* data, simgrid::s4u::Host* host,
333 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
336 XBT_DEBUG("Start actor %s@'%s'", name.c_str(), host->get_cname());
338 if (host->is_off()) {
339 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name.c_str(), host->get_cname());
343 smx_actor_t process = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
345 xbt_assert(code && host != nullptr, "Invalid parameters");
347 process->set_user_data(data);
348 process->code = code;
350 if (parent_process != nullptr)
351 process->ppid_ = parent_process->pid_;
353 XBT_VERB("Create context %s", process->get_cname());
354 process->context_ = SIMIX_context_new(std::move(code), &SIMIX_process_cleanup, process);
357 if (properties != nullptr)
358 for (auto const& kv : *properties)
359 process->set_property(kv.first, kv.second);
361 /* Add the process to its host's process list */
362 host->pimpl_->process_list_.push_back(*process);
364 XBT_DEBUG("Start context '%s'", process->get_cname());
366 /* Now insert it in the global process list and in the process to run list */
367 simix_global->process_list[process->pid_] = process;
368 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
369 simix_global->process_to_run.push_back(process);
370 intrusive_ptr_add_ref(process);
372 /* The onCreation() signal must be delayed until there, where the pid and everything is set */
373 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
374 simgrid::s4u::Actor::on_creation(tmp);
379 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
380 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
382 // This is mostly a copy/paste from SIMIX_process_new(),
383 // it'd be nice to share some code between those two functions.
385 sg_host_t host = sg_host_by_name(hostname);
386 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
388 if (host->is_off()) {
389 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
393 smx_actor_t process = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
395 process->set_user_data(data);
396 process->code = nullptr;
398 if (parent_process != nullptr)
399 process->ppid_ = parent_process->pid_;
401 XBT_VERB("Create context %s", process->get_cname());
402 xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first");
403 process->context_ = simix_global->context_factory->attach(&SIMIX_process_cleanup, process);
406 if (properties != nullptr)
407 for (auto const& kv : *properties)
408 process->set_property(kv.first, kv.second);
410 /* Add the process to it's host process list */
411 host->pimpl_->process_list_.push_back(*process);
413 /* Now insert it in the global process list and in the process to run list */
414 simix_global->process_list[process->pid_] = process;
415 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
416 simix_global->process_to_run.push_back(process);
417 intrusive_ptr_add_ref(process);
419 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context_);
420 xbt_assert(nullptr != context, "Not a suitable context");
421 context->attach_start();
423 /* The on_creation() signal must be delayed until there, where the pid and everything is set */
424 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to on_creation will lead to crashes
425 simgrid::s4u::Actor::on_creation(tmp);
430 void SIMIX_process_detach()
432 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
434 xbt_die("Not a suitable context");
436 auto process = context->process();
437 SIMIX_process_cleanup(process);
438 context->attach_stop();
442 * @brief Executes the processes from simix_global->process_to_run.
444 * The processes of simix_global->process_to_run are run (in parallel if
445 * possible). On exit, simix_global->process_to_run is empty, and
446 * simix_global->process_that_ran contains the list of processes that just ran.
447 * The two lists are swapped so, be careful when using them before and after a
448 * call to this function.
450 void SIMIX_process_runall()
452 SIMIX_context_runall();
454 simix_global->process_to_run.swap(simix_global->process_that_ran);
455 simix_global->process_to_run.clear();
459 * @brief Internal function to kill a SIMIX process.
461 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
462 * or directly for SIMIX internal purposes.
464 * @param process poor victim
465 * @param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
467 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
469 if (process->finished_) {
470 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->get_cname(),
471 process->host_->get_cname());
475 XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", issuer->get_cname(),
476 (issuer->host_ == nullptr ? "(null)" : issuer->host_->get_cname()), process->get_cname(),
477 process->host_->get_cname());
479 process->context_->iwannadie = true;
480 process->blocked_ = false;
481 process->suspended_ = false;
482 process->exception = nullptr;
484 // Forcefully kill the actor if its host is turned off. Not an HostFailureException because you should not survive that
485 if (process->host_->is_off()) {
486 /* HORRIBLE HACK: Don't throw an StopRequest exception in Java, because it breaks sometimes.
488 * It seems to break for the actors started from the Java world, with new Process()
489 * while it works for the ones started from the C world, with the deployment file.
490 * When it happens, the simulation stops brutally with a message "untrapped exception StopRequest".
492 * From what I understand, it works for the native actors because they have a nice try/catch block around their main
493 * but I fail to have something like that for pure Java actors. That's probably a story of C->Java vs Java->C
494 * calling conventions. The right solution may be to have try/catch(StopRequest) blocks around each native call in
495 * JNI. ie, protect every Java->C++ call from C++ exceptions. But this sounds long and painful to do before we
496 * switch to an automatic generator such as SWIG. For now, we don't throw here that exception that we sometimes fail
499 * One of the unfortunate outcome is that the threads started from the deployment file are not stopped anymore.
500 * Or maybe this is the actors stopping gracefully as opposed to the killed ones? Or maybe this is absolutely all
501 * actors of the Java simulation? I'm not sure. Anyway. Because of them, the simulation hangs at the end, waiting
502 * for them to stop but they won't. The current answer to that is very brutal:
503 * we do a "exit(0)" to kill the JVM from the C code after the call to MSG_run(). Definitely unpleasant.
506 if (simgrid::kernel::context::factory_initializer == nullptr) // Only Java sets a factory_initializer, for now
507 process->throw_exception(std::make_exception_ptr(simgrid::kernel::context::Context::StopRequest("Host failed")));
510 /* destroy the blocking synchro if any */
511 if (process->waiting_synchro != nullptr) {
513 simgrid::kernel::activity::ExecImplPtr exec =
514 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
515 simgrid::kernel::activity::CommImplPtr comm =
516 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
517 simgrid::kernel::activity::SleepImplPtr sleep =
518 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
519 simgrid::kernel::activity::RawImplPtr raw =
520 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
521 simgrid::kernel::activity::IoImplPtr io =
522 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
524 if (exec != nullptr) {
525 if (exec->surf_action_) {
526 exec->surf_action_->cancel();
527 exec->surf_action_->unref();
528 exec->surf_action_ = nullptr;
530 } else if (comm != nullptr) {
531 process->comms.remove(process->waiting_synchro);
533 // Remove first occurrence of &process->simcall:
534 auto i = boost::range::find(process->waiting_synchro->simcalls_, &process->simcall);
535 if (i != process->waiting_synchro->simcalls_.end())
536 process->waiting_synchro->simcalls_.remove(&process->simcall);
537 } else if (sleep != nullptr) {
538 if (sleep->surf_sleep)
539 sleep->surf_sleep->cancel();
541 } else if (raw != nullptr) {
542 SIMIX_synchro_stop_waiting(process, &process->simcall);
544 } else if (io != nullptr) {
547 simgrid::kernel::activity::ActivityImplPtr activity = process->waiting_synchro;
548 xbt_die("Activity %s is of unknown type %s", activity->name_.c_str(),
549 simgrid::xbt::demangle(typeid(activity).name()).get());
552 process->waiting_synchro = nullptr;
554 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
555 end(simix_global->process_to_run) &&
557 XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
558 simix_global->process_to_run.push_back(process);
562 /** @deprecated When this function gets removed, also remove the xbt_ex class, that is only there to help users to
564 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
565 SMX_EXCEPTION(process, cat, value, msg);
567 if (process->suspended_)
570 /* cancel the blocking synchro if any */
571 if (process->waiting_synchro) {
573 simgrid::kernel::activity::ExecImplPtr exec =
574 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
575 if (exec != nullptr && exec->surf_action_)
576 exec->surf_action_->cancel();
578 simgrid::kernel::activity::CommImplPtr comm =
579 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
580 if (comm != nullptr) {
581 process->comms.remove(comm);
585 simgrid::kernel::activity::SleepImplPtr sleep =
586 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
587 if (sleep != nullptr) {
588 SIMIX_process_sleep_destroy(process->waiting_synchro);
589 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
590 end(simix_global->process_to_run) &&
591 process != SIMIX_process_self()) {
592 XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
593 simix_global->process_to_run.push_back(process);
597 simgrid::kernel::activity::RawImplPtr raw =
598 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
599 if (raw != nullptr) {
600 SIMIX_synchro_stop_waiting(process, &process->simcall);
603 simgrid::kernel::activity::IoImplPtr io =
604 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
609 process->waiting_synchro = nullptr;
614 * @brief Kills all running processes.
615 * @param issuer this one will not be killed
617 void SIMIX_process_killall(smx_actor_t issuer)
619 for (auto const& kv : simix_global->process_list)
620 if (kv.second != issuer)
621 SIMIX_process_kill(kv.second, issuer);
624 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
626 xbt_assert((actor != nullptr), "Invalid parameters");
627 simgrid::xbt::intrusive_erase(actor->host_->pimpl_->process_list_, *actor);
629 dest->pimpl_->process_list_.push_back(*actor);
632 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
634 smx_activity_t sync_suspend = process->suspend(simcall->issuer);
636 if (process != simcall->issuer) {
637 SIMIX_simcall_answer(simcall);
639 sync_suspend->simcalls_.push_back(simcall);
640 process->waiting_synchro = sync_suspend;
641 process->waiting_synchro->suspend();
643 /* If we are suspending ourselves, then just do not finish the simcall now */
646 int SIMIX_process_get_maxpid() {
647 return simix_process_maxpid;
650 int SIMIX_process_count()
652 return simix_global->process_list.size();
655 void* SIMIX_process_self_get_data()
657 smx_actor_t self = SIMIX_process_self();
662 return self->get_user_data();
665 void SIMIX_process_self_set_data(void *data)
667 SIMIX_process_self()->set_user_data(data);
671 /* needs to be public and without simcall because it is called
672 by exceptions and logging events */
673 const char* SIMIX_process_self_get_name() {
675 smx_actor_t process = SIMIX_process_self();
676 if (process == nullptr || process == simix_global->maestro_process)
679 return process->get_cname();
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,
701 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
702 if (sleep->surf_sleep)
703 sleep->surf_sleep->finish(simgrid::kernel::resource::Action::State::FINISHED);
704 intrusive_ptr_release(sleep);
710 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
712 if (MC_is_active() || MC_record_replay_is_active()) {
713 MC_process_clock_add(simcall->issuer, duration);
714 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
715 SIMIX_simcall_answer(simcall);
718 smx_activity_t sync = simcall->issuer->sleep(duration);
719 sync->simcalls_.push_back(simcall);
720 simcall->issuer->waiting_synchro = sync;
723 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
725 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
726 simgrid::kernel::activity::SleepImplPtr sleep =
727 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
729 if (sleep->surf_sleep) {
730 sleep->surf_sleep->unref();
731 sleep->surf_sleep = nullptr;
736 * @brief Calling this function makes the process to yield.
738 * Only the current process can call this function, giving back the control to maestro.
740 * @param self the current process
742 void SIMIX_process_yield(smx_actor_t self)
744 XBT_DEBUG("Yield actor '%s'", self->get_cname());
746 /* Go into sleep and return control to maestro */
747 self->context_->suspend();
749 /* Ok, maestro returned control to us */
750 XBT_DEBUG("Control returned to me: '%s'", self->get_cname());
752 if (self->context_->iwannadie) {
753 XBT_DEBUG("I wanna die!");
754 self->finished_ = true;
755 /* execute the on_exit functions */
756 SIMIX_process_on_exit_runall(self);
758 if (self->auto_restart_ && self->host_->is_off() &&
759 watched_hosts.find(self->host_->get_cname()) == watched_hosts.end()) {
760 XBT_DEBUG("Push host %s to watched_hosts because it's off and %s needs to restart", self->host_->get_cname(),
762 watched_hosts.insert(self->host_->get_cname());
765 XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host_->get_cname());
766 self->context_->stop();
767 xbt_backtrace_display_current();
768 xbt_die("I should be dead by now.");
771 if (self->suspended_) {
772 XBT_DEBUG("Hey! I'm suspended.");
773 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
774 self->suspended_ = false;
778 if (self->exception != nullptr) {
779 XBT_DEBUG("Wait, maestro left me an exception");
780 std::exception_ptr exception = std::move(self->exception);
781 self->exception = nullptr;
782 std::rethrow_exception(std::move(exception));
785 if (SMPI_switch_data_segment && not self->finished_) {
786 SMPI_switch_data_segment(self->iface());
790 /** @brief Returns the list of processes to run. */
791 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
793 return simix_global->process_to_run;
796 /** @brief Returns the process from PID. */
797 smx_actor_t SIMIX_process_from_PID(aid_t PID)
799 auto process = simix_global->process_list.find(PID);
800 return process == simix_global->process_list.end() ? nullptr : process->second;
803 void SIMIX_process_on_exit_runall(smx_actor_t process) {
804 simgrid::s4u::Actor::on_destruction(process->iface());
805 smx_process_exit_status_t exit_status = (process->context_->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
806 while (not process->on_exit.empty()) {
807 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
808 process->on_exit.pop_back();
809 (exit_fun.fun)(exit_status, exit_fun.arg);
813 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void* data)
815 SIMIX_process_on_exit(process, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
818 void SIMIX_process_on_exit(smx_actor_t process, std::function<void(int, void*)> fun, void* data)
820 xbt_assert(process, "current process not found: are you in maestro context ?");
822 process->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
825 /** @brief Restart a process, starting it again from the beginning. */
827 * @ingroup simix_process_management
828 * @brief Creates and runs a new SIMIX process.
830 * The structure and the corresponding thread are created and put in the list of ready processes.
832 * @param name a name for the process. It is for user-level information and can be nullptr.
833 * @param code the main function of the process
834 * @param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
836 * It can be retrieved with the method ActorImpl::getUserData().
837 * @param host where the new agent is executed.
838 * @param properties the properties of the process
840 smx_actor_t simcall_process_create(std::string name, simgrid::simix::ActorCode code, void* data, sg_host_t host,
841 std::unordered_map<std::string, std::string>* properties)
843 smx_actor_t self = SIMIX_process_self();
844 return simgrid::simix::simcall([name, code, data, host, properties, self] {
845 return SIMIX_process_create(name, std::move(code), data, host, properties, self);