11 #include <condition_variable> 97 ::std::condition_variable
cv;
108 template <
typename T>
114 this->acquireSucceeded = this->semaphore.acquire();
119 if (this->acquireSucceeded)
120 this->semaphore.release();
130 return this->acquireSucceeded;
138 bool acquireSucceeded {
false};
void release()
Release the semaphore (decrease its count).
void enable()
Enable the semaphore. Calling acquire() works normally after this call.
A reverse counting semaphore which can wait until its count is zero. Each acquire() increases this co...
~ReverseSemaphore()
Destroys this semaphore. Internally blocks it and waits for zero count.
T & semaphore
The guarded semaphore.
size_t getCount() const
Get the current number of unreleased acquire()s.
bool acquired() const
Whether the semaphore acquisition succeeded when constructing this guard.
volatile size_t count
The internal count of the semaphore.
volatile bool disabled
Whether the semaphore is disabled.
bool acquire()
Acquire the semaphore (increase its count). This method never blocks for long.
bool waitZeroAtDestroy
Whether to wait for zero when the object is being destroyed.
ReverseSemaphore(bool waitZeroAtDestroy=true)
Create the semaphore (internal count is zero).
::std::condition_variable cv
Condition variable used for signalling between release() and waitZero().
void disable()
Disable the semaphore. All following acquire() calls will return immediately with false...
mutable ::std::mutex mutex
Mutex protecting cv, count and disabled.
SemaphoreGuard(T &semaphore)
bool isEnabled() const
Whether the semaphore is enabled or not.
bool waitZero()
Wait until the internal count reaches zero.
bool isDestroying
True if the destructor has begun.
RAII guard for operations with a semaphore. On creation, the semaphore is acquired, and on destruction, it is released.