Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++ atomic instead of compiler builtins.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Sun, 29 Oct 2017 22:27:32 +0000 (23:27 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Sun, 29 Oct 2017 22:30:46 +0000 (23:30 +0100)
src/kernel/context/ContextBoost.cpp
src/kernel/context/ContextBoost.hpp
src/kernel/context/ContextRaw.cpp
src/kernel/context/ContextRaw.hpp
src/kernel/context/ContextUnix.cpp
src/kernel/context/ContextUnix.hpp

index a9422e0..1d26b05 100644 (file)
@@ -202,7 +202,7 @@ void SerialBoostContext::run_all()
 #if HAVE_THREAD_CONTEXTS
 
 simgrid::xbt::Parmap<smx_actor_t>* ParallelBoostContext::parmap_;
-uintptr_t ParallelBoostContext::threads_working_;
+std::atomic<uintptr_t> ParallelBoostContext::threads_working_;
 xbt_os_thread_key_t ParallelBoostContext::worker_id_key_;
 std::vector<ParallelBoostContext*> 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<void*>(worker_id));
 
   ParallelBoostContext* worker_context = static_cast<ParallelBoostContext*>(SIMIX_context_self());
index 1132f98..b06ddd7 100644 (file)
@@ -13,6 +13,7 @@
 #include <boost/context/detail/fcontext.hpp>
 #endif
 
+#include <atomic>
 #include <cstdint>
 #include <functional>
 #include <vector>
@@ -96,7 +97,7 @@ public:
 private:
   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
   static std::vector<ParallelBoostContext*> workers_context_;
-  static uintptr_t threads_working_;
+  static std::atomic<uintptr_t> threads_working_;
   static xbt_os_thread_key_t worker_id_key_;
 };
 #endif
index c9b66a3..90e0d8e 100644 (file)
@@ -315,7 +315,7 @@ void SerialRawContext::run_all()
 #if HAVE_THREAD_CONTEXTS
 
 simgrid::xbt::Parmap<smx_actor_t>* ParallelRawContext::parmap_;
-uintptr_t ParallelRawContext::threads_working_;         /* number of threads that have started their work */
+std::atomic<uintptr_t> 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*> 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<uintptr_t>(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<void*>(worker_id));
   ParallelRawContext* worker_context = static_cast<ParallelRawContext*>(SIMIX_context_self());
   workers_context_[worker_id]        = worker_context;
index f2c9bc6..68c7797 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef SIMGRID_SIMIX_RAW_CONTEXT_HPP
 #define SIMGRID_SIMIX_RAW_CONTEXT_HPP
 
+#include <atomic>
 #include <cstdint>
 #include <functional>
 #include <vector>
@@ -79,7 +80,7 @@ public:
 private:
   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
   static std::vector<ParallelRawContext*> workers_context_;
-  static uintptr_t threads_working_;
+  static std::atomic<uintptr_t> threads_working_;
   static xbt_os_thread_key_t worker_id_key_;
 };
 #endif
index ea21e25..4e739c1 100644 (file)
@@ -188,7 +188,7 @@ void SerialUContext::run_all()
 #if HAVE_THREAD_CONTEXTS
 
 simgrid::xbt::Parmap<smx_actor_t>* ParallelUContext::parmap_;
-uintptr_t ParallelUContext::threads_working_;                      /* number of threads that have started their work */
+std::atomic<uintptr_t> 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*> 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<void*>(worker_id));
   // Get my current soul:
index 91badf2..471b11f 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <ucontext.h> /* context relative declarations */
 
+#include <atomic>
 #include <cstdint>
 #include <functional>
 #include <vector>
@@ -76,7 +77,7 @@ public:
 private:
   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
   static std::vector<ParallelUContext*> workers_context_;
-  static uintptr_t threads_working_;
+  static std::atomic<uintptr_t> threads_working_;
   static xbt_os_thread_key_t worker_id_key_;
 };
 #endif