From: Millian Poquet Date: Mon, 24 Sep 2018 17:11:50 +0000 (+0200) Subject: [s4u] Barrier refcounting X-Git-Tag: v3_21~47^2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8bff32bae7e7bcd9c20695dc02666e6c450a0bdc [s4u] Barrier refcounting --- diff --git a/include/simgrid/forward.h b/include/simgrid/forward.h index 38d662dfb2..86a28bd641 100644 --- a/include/simgrid/forward.h +++ b/include/simgrid/forward.h @@ -24,6 +24,10 @@ XBT_PUBLIC void intrusive_ptr_release(Actor* actor); XBT_PUBLIC void intrusive_ptr_add_ref(Actor* actor); class Barrier; +/** Smart pointer to a simgrid::s4u::Barrier */ +typedef boost::intrusive_ptr BarrierPtr; +XBT_PUBLIC void intrusive_ptr_release(Barrier* m); +XBT_PUBLIC void intrusive_ptr_add_ref(Barrier* m); class Comm; /** Smart pointer to a simgrid::s4u::Comm */ diff --git a/include/simgrid/s4u/Barrier.hpp b/include/simgrid/s4u/Barrier.hpp index 4b578983ec..b225d9f772 100644 --- a/include/simgrid/s4u/Barrier.hpp +++ b/include/simgrid/s4u/Barrier.hpp @@ -6,10 +6,12 @@ #ifndef SIMGRID_S4U_BARRIER_HPP #define SIMGRID_S4U_BARRIER_HPP -#include "simgrid/s4u/ConditionVariable.hpp" +#include +#include #include #include +#include #include namespace simgrid { @@ -22,13 +24,23 @@ private: unsigned int expected_processes_; unsigned int arrived_processes_ = 0; + /* refcounting */ + std::atomic_int_fast32_t refcount_{0}; + public: explicit Barrier(unsigned int count); ~Barrier() = default; Barrier(Barrier const&) = delete; Barrier& operator=(Barrier const&) = delete; + /** Constructs a new barrier */ + static BarrierPtr create(unsigned int expected_processes); + int wait(); + + /* refcounting */ + friend XBT_PUBLIC void intrusive_ptr_add_ref(Barrier* barrier); + friend XBT_PUBLIC void intrusive_ptr_release(Barrier* barrier); }; } } // namespace simgrid::s4u diff --git a/src/s4u/s4u_Barrier.cpp b/src/s4u/s4u_Barrier.cpp index d19fb26337..fb71bbb4c9 100644 --- a/src/s4u/s4u_Barrier.cpp +++ b/src/s4u/s4u_Barrier.cpp @@ -21,6 +21,15 @@ Barrier::Barrier(unsigned int expected_processes) : mutex_(Mutex::create()), con { } +/** @brief Create a new barrier + * + * See @ref s4u_raii. + */ +BarrierPtr Barrier::create(unsigned int expected_processes) +{ + return BarrierPtr(new Barrier(expected_processes)); +} + /** * Wait functions */ @@ -40,6 +49,21 @@ int Barrier::wait() mutex_->unlock(); return 0; } + +void intrusive_ptr_add_ref(Barrier* barrier) +{ + xbt_assert(barrier); + barrier->refcount_.fetch_add(1, std::memory_order_relaxed); +} + +void intrusive_ptr_release(Barrier* barrier) +{ + xbt_assert(barrier); + if (barrier->refcount_.fetch_sub(1, std::memory_order_release) == 1) { + std::atomic_thread_fence(std::memory_order_acquire); + delete barrier; + } +} } // namespace s4u } // namespace simgrid