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... | |
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.
|
explicit |
Create the semaphore (internal count is zero).
[in] | waitZeroAtDestroy | Whether the semaphore should waitForZero() when it is destroyed. |
cras::ReverseSemaphore::~ReverseSemaphore | ( | ) |
Destroys this semaphore. Internally blocks it and waits for zero count.
bool cras::ReverseSemaphore::acquire | ( | ) |
Acquire the semaphore (increase its count). This method never blocks for long.
void cras::ReverseSemaphore::disable | ( | ) |
Disable the semaphore. All following acquire()
calls will return immediately with false.
void cras::ReverseSemaphore::enable | ( | ) |
Enable the semaphore. Calling acquire()
works normally after this call.
size_t cras::ReverseSemaphore::getCount | ( | ) | const |
Get the current number of unreleased acquire()
s.
acquire()
s are released. Use waitZero()
for that. acquire()
s. bool cras::ReverseSemaphore::isEnabled | ( | ) | const |
Whether the semaphore is enabled or not.
void cras::ReverseSemaphore::release | ( | ) |
Release the semaphore (decrease its count).
waitZero()
calls are notified. waitZero()
calls are notified. release()
calls are processed even if the semaphore is disabled. bool cras::ReverseSemaphore::waitZero | ( | ) |
Wait until the internal count reaches zero.
disable()
before this method if you call it because some object needs to exit.
|
private |
The internal count of the semaphore.
Definition at line 88 of file semaphore.hpp.
|
private |
Condition variable used for signalling between release()
and waitZero()
.
Definition at line 97 of file semaphore.hpp.
|
private |
Whether the semaphore is disabled.
Definition at line 91 of file semaphore.hpp.
|
private |
True if the destructor has begun.
Definition at line 85 of file semaphore.hpp.
|
private |
Mutex protecting cv
, count
and disabled
.
Definition at line 94 of file semaphore.hpp.
|
private |
Whether to wait for zero when the object is being destroyed.
Definition at line 82 of file semaphore.hpp.