30#include <condition_variable>
52 mutable std::mutex mtx_cd;
53 mutable std::condition_variable cv;
58 static constexpr size_t max() noexcept {
return std::numeric_limits<size_t>::max(); }
76 latch(const
size_t count_) noexcept
93 size_t value() const noexcept {
return count; }
110 std::unique_lock<std::mutex> lock(mtx_cd);
136 std::unique_lock<std::mutex> lock(mtx_cd);
137 if( n <= std::numeric_limits<std::size_t>::max() - count ) {
140 count = std::numeric_limits<std::size_t>::max();
153 void set(
const size_t n)
noexcept {
154 std::unique_lock<std::mutex> lock(mtx_cd);
176 std::unique_lock<std::mutex> lock(mtx_cd);
211 std::unique_lock<std::mutex> lock(mtx_cd);
214 std::cv_status s =
wait_until(cv, lock, timeout_time);
218 if( std::cv_status::timeout == s ) {
259 template<
typename Rep,
typename Period>
260 bool wait_for(
const std::chrono::duration<Rep, Period>& timeout_duration)
const noexcept {
262 std::unique_lock<std::mutex> lock(mtx_cd);
263 std::chrono::steady_clock::time_point timeout_time = std::chrono::steady_clock::now() + timeout_duration;
265 std::cv_status s = cv.wait_until(lock, timeout_time );
269 if( std::cv_status::timeout == s ) {
293 template<
typename Rep,
typename Period>
294 bool arrive_and_wait_for(
const std::chrono::duration<Rep, Period>& timeout_duration,
const size_t n = 1) noexcept {
void arrive_and_wait(const size_t n=1) noexcept
Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until th...
latch(const latch &o)=delete
No copy constructor nor move constructor.
static constexpr size_t max() noexcept
Returns the maximum value of the internal counter supported by the implementation.
size_t value() const noexcept
Return the current atomic internal counter.
bool arrive_and_wait_for(const std::chrono::duration< Rep, Period > &timeout_duration, const size_t n=1) noexcept
Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until th...
bool try_wait() const noexcept
Returns true only if the internal counter has reached zero.
void set(const size_t n) noexcept
Atomically set the internal counter by n.
bool wait_for(const fraction_i64 &timeout_duration) const noexcept
Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expi...
void count_down(const size_t n=1) noexcept
Atomically decrements the internal counter by n and notifies all blocked wait() threads if zero is re...
latch() noexcept=default
Initialize instance with counter zero.
bool arrive_and_wait_for(const fraction_i64 &timeout_duration, const size_t n=1) noexcept
Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until th...
void count_up(const size_t n=1) noexcept
Atomically increments the internal counter by n, i.e.
void wait() const noexcept
Blocks the calling thread until the internal counter reaches 0.
bool wait_for(const std::chrono::duration< Rep, Period > &timeout_duration) const noexcept
Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expi...
ordered_atomic< std::size_t, std::memory_order_seq_cst > sc_atomic_size_t
SC atomic integral scalar size_t.
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
std::cv_status wait_until(std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_timespec &absolute_time, const bool monotonic=true) noexcept
wait_until causes the current thread to block until the condition variable is notified,...
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...