From 7e42dd535dfc0d68de20fba4b9fc5e480b56c74b Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sat, 5 Jan 2019 17:10:34 +0100 Subject: [PATCH 1/1] make SIMIX_context_self() useless and use Context::self() instead --- src/bindings/java/jmsg.cpp | 8 +++++--- src/kernel/context/Context.cpp | 17 ++++++----------- src/kernel/context/Context.hpp | 2 -- src/kernel/context/ContextBoost.cpp | 2 +- src/kernel/context/ContextRaw.cpp | 2 +- src/kernel/context/ContextUnix.cpp | 2 +- src/s4u/s4u_Actor.cpp | 2 +- src/simix/ActorImpl.cpp | 4 ++-- 8 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/bindings/java/jmsg.cpp b/src/bindings/java/jmsg.cpp index 7987779dcd..75b71f8740 100644 --- a/src/bindings/java/jmsg.cpp +++ b/src/bindings/java/jmsg.cpp @@ -50,7 +50,7 @@ JavaVM *__java_vm = nullptr; JNIEnv *get_current_thread_env() { using simgrid::kernel::context::JavaContext; - JavaContext* ctx = static_cast(SIMIX_context_self()); + JavaContext* ctx = static_cast(simgrid::kernel::context::Context::self()); return ctx->jenv_; } @@ -269,7 +269,8 @@ static void run_jprocess(JNIEnv *env, jobject jprocess) static int java_main(int argc, char *argv[]) { JNIEnv *env = get_current_thread_env(); - simgrid::kernel::context::JavaContext* context = static_cast(SIMIX_context_self()); + simgrid::kernel::context::JavaContext* context = + static_cast(simgrid::kernel::context::Context::self()); //Change the "." in class name for "/". std::string arg0 = argv[0]; @@ -315,7 +316,8 @@ namespace context { void java_main_jprocess(jobject jprocess) { JNIEnv *env = get_current_thread_env(); - simgrid::kernel::context::JavaContext* context = static_cast(SIMIX_context_self()); + simgrid::kernel::context::JavaContext* context = + static_cast(simgrid::kernel::context::Context::self()); context->jprocess_ = jprocess; jprocess_bind(context->jprocess_, MSG_process_self(), env); diff --git a/src/kernel/context/Context.cpp b/src/kernel/context/Context.cpp index 8a725349e8..63321ebb8d 100644 --- a/src/kernel/context/Context.cpp +++ b/src/kernel/context/Context.cpp @@ -34,7 +34,7 @@ ContextFactoryInitializer factory_initializer = nullptr; ContextFactory::~ContextFactory() = default; -static thread_local smx_context_t smx_current_context; +static thread_local smx_context_t smx_current_context = nullptr; Context* Context::self() { return smx_current_context; @@ -77,7 +77,11 @@ Context::Context(std::function code, void_pfn_smxprocess_t cleanup_func, set_current(this); } -Context::~Context() = default; +Context::~Context() +{ + if (self() == this) + set_current(nullptr); +} void Context::stop() { @@ -99,12 +103,3 @@ void SIMIX_context_runall() { simix_global->context_factory->run_all(); } - -/** @brief returns the current running context */ -smx_context_t SIMIX_context_self() -{ - if (simix_global && simix_global->context_factory) - return simgrid::kernel::context::Context::self(); - else - return nullptr; -} diff --git a/src/kernel/context/Context.hpp b/src/kernel/context/Context.hpp index 5219608220..6271a8b704 100644 --- a/src/kernel/context/Context.hpp +++ b/src/kernel/context/Context.hpp @@ -145,8 +145,6 @@ XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ]; /** @brief Executes all the processes to run (in parallel if possible). */ XBT_PRIVATE void SIMIX_context_runall(); -/** @brief returns the current running context */ -XBT_PUBLIC smx_context_t SIMIX_context_self(); // public because it's used in simgrid-java XBT_PRIVATE void *SIMIX_context_stack_new(); XBT_PRIVATE void SIMIX_context_stack_delete(void *stack); diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index c745dff377..07bb785472 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -231,7 +231,7 @@ void ParallelBoostContext::resume() { worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed); - ParallelBoostContext* worker_context = static_cast(SIMIX_context_self()); + ParallelBoostContext* worker_context = static_cast(self()); workers_context_[worker_id_] = worker_context; Context::set_current(this); diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index d98470bb5e..a0c065fcfa 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -374,7 +374,7 @@ void ParallelRawContext::suspend() void ParallelRawContext::resume() { worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed); - ParallelRawContext* worker_context = static_cast(SIMIX_context_self()); + ParallelRawContext* worker_context = static_cast(self()); workers_context_[worker_id_] = worker_context; XBT_DEBUG("Saving worker stack %zu", worker_id_); Context::set_current(this); diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index 5dc49b5fc3..e2641444c2 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -266,7 +266,7 @@ void ParallelUContext::resume() // What is my containing body? Store its number in os-thread-specific area : worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed); // Get my current soul: - ParallelUContext* worker_context = static_cast(SIMIX_context_self()); + ParallelUContext* worker_context = static_cast(self()); // Write down that this soul is hosted in that body (for now) workers_context_[worker_id_] = worker_context; // Write in simix that I switched my soul diff --git a/src/s4u/s4u_Actor.cpp b/src/s4u/s4u_Actor.cpp index 83ea57183e..f693e258b1 100644 --- a/src/s4u/s4u_Actor.cpp +++ b/src/s4u/s4u_Actor.cpp @@ -31,7 +31,7 @@ simgrid::xbt::signal s4u::Actor::on_destruction; // ***** Actor creation ***** ActorPtr Actor::self() { - smx_context_t self_context = SIMIX_context_self(); + smx_context_t self_context = simgrid::kernel::context::Context::self(); if (self_context == nullptr) return simgrid::s4u::ActorPtr(); diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 4d494cda4f..f9bc84422e 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -34,7 +34,7 @@ unsigned long simix_process_maxpid = 0; */ smx_actor_t SIMIX_process_self() { - smx_context_t self_context = SIMIX_context_self(); + smx_context_t self_context = simgrid::kernel::context::Context::self(); return (self_context != nullptr) ? self_context->process() : nullptr; } @@ -429,7 +429,7 @@ smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostn void SIMIX_process_detach() { - auto* context = dynamic_cast(SIMIX_context_self()); + auto* context = dynamic_cast(simgrid::kernel::context::Context::self()); if (context == nullptr) xbt_die("Not a suitable context"); -- 2.20.1