13#include "coopsync_tbb/detail/macros.hpp"
14#include "coopsync_tbb/detail/wait_queue.hpp"
22inline void completion() noexcept {}
24using default_completion_t = void (*)();
37template <
typename CompletionFunction = detail::barrier::default_completion_t>
49 explicit barrier(std::ptrdiff_t expected, CompletionFunction completion =
50 detail::barrier::completion);
72 static constexpr std::ptrdiff_t
max() noexcept;
105 using token_base_t =
unsigned char;
106 CompletionFunction m_completion;
107 std::atomic<std::ptrdiff_t> m_initial_expected;
108 std::atomic<std::ptrdiff_t> m_expected;
109 std::atomic<token_base_t> m_phase;
110 detail::wait_queue m_waiters;
115template <typename CompletionFunction>
116class barrier<CompletionFunction>::arrival_token {
118 friend class barrier;
119 explicit arrival_token(token_base_t phase) noexcept : m_phase(phase) {}
120 token_base_t m_phase;
123template <
typename CompletionFunction>
125 CompletionFunction completion)
126 : m_completion(std::move(completion)),
127 m_initial_expected(expected),
128 m_expected(expected),
130 assert(expected >= 0);
131 assert(expected <=
max());
134template <
typename CompletionFunction>
136 assert(m_waiters.empty());
139template <
typename CompletionFunction>
141 return std::numeric_limits<std::ptrdiff_t>::max();
144template <
typename CompletionFunction>
149 const auto phase = m_phase.load(std::memory_order_relaxed);
150 const auto expected =
151 m_expected.fetch_sub(update, std::memory_order_acq_rel);
152 assert(expected >= 0);
155 if (expected == update) {
159 const auto next_expected =
160 m_initial_expected.load(std::memory_order_acquire);
161 m_expected.store(next_expected, std::memory_order_release);
163 const auto next_phase =
static_cast<token_base_t
>(phase + 1);
164 m_phase.store(next_phase, std::memory_order_release);
166 m_waiters.resume_all();
172template <
typename CompletionFunction>
176 const auto current_phase = m_phase.load(std::memory_order_acquire);
177 const auto previous_phase = current_phase - 1;
178 if (arrival.m_phase == previous_phase) {
181 assert(arrival.m_phase == current_phase);
184 const auto arrival_phase = arrival.m_phase;
185 m_waiters.wait_if([
this, arrival_phase]() {
186 return m_phase.load(std::memory_order_acquire) == arrival_phase;
190template <
typename CompletionFunction>
195template <
typename CompletionFunction>
198 m_initial_expected.fetch_sub(1, std::memory_order_acq_rel);
An opaque value representing task arrival at the barrier. It is used to wait for the completion of a ...
Definition barrier.hpp:116
void arrive_and_wait()
Decrements the expected counter by 1 and waits for the completion of the current phase....
Definition barrier.hpp:191
arrival_token arrive(std::ptrdiff_t update=1)
Arrives at the barrier with the specified update. Constructs an arrival token associated with the cur...
Definition barrier.hpp:146
barrier(const barrier &)=delete
Barrier is not copy-constructible.
barrier(std::ptrdiff_t expected, CompletionFunction completion=detail::barrier::completion)
Constructs a barrier with the specified initial expected count and completion function.
Definition barrier.hpp:124
barrier(barrier &&)=delete
Barrier is not move-constructible.
~barrier()
Destroys the mutex.
Definition barrier.hpp:135
void arrive_and_drop()
Decrements the initial expected count for the following phases by 1 and decrements the expect count f...
Definition barrier.hpp:196
barrier & operator=(barrier &&)=delete
Barrier is not move-assignable.
void wait(arrival_token arrival)
Waits for the completion of the phase associated with the given arrival token. If the token is associ...
Definition barrier.hpp:173
static constexpr std::ptrdiff_t max() noexcept
Returns the maximum value for the barrier counter.
Definition barrier.hpp:140
barrier & operator=(const barrier &)=delete
Barrier is not copy-assignable.
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11