From: Martin Quinson Date: Sat, 5 Jan 2019 14:33:22 +0000 (+0100) Subject: move ContextFactory::self() into Context::self() and tidy it up X-Git-Tag: v3_22~678 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/e64d6da8afc98eb8ea9838e48735e01e922863fc?hp=bd681bde4c1fcad799c5c7c7ee5b58ad345157ef move ContextFactory::self() into Context::self() and tidy it up --- diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp index 937f70745a..f42bcdea70 100644 --- a/src/bindings/java/JavaContext.cpp +++ b/src/bindings/java/JavaContext.cpp @@ -53,7 +53,7 @@ JavaContext::JavaContext(std::function code, void_pfn_smxprocess_t clean void JavaContext::start_hook() { - SIMIX_context_set_current(this); // We need to attach it also for maestro, in contrary to our ancestor + Context::set_current(this); // We need to attach it also for maestro, in contrary to our ancestor //Attach the thread to the JVM JNIEnv *env; diff --git a/src/include/xbt/parmap.hpp b/src/include/xbt/parmap.hpp index c5c79a6ee0..c58936483d 100644 --- a/src/include/xbt/parmap.hpp +++ b/src/include/xbt/parmap.hpp @@ -281,7 +281,7 @@ template void* Parmap::worker_main(void* arg) Parmap& parmap = data->parmap; unsigned round = 0; smx_context_t context = SIMIX_context_new(std::function(), nullptr, nullptr); - SIMIX_context_set_current(context); + kernel::context::Context::set_current(context); XBT_CDEBUG(xbt_parmap, "New worker thread created"); diff --git a/src/kernel/context/Context.cpp b/src/kernel/context/Context.cpp index 0aead0c913..8a725349e8 100644 --- a/src/kernel/context/Context.cpp +++ b/src/kernel/context/Context.cpp @@ -29,16 +29,20 @@ smx_context_t SIMIX_context_new( namespace simgrid { namespace kernel { namespace context { -static thread_local smx_context_t smx_current_context; ContextFactoryInitializer factory_initializer = nullptr; ContextFactory::~ContextFactory() = default; -Context* ContextFactory::self() +static thread_local smx_context_t smx_current_context; +Context* Context::self() { return smx_current_context; } +void Context::set_current(Context* self) +{ + smx_current_context = self; +} void Context::declare_context(std::size_t size) { @@ -70,7 +74,7 @@ Context::Context(std::function code, void_pfn_smxprocess_t cleanup_func, if (has_code()) this->cleanup_func_ = cleanup_func; else - SIMIX_context_set_current(this); + set_current(this); } Context::~Context() = default; @@ -100,16 +104,7 @@ void SIMIX_context_runall() smx_context_t SIMIX_context_self() { if (simix_global && simix_global->context_factory) - return simix_global->context_factory->self(); + return simgrid::kernel::context::Context::self(); else return nullptr; } - -/** - * @brief Sets the current context of this thread. - * @param context the context to set - */ -void SIMIX_context_set_current(smx_context_t context) -{ - simgrid::kernel::context::smx_current_context = context; -} diff --git a/src/kernel/context/Context.hpp b/src/kernel/context/Context.hpp index 0f4a2b0451..5219608220 100644 --- a/src/kernel/context/Context.hpp +++ b/src/kernel/context/Context.hpp @@ -34,8 +34,6 @@ public: virtual Context* create_maestro(std::function code, smx_actor_t process); virtual void run_all() = 0; - /** @brief Returns the current context of this thread. */ - Context* self(); std::string const& name() const { return name_; } protected: @@ -57,34 +55,41 @@ private: void declare_context(std::size_t size); public: - class StopRequest { - /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff - * - * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception. - * Otherwise, users may accidentally catch it with a try {} catch (std::exception) - */ - public: - StopRequest() = default; - explicit StopRequest(std::string msg) : msg_(msg) {} - - private: - std::string msg_; - }; bool iwannadie = false; Context(std::function code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process); Context(const Context&) = delete; Context& operator=(const Context&) = delete; + virtual ~Context(); void operator()() { code_(); } bool has_code() const { return static_cast(code_); } smx_actor_t process() { return this->actor_; } void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; } - // Virtual methods - virtual ~Context(); + // Scheduling methods virtual void stop(); virtual void suspend() = 0; + + // Retrieving the self() context + /** @brief Retrives the current context of this thread */ + static Context* self(); + /** @brief Sets the current context of this thread */ + static void set_current(Context* self); + + class StopRequest { + /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff + * + * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception. + * Otherwise, users may accidentally catch it with a try {} catch (std::exception) + */ + public: + StopRequest() = default; + explicit StopRequest(std::string msg) : msg_(msg) {} + + private: + std::string msg_; + }; }; class XBT_PUBLIC AttachContext : public Context { @@ -146,8 +151,6 @@ XBT_PUBLIC smx_context_t SIMIX_context_self(); // public because it's used in si XBT_PRIVATE void *SIMIX_context_stack_new(); XBT_PRIVATE void SIMIX_context_stack_delete(void *stack); -XBT_PUBLIC void SIMIX_context_set_current(smx_context_t context); - XBT_PUBLIC int SIMIX_process_get_maxpid(); XBT_PRIVATE void SIMIX_post_create_environment(); diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index e1fff9327a..c745dff377 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -157,13 +157,13 @@ void SerialBoostContext::suspend() XBT_DEBUG("No more process to run"); next_context = static_cast(BoostContext::get_maestro()); } - SIMIX_context_set_current(next_context); + Context::set_current(next_context); BoostContext::swap(this, next_context); } void SerialBoostContext::resume() { - SIMIX_context_set_current(this); + Context::set_current(this); BoostContext::swap(BoostContext::get_maestro(), this); } @@ -223,7 +223,7 @@ void ParallelBoostContext::suspend() next_context = workers_context_[worker_id_]; } - SIMIX_context_set_current(next_context); + Context::set_current(next_context); BoostContext::swap(this, next_context); } @@ -234,7 +234,7 @@ void ParallelBoostContext::resume() ParallelBoostContext* worker_context = static_cast(SIMIX_context_self()); workers_context_[worker_id_] = worker_context; - SIMIX_context_set_current(this); + Context::set_current(this); BoostContext::swap(worker_context, this); } diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index 302927489b..d98470bb5e 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -297,13 +297,13 @@ void SerialRawContext::suspend() XBT_DEBUG("No more process to run"); next_context = static_cast(RawContext::get_maestro()); } - SIMIX_context_set_current(next_context); + Context::set_current(next_context); RawContext::swap(this, next_context); } void SerialRawContext::resume() { - SIMIX_context_set_current(this); + Context::set_current(this); RawContext::swap(RawContext::get_maestro(), this); } @@ -367,7 +367,7 @@ void ParallelRawContext::suspend() XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id_, threads_working_.load()); } - SIMIX_context_set_current(next_context); + Context::set_current(next_context); RawContext::swap(this, next_context); } @@ -377,7 +377,7 @@ void ParallelRawContext::resume() ParallelRawContext* worker_context = static_cast(SIMIX_context_self()); workers_context_[worker_id_] = worker_context; XBT_DEBUG("Saving worker stack %zu", worker_id_); - SIMIX_context_set_current(this); + Context::set_current(this); RawContext::swap(worker_context, this); } diff --git a/src/kernel/context/ContextThread.cpp b/src/kernel/context/ContextThread.cpp index 01c084ed24..33df5e7307 100644 --- a/src/kernel/context/ContextThread.cpp +++ b/src/kernel/context/ContextThread.cpp @@ -76,7 +76,7 @@ ThreadContext::ThreadContext(std::function code, void_pfn_smxprocess_t c /* Otherwise, we attach to the current thread */ else { - SIMIX_context_set_current(this); + Context::set_current(this); } } @@ -89,7 +89,7 @@ ThreadContext::~ThreadContext() void *ThreadContext::wrapper(void *param) { ThreadContext* context = static_cast(param); - SIMIX_context_set_current(context); + Context::set_current(context); #ifndef WIN32 /* Install alternate signal stack, for SIGSEGV handler. */ @@ -176,7 +176,7 @@ void ThreadContext::attach_stop() ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_; maestro->end_.acquire(); - SIMIX_context_set_current(nullptr); + Context::set_current(nullptr); } // SerialThreadContext diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index e31ba6aedf..5dc49b5fc3 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -170,13 +170,13 @@ void SerialUContext::suspend() XBT_DEBUG("No more process to run"); next_context = static_cast(UContext::get_maestro()); } - SIMIX_context_set_current(next_context); + Context::set_current(next_context); UContext::swap(this, next_context); } void SerialUContext::resume() { - SIMIX_context_set_current(this); + Context::set_current(this); UContext::swap(UContext::get_maestro(), this); } @@ -255,7 +255,7 @@ void ParallelUContext::suspend() // When given that soul, the body will wait for the next scheduling round } - SIMIX_context_set_current(next_context); + Context::set_current(next_context); // Get the next soul to run, either simulated or initial minion's one: UContext::swap(this, next_context); } @@ -270,7 +270,7 @@ void ParallelUContext::resume() // 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 - SIMIX_context_set_current(this); + Context::set_current(this); // Actually do that using the relevant library call: UContext::swap(worker_context, this); // No body runs that soul anymore at this point. Instead the current body took the soul of simulated process The