5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_SYNCHRONIZATION_BARRIER_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_SYNCHRONIZATION_BARRIER_HPP_
29namespace barrier_detail {
31class default_completion_function {
35 constexpr void operator()() const noexcept {}
45template <
typename CompletionFunction = barrier_detail::default_completion_function>
50 "The completion function must be move-constructible"
55 "The completion function must be nothrow-invocable with no arguments"
60 class private_construction_tag {
63 explicit private_construction_tag()
noexcept =
default;
111 : completion_function_(
std::
move(func)),
113 initial_count_(count),
125 typename F = CompletionFunction,
128 : completion_function_(CompletionFunction{}),
130 initial_count_(count),
162 std::ignore = initial_count_.fetch_sub(1, std::memory_order_relaxed);
174 auto const remaining_count = count_.load(std::memory_order_relaxed);
176 auto phase = phase_.load(std::memory_order_relaxed);
177 if (count_.fetch_sub(arrivals, std::memory_order_acq_rel) == arrivals) {
178 run_completion_step(phase);
180 return {private_construction_tag{}, phase};
194 auto const current_phase = phase_.load(std::memory_order_relaxed);
196 completed_event(token.
get_phase()).wait();
203 completion_function_();
204 auto new_phase = phase_.fetch_add(1U, std::memory_order_relaxed) + 1U;
205 count_ = initial_count_.load(std::memory_order_relaxed);
206 completed_event(new_phase).reset();
207 completed_event(completed_phase).signal();
213 auto completed_event(
std::uint64_t event_phase) -> manual_reset_event& {
215 return (event_phase % two) == 0U ? completed_even_ : completed_odd_;
219 CompletionFunction completion_function_;
221 std::atomic<std::uint64_t> phase_;
223 std::atomic<std::ptrdiff_t> initial_count_;
225 std::atomic<std::ptrdiff_t> count_;
227 manual_reset_event completed_odd_;
229 manual_reset_event completed_even_;
A class holding the result of a call to arrive, which can then be passed to wait to wait for the end ...
Definition barrier.hpp:74
auto get_phase() const noexcept -> std::uint64_t
Get the phase this token is associated with.
Definition barrier.hpp:100
arrival_token(arrival_token &&other) noexcept=default
Default move constructor.
auto operator=(arrival_token &&other) noexcept -> arrival_token &=default
Default move assignment.
arrival_token(arrival_token const &other)=delete
not copyable
auto operator=(arrival_token const &other) -> arrival_token &=delete
not copyable
arrival_token(private_construction_tag, std::uint64_t phase) noexcept
Construct a new arrival token for the phase.
Definition barrier.hpp:82
~arrival_token()=default
Default destructor.
ARENE_NODISCARD auto arrive() -> arrival_token
Arrive at the barrier, and obtain a token that can be used to wait for the end of the completion step...
Definition barrier.hpp:186
constexpr barrier(std::ptrdiff_t count) noexcept(std::is_nothrow_default_constructible< F >::value)
Construct a barrier with a specified count and default-constructed completion function.
Definition barrier.hpp:127
static constexpr auto max() noexcept -> std::ptrdiff_t
Get the maximum supported count.
Definition barrier.hpp:69
ARENE_NODISCARD auto arrive(std::ptrdiff_t arrivals) -> arrival_token
Arrive at the barrier with the specified number of arrivals, and obtain a token that can be used to w...
Definition barrier.hpp:173
~barrier()=default
Default destructor.
barrier(barrier &&)=delete
Not movable.
auto operator=(barrier const &) -> barrier &=delete
Not copyable.
constexpr barrier(std::ptrdiff_t count, CompletionFunction func) noexcept(std::is_nothrow_move_constructible< CompletionFunction >::value)
Construct a barrier with a specified count and completion function.
Definition barrier.hpp:108
void arrive_and_wait()
Arrive at the barrier and wait for the completion step to end. May trigger the completion step.
Definition barrier.hpp:153
auto operator=(barrier &&) -> barrier &=delete
Not movable.
void arrive_and_drop()
Decrease the number of arrivals required for the subsequent phases to end, and arrive at the barrier....
Definition barrier.hpp:160
void wait(arrival_token token)
Wait for the completion step associated with the phase identified by token to end.
Definition barrier.hpp:193
barrier(barrier const &)=delete
Not copyable.
static constexpr value_type value
Definition integral_constant.hpp:34
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
Public export header for the constraints subpackage.
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Public export header for the contracts subpackage.
Definition accumulate.hpp:37
constexpr auto move(InIterator first, InIterator last, OutIterator output) noexcept(noexcept(*first++) &&noexcept(output++) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InIterator >::value_type >::value) -> OutIterator
Moves the elements in the range [first, last), to another range beginning at output,...
Definition move.hpp:46
::std::ptrdiff_t ptrdiff_t
The type of the difference between two pointers.
Definition builtin_types.hpp:27
::std::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition integer_types.hpp:46
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26