From: Arnaud Giersch Date: Fri, 8 Mar 2019 22:06:26 +0000 (+0100) Subject: Make ActorImpl::context_ a std::unique_ptr. X-Git-Tag: v3_22~130 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/82567f27fa74ddc2b6afa8d9b682bdde15fe3f9c?hp=fa0bb7e4203ef24f51ea4f8d1a8abe376e4885d5 Make ActorImpl::context_ a std::unique_ptr. --- diff --git a/src/bindings/java/jmsg_process.cpp b/src/bindings/java/jmsg_process.cpp index 0cfc8057dd..26906a4f3c 100644 --- a/src/bindings/java/jmsg_process.cpp +++ b/src/bindings/java/jmsg_process.cpp @@ -26,7 +26,7 @@ jfieldID jprocess_field_Process_ppid; jobject jprocess_from_native(msg_process_t process) { simgrid::kernel::context::JavaContext* context = - (simgrid::kernel::context::JavaContext*)process->get_impl()->context_; + static_cast(process->get_impl()->context_.get()); return context->jprocess_; } diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index 0b59cd8237..ccc04209b2 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -171,7 +171,7 @@ void SwappedContextFactory::run_all() // - So, resume() is only launched from the parmap for the first job of each minion. parmap_->apply( [](smx_actor_t process) { - SwappedContext* context = static_cast(process->context_); + SwappedContext* context = static_cast(process->context_.get()); context->resume(); }, simix_global->actors_to_run); @@ -183,7 +183,7 @@ void SwappedContextFactory::run_all() smx_actor_t first_actor = simix_global->actors_to_run.front(); process_index_ = 1; /* execute the first actor; it will chain to the others when using suspend() */ - static_cast(first_actor->context_)->resume(); + static_cast(first_actor->context_.get())->resume(); } } @@ -230,7 +230,7 @@ void SwappedContext::suspend() if (next_work) { // There is a next soul to embody (ie, another executable actor) XBT_DEBUG("Run next process"); - next_context = static_cast(next_work.get()->context_); + next_context = static_cast(next_work.get()->context_.get()); } else { // All actors were run, go back to the parmap context XBT_DEBUG("No more actors to run"); @@ -252,7 +252,7 @@ void SwappedContext::suspend() if (i < simix_global->actors_to_run.size()) { /* Actually swap into the next actor directly without transiting to maestro */ XBT_DEBUG("Run next actor"); - next_context = static_cast(simix_global->actors_to_run[i]->context_); + next_context = static_cast(simix_global->actors_to_run[i]->context_.get()); } else { /* all processes were run, actually return to maestro */ XBT_DEBUG("No more actors to run"); diff --git a/src/kernel/context/ContextThread.cpp b/src/kernel/context/ContextThread.cpp index 7914f29e76..3e2b0668bd 100644 --- a/src/kernel/context/ContextThread.cpp +++ b/src/kernel/context/ContextThread.cpp @@ -158,7 +158,7 @@ void ThreadContext::suspend() void ThreadContext::attach_start() { // We're breaking the layers here by depending on the upper layer: - ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_; + ThreadContext* maestro = static_cast(simix_global->maestro_process->context_.get()); maestro->begin_.release(); xbt_assert(not this->is_maestro()); this->start(); @@ -169,7 +169,7 @@ void ThreadContext::attach_stop() xbt_assert(not this->is_maestro()); this->yield(); - ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_; + ThreadContext* maestro = static_cast(simix_global->maestro_process->context_.get()); maestro->end_.acquire(); Context::set_current(nullptr); @@ -181,7 +181,7 @@ void SerialThreadContext::run_all() { for (smx_actor_t const& actor : simix_global->actors_to_run) { XBT_DEBUG("Handling %p", actor); - ThreadContext* context = static_cast(actor->context_); + ThreadContext* context = static_cast(actor->context_.get()); context->release(); context->wait(); } @@ -205,9 +205,9 @@ void ParallelThreadContext::finalize() void ParallelThreadContext::run_all() { for (smx_actor_t const& actor : simix_global->actors_to_run) - static_cast(actor->context_)->release(); + static_cast(actor->context_.get())->release(); for (smx_actor_t const& actor : simix_global->actors_to_run) - static_cast(actor->context_)->wait(); + static_cast(actor->context_.get())->wait(); } void ParallelThreadContext::start_hook() diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index e4acc92281..9cdb6057f5 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -181,7 +181,7 @@ msg_process_t MSG_process_self() } smx_context_t MSG_process_get_smx_ctx(msg_process_t process) { // deprecated -- smx_context_t should die afterward - return process->get_impl()->context_; + return process->get_impl()->context_.get(); } /** @brief Add a function to the list of "on_exit" functions for the current process. * The on_exit functions are the functions executed when your process is killed. diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 17c34d2880..984e0ec9aa 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -58,10 +58,8 @@ ActorImpl::ActorImpl(simgrid::xbt::string name, s4u::Host* host) : host_(host), simcall.issuer = this; } -ActorImpl::~ActorImpl() -{ - delete this->context_; -} +ActorImpl::~ActorImpl() = default; + /* Become an actor in the simulation * * Currently this can only be called by the main thread (once) and only work with some thread factories @@ -90,7 +88,7 @@ ActorImplPtr ActorImpl::attach(std::string name, void* data, s4u::Host* host, 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); + actor->context_.reset(simix_global->context_factory->attach(actor)); /* Add properties */ if (properties != nullptr) @@ -106,7 +104,7 @@ ActorImplPtr ActorImpl::attach(std::string name, void* data, s4u::Host* host, simix_global->actors_to_run.push_back(actor); intrusive_ptr_add_ref(actor); - auto* context = dynamic_cast(actor->context_); + auto* context = dynamic_cast(actor->context_.get()); xbt_assert(nullptr != context, "Not a suitable context"); context->attach_start(); @@ -469,7 +467,7 @@ ActorImpl* ActorImpl::start(const simix::ActorCode& code) this->code = code; XBT_VERB("Create context %s", get_cname()); - context_ = simix_global->context_factory->create_context(simix::ActorCode(code), this); + context_.reset(simix_global->context_factory->create_context(simix::ActorCode(code), this)); XBT_DEBUG("Start context '%s'", get_cname()); @@ -514,9 +512,9 @@ void create_maestro(const std::function& code) ActorImpl* maestro = new ActorImpl(xbt::string(""), /*host*/ nullptr); if (not code) { - maestro->context_ = simix_global->context_factory->create_context(simix::ActorCode(), maestro); + maestro->context_.reset(simix_global->context_factory->create_context(simix::ActorCode(), maestro)); } else { - maestro->context_ = simix_global->context_factory->create_maestro(simix::ActorCode(code), maestro); + maestro->context_.reset(simix_global->context_factory->create_maestro(simix::ActorCode(code), maestro)); } maestro->simcall.issuer = maestro; diff --git a/src/simix/ActorImpl.hpp b/src/simix/ActorImpl.hpp index 18ee93888c..785e25b280 100644 --- a/src/simix/ActorImpl.hpp +++ b/src/simix/ActorImpl.hpp @@ -13,6 +13,7 @@ #include #include #include +#include struct s_smx_process_exit_fun_t { std::function fun; @@ -59,7 +60,7 @@ public: bool has_to_auto_restart() { return auto_restart_; } void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; } - context::Context* context_ = nullptr; /* the context (uctx/raw/thread) that executes the user function */ + std::unique_ptr context_; /* the context (uctx/raw/thread) that executes the user function */ std::exception_ptr exception_; bool finished_ = false; diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 9fe7ef1f54..2485979cdc 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -288,8 +288,6 @@ void SIMIX_clean() #endif /* Let's free maestro now */ - delete simix_global->maestro_process->context_; - simix_global->maestro_process->context_ = nullptr; delete simix_global->maestro_process; simix_global->maestro_process = nullptr;