From aa9cfa5743045b941f3afca368462ea2be992a5f Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 7 Jan 2019 11:34:57 +0100 Subject: [PATCH] Fix a race condition When saving the context of maestro, we must ensure that we are actually thread 0. Otherwise, we may end up saving the context of a minion (a working thread). --- src/kernel/context/ContextSwapped.cpp | 7 +++++++ src/kernel/context/ContextSwapped.hpp | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index cb2f31487d..87da1fcf61 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -134,9 +134,16 @@ SwappedContext::~SwappedContext() xbt_free(stack_); } +void SwappedContext::set_maestro(SwappedContext* ctx) +{ + if (factory_->threads_working_ == 0) // Don't save the soul of minions, only the one of maestro + factory_->workers_context_[0] = ctx; +} + void SwappedContext::stop() { Context::stop(); + /* We must cut the actor execution using an exception to properly free the C++ RAII variables */ throw StopRequest(); } diff --git a/src/kernel/context/ContextSwapped.hpp b/src/kernel/context/ContextSwapped.hpp index 41a174720c..606dd447ce 100644 --- a/src/kernel/context/ContextSwapped.hpp +++ b/src/kernel/context/ContextSwapped.hpp @@ -45,7 +45,7 @@ public: virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses - void set_maestro(SwappedContext* ctx) { factory_->workers_context_[0] = ctx; } + void set_maestro(SwappedContext* ctx); // FIXME: Killme static thread_local uintptr_t worker_id_; -- 2.20.1