X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/7415b6acc6794b420f8884de0f73fcb3e020d9c0..08e94eb0482589e4b287cbea301b84daf52635bd:/src/simix/ActorImpl.cpp diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index e31c9c5e07..2a664f42e5 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -62,6 +62,75 @@ ActorImpl::~ActorImpl() { delete this->context_; } +/* Become an actor in the simulation + * + * Currently this can only be called by the main thread (once) and only work with some thread factories + * (currently ThreadContextFactory). + * + * In the future, it might be extended in order to attach other threads created by a third party library. + */ + +ActorImplPtr ActorImpl::attach(std::string name, void* data, s4u::Host* host, + std::unordered_map* properties) +{ + // This is mostly a copy/paste from create(), it'd be nice to share some code between those two functions. + + XBT_DEBUG("Attach process %s on host '%s'", name.c_str(), host->get_cname()); + + if (not host->is_on()) { + XBT_WARN("Cannot launch process '%s' on failed host '%s'", name.c_str(), host->get_cname()); + std::rethrow_exception( + std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Cannot attach actor on failed host."))); + } + + ActorImpl* actor = new ActorImpl(xbt::string(name), host); + /* Actor data */ + actor->set_user_data(data); + actor->code = nullptr; + + XBT_VERB("Create context %s", actor->get_cname()); + xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first"); + actor->context_ = simix_global->context_factory->attach(actor); + + /* Add properties */ + if (properties != nullptr) + for (auto const& kv : *properties) + actor->set_property(kv.first, kv.second); + + /* Add the process to it's host process list */ + host->pimpl_->process_list_.push_back(*actor); + + /* Now insert it in the global process list and in the process to run list */ + simix_global->process_list[actor->get_pid()] = actor; + XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), host->get_cname()); + simix_global->actors_to_run.push_back(actor); + intrusive_ptr_add_ref(actor); + + auto* context = dynamic_cast(actor->context_); + xbt_assert(nullptr != context, "Not a suitable context"); + context->attach_start(); + + /* The on_creation() signal must be delayed until there, where the pid and everything is set */ + simgrid::s4u::ActorPtr tmp = actor->iface(); // Passing this directly to on_creation will lead to crashes + simgrid::s4u::Actor::on_creation(tmp); + + return ActorImplPtr(actor); +} +/** @brief Detach an actor attached with `attach()` + * + * This is called when the current actor has finished its job. + * Used in the main thread, it waits for the simulation to finish before returning. When it returns, the other + * simulated actors and the maestro are destroyed. + */ +void ActorImpl::detach() +{ + auto* context = dynamic_cast(context::Context::self()); + if (context == nullptr) + xbt_die("Not a suitable context"); + + context->get_actor()->cleanup(); + context->attach_stop(); +} void ActorImpl::cleanup() { @@ -95,7 +164,7 @@ void ActorImpl::exit() // Forcefully kill the actor if its host is turned off. Not a HostFailureException because you should not survive that if (not host_->is_on()) - this->throw_exception(std::make_exception_ptr(simgrid::kernel::context::StopRequest("host failed"))); + this->throw_exception(std::make_exception_ptr(ForcefulKillException("host failed"))); /* destroy the blocking synchro if any */ if (waiting_synchro != nullptr) { @@ -204,14 +273,15 @@ void ActorImpl::yield() if (context_->iwannadie) { XBT_DEBUG("Actor %s@%s is dead", get_cname(), host_->get_cname()); - // throw simgrid::kernel::context::StopRequest(); Does not seem to properly kill the actor + // throw simgrid::kernel::context::ForcefulKillException(); Does not seem to properly kill the actor context_->stop(); THROW_IMPOSSIBLE; } if (suspended_) { XBT_DEBUG("Hey! I'm suspended."); - xbt_assert(exception_ != nullptr, "Gasp! This exception may be lost by subsequent calls."); + + xbt_assert(exception_ == nullptr, "Gasp! This exception may be lost by subsequent calls."); suspended_ = false; suspend(this); } @@ -274,7 +344,7 @@ activity::ActivityImplPtr ActorImpl::suspend(ActorImpl* issuer) return nullptr; } else { - return activity::ExecImplPtr(new activity::ExecImpl("suspend", "", nullptr, this->host_))->start(0.0, 1.0, 0.0); + return activity::ExecImplPtr(new activity::ExecImpl("suspend", "", this->host_))->start(0.0, 1.0, 0.0); } } @@ -382,7 +452,8 @@ ActorImplPtr ActorImpl::create(std::string name, simix::ActorCode code, void* da if (not host->is_on()) { XBT_WARN("Cannot launch actor '%s' on failed host '%s'", name.c_str(), host->get_cname()); - return nullptr; + std::rethrow_exception( + std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Cannot create actor on failed host."))); } ActorImpl* actor = new ActorImpl(simgrid::xbt::string(name), host); @@ -439,68 +510,18 @@ void create_maestro(simix::ActorCode code) } // namespace kernel } -smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname, - std::unordered_map* properties, smx_actor_t parent_process) +void SIMIX_process_detach() { - // This is mostly a copy/paste from SIMIX_process_new(), - // it'd be nice to share some code between those two functions. - - sg_host_t host = sg_host_by_name(hostname); - XBT_DEBUG("Attach process %s on host '%s'", name, hostname); - - if (not host->is_on()) { - XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname); - return nullptr; - } - - smx_actor_t actor = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host); - /* Actor data */ - actor->set_user_data(data); - actor->code = nullptr; - - if (parent_process != nullptr) - actor->set_ppid(parent_process->get_pid()); - - XBT_VERB("Create context %s", actor->get_cname()); - xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first"); - actor->context_ = simix_global->context_factory->attach(actor); - - /* Add properties */ - if (properties != nullptr) - for (auto const& kv : *properties) - actor->set_property(kv.first, kv.second); - - /* Add the process to it's host process list */ - host->pimpl_->process_list_.push_back(*actor); - - /* Now insert it in the global process list and in the process to run list */ - simix_global->process_list[actor->get_pid()] = actor; - XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), host->get_cname()); - simix_global->actors_to_run.push_back(actor); - intrusive_ptr_add_ref(actor); - - auto* context = dynamic_cast(actor->context_); - xbt_assert(nullptr != context, "Not a suitable context"); - context->attach_start(); - - /* The on_creation() signal must be delayed until there, where the pid and everything is set */ - simgrid::s4u::ActorPtr tmp = actor->iface(); // Passing this directly to on_creation will lead to crashes - simgrid::s4u::Actor::on_creation(tmp); - - return actor; + simgrid::kernel::actor::ActorImpl::detach(); } -void SIMIX_process_detach() +smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname, + std::unordered_map* properties, + smx_actor_t /*parent_process*/) { - auto* context = dynamic_cast(simgrid::kernel::context::Context::self()); - if (context == nullptr) - xbt_die("Not a suitable context"); - - context->get_actor()->cleanup(); - context->attach_stop(); + return simgrid::kernel::actor::ActorImpl::attach(name, data, sg_host_by_name(hostname), properties).get(); } - /** @deprecated When this function gets removed, also remove the xbt_ex class, that is only there to help users to * transition */ void SIMIX_process_throw(smx_actor_t actor, xbt_errcat_t cat, int value, const char* msg) @@ -668,7 +689,7 @@ void SIMIX_process_on_exit(smx_actor_t actor, int_f_pvoid_pvoid_t fun, void* dat SIMIX_process_on_exit(actor, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data); } -void SIMIX_process_on_exit(smx_actor_t actor, std::function fun, void* data) +void SIMIX_process_on_exit(smx_actor_t actor, std::function fun, void* data) { xbt_assert(actor, "current process not found: are you in maestro context ?");