From 5dd12fd4cbebeabea922611c6cb82281e9e03c97 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Sun, 29 Oct 2017 23:27:32 +0100 Subject: [PATCH 1/1] Use C++ atomic instead of compiler builtins. --- src/kernel/context/ContextBoost.cpp | 4 ++-- src/kernel/context/ContextBoost.hpp | 3 ++- src/kernel/context/ContextRaw.cpp | 6 +++--- src/kernel/context/ContextRaw.hpp | 3 ++- src/kernel/context/ContextUnix.cpp | 4 ++-- src/kernel/context/ContextUnix.hpp | 3 ++- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index a9422e0d0c..1d26b050db 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -202,7 +202,7 @@ void SerialBoostContext::run_all() #if HAVE_THREAD_CONTEXTS simgrid::xbt::Parmap* ParallelBoostContext::parmap_; -uintptr_t ParallelBoostContext::threads_working_; +std::atomic ParallelBoostContext::threads_working_; xbt_os_thread_key_t ParallelBoostContext::worker_id_key_; std::vector ParallelBoostContext::workers_context_; @@ -254,7 +254,7 @@ void ParallelBoostContext::suspend() void ParallelBoostContext::resume() { - uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1); + uintptr_t worker_id = threads_working_.fetch_add(1, std::memory_order_relaxed); xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast(worker_id)); ParallelBoostContext* worker_context = static_cast(SIMIX_context_self()); diff --git a/src/kernel/context/ContextBoost.hpp b/src/kernel/context/ContextBoost.hpp index 1132f9860f..b06ddd7940 100644 --- a/src/kernel/context/ContextBoost.hpp +++ b/src/kernel/context/ContextBoost.hpp @@ -13,6 +13,7 @@ #include #endif +#include #include #include #include @@ -96,7 +97,7 @@ public: private: static simgrid::xbt::Parmap* parmap_; static std::vector workers_context_; - static uintptr_t threads_working_; + static std::atomic threads_working_; static xbt_os_thread_key_t worker_id_key_; }; #endif diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index c9b66a3d23..90e0d8e384 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -315,7 +315,7 @@ void SerialRawContext::run_all() #if HAVE_THREAD_CONTEXTS simgrid::xbt::Parmap* ParallelRawContext::parmap_; -uintptr_t ParallelRawContext::threads_working_; /* number of threads that have started their work */ +std::atomic ParallelRawContext::threads_working_; /* number of threads that have started their work */ xbt_os_thread_key_t ParallelRawContext::worker_id_key_; /* thread-specific storage for the thread id */ std::vector ParallelRawContext::workers_context_; /* space to save the worker context in each thread */ @@ -363,7 +363,7 @@ void ParallelRawContext::suspend() XBT_DEBUG("No more processes to run"); uintptr_t worker_id = reinterpret_cast(xbt_os_thread_get_specific(worker_id_key_)); next_context = workers_context_[worker_id]; - XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id, threads_working_); + XBT_DEBUG("Restoring worker stack %zu (working threads = %zu)", worker_id, threads_working_.load()); } SIMIX_context_set_current(next_context); @@ -372,7 +372,7 @@ void ParallelRawContext::suspend() void ParallelRawContext::resume() { - uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1); + uintptr_t worker_id = threads_working_.fetch_add(1, std::memory_order_relaxed); xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast(worker_id)); ParallelRawContext* worker_context = static_cast(SIMIX_context_self()); workers_context_[worker_id] = worker_context; diff --git a/src/kernel/context/ContextRaw.hpp b/src/kernel/context/ContextRaw.hpp index f2c9bc6765..68c779725f 100644 --- a/src/kernel/context/ContextRaw.hpp +++ b/src/kernel/context/ContextRaw.hpp @@ -6,6 +6,7 @@ #ifndef SIMGRID_SIMIX_RAW_CONTEXT_HPP #define SIMGRID_SIMIX_RAW_CONTEXT_HPP +#include #include #include #include @@ -79,7 +80,7 @@ public: private: static simgrid::xbt::Parmap* parmap_; static std::vector workers_context_; - static uintptr_t threads_working_; + static std::atomic threads_working_; static xbt_os_thread_key_t worker_id_key_; }; #endif diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index ea21e2579e..4e739c1555 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -188,7 +188,7 @@ void SerialUContext::run_all() #if HAVE_THREAD_CONTEXTS simgrid::xbt::Parmap* ParallelUContext::parmap_; -uintptr_t ParallelUContext::threads_working_; /* number of threads that have started their work */ +std::atomic ParallelUContext::threads_working_; /* number of threads that have started their work */ xbt_os_thread_key_t ParallelUContext::worker_id_key_; /* thread-specific storage for the thread id */ std::vector ParallelUContext::workers_context_; /* space to save the worker's context * in each thread */ @@ -263,7 +263,7 @@ void ParallelUContext::suspend() void ParallelUContext::resume() { // What is my containing body? - uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1); + uintptr_t worker_id = threads_working_.fetch_add(1, std::memory_order_relaxed); // Store the number of my containing body in os-thread-specific area : xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast(worker_id)); // Get my current soul: diff --git a/src/kernel/context/ContextUnix.hpp b/src/kernel/context/ContextUnix.hpp index 91badf2d55..471b11f98b 100644 --- a/src/kernel/context/ContextUnix.hpp +++ b/src/kernel/context/ContextUnix.hpp @@ -8,6 +8,7 @@ #include /* context relative declarations */ +#include #include #include #include @@ -76,7 +77,7 @@ public: private: static simgrid::xbt::Parmap* parmap_; static std::vector workers_context_; - static uintptr_t threads_working_; + static std::atomic threads_working_; static xbt_os_thread_key_t worker_id_key_; }; #endif -- 2.20.1