Public Member Functions | Private Attributes | List of all members
cras::ReverseSemaphore Class Reference

A reverse counting semaphore which can wait until its count is zero. Each acquire() increases this count and each release() decreases it. waitZero() is the function that waits until the internal count is zero. The semaphore can be disabled, which means no new acquire() calls will be accepted. This is useful if you plan to quit. More...

#include <semaphore.hpp>

Public Member Functions

bool acquire ()
 Acquire the semaphore (increase its count). This method never blocks for long. More...
 
void disable ()
 Disable the semaphore. All following acquire() calls will return immediately with false. More...
 
void enable ()
 Enable the semaphore. Calling acquire() works normally after this call. More...
 
size_t getCount () const
 Get the current number of unreleased acquire()s. More...
 
bool isEnabled () const
 Whether the semaphore is enabled or not. More...
 
void release ()
 Release the semaphore (decrease its count). More...
 
 ReverseSemaphore (bool waitZeroAtDestroy=true)
 Create the semaphore (internal count is zero). More...
 
bool waitZero ()
 Wait until the internal count reaches zero. More...
 
 ~ReverseSemaphore ()
 Destroys this semaphore. Internally blocks it and waits for zero count. More...
 

Private Attributes

volatile size_t count {0}
 The internal count of the semaphore. More...
 
::std::condition_variable cv
 Condition variable used for signalling between release() and waitZero(). More...
 
volatile bool disabled {false}
 Whether the semaphore is disabled. More...
 
bool isDestroying {false}
 True if the destructor has begun. More...
 
mutable ::std::mutex mutex
 Mutex protecting cv, count and disabled. More...
 
bool waitZeroAtDestroy
 Whether to wait for zero when the object is being destroyed. More...
 

Detailed Description

A reverse counting semaphore which can wait until its count is zero. Each acquire() increases this count and each release() decreases it. waitZero() is the function that waits until the internal count is zero. The semaphore can be disabled, which means no new acquire() calls will be accepted. This is useful if you plan to quit.

Definition at line 22 of file semaphore.hpp.

Constructor & Destructor Documentation

◆ ReverseSemaphore()

cras::ReverseSemaphore::ReverseSemaphore ( bool  waitZeroAtDestroy = true)
explicit

Create the semaphore (internal count is zero).

Parameters
[in]waitZeroAtDestroyWhether the semaphore should waitForZero() when it is destroyed.

◆ ~ReverseSemaphore()

cras::ReverseSemaphore::~ReverseSemaphore ( )

Destroys this semaphore. Internally blocks it and waits for zero count.

Member Function Documentation

◆ acquire()

bool cras::ReverseSemaphore::acquire ( )

Acquire the semaphore (increase its count). This method never blocks for long.

Returns
Whether acquisition succeeded. It can fail if the semaphore is disabled.

◆ disable()

void cras::ReverseSemaphore::disable ( )

Disable the semaphore. All following acquire() calls will return immediately with false.

◆ enable()

void cras::ReverseSemaphore::enable ( )

Enable the semaphore. Calling acquire() works normally after this call.

◆ getCount()

size_t cras::ReverseSemaphore::getCount ( ) const

Get the current number of unreleased acquire()s.

Note
Do not use this to tell when all acquire()s are released. Use waitZero() for that.
Returns
The number of unreleased acquire()s.

◆ isEnabled()

bool cras::ReverseSemaphore::isEnabled ( ) const

Whether the semaphore is enabled or not.

◆ release()

void cras::ReverseSemaphore::release ( )

Release the semaphore (decrease its count).

Note
If the internal count decreases to zero, all outstanding waitZero() calls are notified.
If the internal count should go below 0, an error is printed to stderr and all outstanding waitZero() calls are notified.
release() calls are processed even if the semaphore is disabled.

◆ waitZero()

bool cras::ReverseSemaphore::waitZero ( )

Wait until the internal count reaches zero.

Returns
Whether the wait succeeded. False can be returned if this semaphore is being destroyed.
Note
It is suggested to call disable() before this method if you call it because some object needs to exit.

Member Data Documentation

◆ count

volatile size_t cras::ReverseSemaphore::count {0}
private

The internal count of the semaphore.

Definition at line 88 of file semaphore.hpp.

◆ cv

::std::condition_variable cras::ReverseSemaphore::cv
private

Condition variable used for signalling between release() and waitZero().

Definition at line 97 of file semaphore.hpp.

◆ disabled

volatile bool cras::ReverseSemaphore::disabled {false}
private

Whether the semaphore is disabled.

Definition at line 91 of file semaphore.hpp.

◆ isDestroying

bool cras::ReverseSemaphore::isDestroying {false}
private

True if the destructor has begun.

Definition at line 85 of file semaphore.hpp.

◆ mutex

mutable ::std::mutex cras::ReverseSemaphore::mutex
private

Mutex protecting cv, count and disabled.

Definition at line 94 of file semaphore.hpp.

◆ waitZeroAtDestroy

bool cras::ReverseSemaphore::waitZeroAtDestroy
private

Whether to wait for zero when the object is being destroyed.

Definition at line 82 of file semaphore.hpp.


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


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sun Jan 14 2024 03:48:14