Class ReverseSemaphore

Class Documentation

class ReverseSemaphore

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.

Public Functions

explicit ReverseSemaphore(bool waitZeroAtDestroy = true)

Create the semaphore (internal count is zero).

Parameters:

waitZeroAtDestroy[in] Whether the semaphore should waitForZero() when it is destroyed.

~ReverseSemaphore()

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

bool 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.

void release()

Release the semaphore (decrease its count).

Note

If the internal count decreases to zero, all outstanding waitZero() calls are notified.

Note

If the internal count should go below 0, an error is printed to stderr and all outstanding waitZero() calls are notified.

Note

release() calls are processed even if the semaphore is disabled.

bool waitZero()

Wait until the internal count reaches zero.

Note

It is suggested to call disable() before this method if you call it because some object needs to exit.

Returns:

Whether the wait succeeded. False can be returned if this semaphore is being destroyed.

void disable()

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

void enable()

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

bool isEnabled() const

Whether the semaphore is enabled or not.

size_t 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.