jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Public Member Functions | Static Public Member Functions | List of all members
jau::latch Class Reference

Inspired by std::latch of C++20. More...

#include <latch.hpp>

Collaboration diagram for jau::latch:

Public Member Functions

 latch () noexcept
 Initialize instance with counter zero. More...
 
 latch (const latch &o)=delete
 No copy constructor nor move constructor. More...
 
 latch (const size_t count_) noexcept
 Initialize instance with given counter. More...
 
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 the counter reaches zero. More...
 
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 the counter reaches zero or the given timeout duration has expired. More...
 
template<typename Rep , typename Period >
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 the counter reaches zero or the given timeout duration has expired. More...
 
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 reached. More...
 
void count_up (const size_t n=1) noexcept
 Atomically increments the internal counter by n, i.e. More...
 
void set (const size_t n) noexcept
 Atomically set the internal counter by n. More...
 
bool try_wait () const noexcept
 Returns true only if the internal counter has reached zero. More...
 
size_t value () const noexcept
 Return the current atomic internal counter. More...
 
void wait () const noexcept
 Blocks the calling thread until the internal counter reaches 0. More...
 
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 expired. More...
 
template<typename Rep , typename Period >
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 expired. More...
 

Static Public Member Functions

static constexpr size_t max () noexcept
 Returns the maximum value of the internal counter supported by the implementation. More...
 

Detailed Description

Inspired by std::latch of C++20.

Adds count_up() as an extension, allowing to dynamically add events to required to complete.

See also
https://en.cppreference.com/w/cpp/thread/latch

Definition at line 50 of file latch.hpp.

Constructor & Destructor Documentation

◆ latch() [1/3]

jau::latch::latch ( )
inlinenoexcept

Initialize instance with counter zero.

Useful for member instances in combination with count_up() or with set() before count_down().

Extension of std::latch.

Definition at line 67 of file latch.hpp.

◆ latch() [2/3]

jau::latch::latch ( const size_t  count_)
inlinenoexcept

Initialize instance with given counter.

Compatible with std::latch.

Parameters
count_

Definition at line 77 of file latch.hpp.

◆ latch() [3/3]

jau::latch::latch ( const latch o)
delete

No copy constructor nor move constructor.

Compatible with std::latch.

Parameters
o

Member Function Documentation

◆ max()

static constexpr size_t jau::latch::max ( )
inlinestaticconstexprnoexcept

Returns the maximum value of the internal counter supported by the implementation.

Definition at line 58 of file latch.hpp.

◆ value()

size_t jau::latch::value ( ) const
inlinenoexcept

Return the current atomic internal counter.

Extension of std::latch.

Definition at line 94 of file latch.hpp.

Here is the caller graph for this function:

◆ count_down()

void jau::latch::count_down ( const size_t  n = 1)
inlinenoexcept

Atomically decrements the internal counter by n and notifies all blocked wait() threads if zero is reached.

If n is greater than the value of the internal counter, the counter is set to zero.

This operation strongly happens-before all calls that are unblocked on this latch.

Compatible with std::latch.

Parameters
nthe value by which the internal counter is decreased, defaults to 1

Definition at line 108 of file latch.hpp.

Here is the caller graph for this function:

◆ count_up()

void jau::latch::count_up ( const size_t  n = 1)
inlinenoexcept

Atomically increments the internal counter by n, i.e.

adds value to count down.

If internal counter + n is greater than the maximum value of the internal counter, the counter is set to its maximum.

This operation strongly happens-before all calls that are unblocked on this latch.

Extension of std::latch.

Parameters
nthe value by which the internal counter is increased, defaults to 1

Definition at line 136 of file latch.hpp.

◆ set()

void jau::latch::set ( const size_t  n)
inlinenoexcept

Atomically set the internal counter by n.

This operation strongly happens-before all calls that are unblocked on this latch.

Extension of std::latch.

Parameters
nthe value to be assigned to the internal counter

Definition at line 154 of file latch.hpp.

Here is the caller graph for this function:

◆ try_wait()

bool jau::latch::try_wait ( ) const
inlinenoexcept

Returns true only if the internal counter has reached zero.

Compatible with std::latch.

Definition at line 164 of file latch.hpp.

◆ wait()

void jau::latch::wait ( ) const
inlinenoexcept

Blocks the calling thread until the internal counter reaches 0.

If the internal counter is zero already, returns immediately.

Compatible with std::latch.

Definition at line 175 of file latch.hpp.

Here is the caller graph for this function:

◆ arrive_and_wait()

void jau::latch::arrive_and_wait ( const size_t  n = 1)
inlinenoexcept

Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero.

Equivalent to count_down(n); wait();.

Compatible with std::latch.

Parameters
nthe value by which the internal counter is decreased, defaults to 1

Definition at line 193 of file latch.hpp.

Here is the caller graph for this function:

◆ wait_for() [1/2]

bool jau::latch::wait_for ( const fraction_i64 timeout_duration) const
inlinenoexcept

Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.

If the internal counter is zero already, returns immediately.

Implementation uses wait_for() w/ a monotonic clock and fraction_i64.

Extension of std::latch.

Parameters
timeout_durationmaximum duration in fractions of seconds to wait
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 210 of file latch.hpp.

Here is the caller graph for this function:

◆ arrive_and_wait_for() [1/2]

bool jau::latch::arrive_and_wait_for ( const fraction_i64 timeout_duration,
const size_t  n = 1 
)
inlinenoexcept

Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero or the given timeout duration has expired.

Equivalent to count_down(n); wait(timeout_duration);.

Implementation uses std::chrono::steady_clock::now() and fraction_i64.

Extension of std::latch.

Parameters
timeout_durationmaximum duration in fractions of seconds to wait
nthe value by which the internal counter is decreased, defaults to 1
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 241 of file latch.hpp.

Here is the caller graph for this function:

◆ wait_for() [2/2]

template<typename Rep , typename Period >
bool jau::latch::wait_for ( const std::chrono::duration< Rep, Period > &  timeout_duration) const
inlinenoexcept

Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.

If the internal counter is zero already, returns immediately.

Implementation uses std::chrono::steady_clock::now().

Extension of std::latch.

Template Parameters
Rep
Period
Parameters
timeout_durationmaximum duration to wait
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 261 of file latch.hpp.

◆ arrive_and_wait_for() [2/2]

template<typename Rep , typename Period >
bool jau::latch::arrive_and_wait_for ( const std::chrono::duration< Rep, Period > &  timeout_duration,
const size_t  n = 1 
)
inlinenoexcept

Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero or the given timeout duration has expired.

Equivalent to count_down(n); wait(timeout_duration);.

Implementation uses std::chrono::steady_clock::now().

Extension of std::latch.

Template Parameters
Rep
Period
Parameters
timeout_durationmaximum duration to wait
nthe value by which the internal counter is decreased, defaults to 1
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 295 of file latch.hpp.


The documentation for this class was generated from the following file: