#include <wefts_semaphore.h>
Inheritance diagram for Wefts::Semaphore:
Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the counter atomically, and wait until the counter is non-zero and decrement it atomically.
Wefts semaphores have some added ability with respect of posix version: timed waits are allowed, and an internal count of waiters is held.
Wait (both timed and infinite) is considered a cancellation point. A cleanup routine will be called and the thread will be termianted if a cancellation request is issued while the thread is waiting for the semaphore to become available.
Public Member Functions | |
Semaphore (int initial_count=1) | |
Initializes the semaphore. | |
int | value () |
Gets the current value of the semaphore. | |
int | waiters () |
Gets the current amount of threads waiting for this resource. | |
void | post (int amount=1) |
Increments the semaphore count. | |
void | wait (CleanupHandler *handler=0, int position=0) |
Wait endlessy until the resource is available. | |
bool | wait (double seconds, CleanupHandler *handler=0, int position=0) |
Wait until the resource is available, with a given timeout. | |
bool | tryWait () |
Test if the resource is free, and if it is, it decrements resource count. | |
virtual void | handleCleanup (int code, void *caller=0) |
Private Attributes | |
int | m_count |
int | m_waiting |
FastCondition | m_cond |
|
Initializes the semaphore. The initial count value is 1 by default. This will make the first wait request on the semaphore to suceed and the second one to be blocked, thus making the semaphore acting effectively as a non-reentrant mutex. Use a different initialization value if the semaphore is a counter for a shared resource that may be assigned to a given amount of threads before to block other requests. In particular, 0 may be used to prevent the first wait request to proceed, providing a shared resource count that can be risen by other threads as resources becomes available. With initial count equal to 0, the semaphore acts similarily to a condition variable.
|
|
Implements Wefts::CleanupHandler. |
|
Increments the semaphore count. This is done by a thread when it wants to state that it is not holding the shared resource anymore. If the count reaches 1 or above and there are waiting threads, a signal is issued to wake them. If the current thread is responible for more than one shared resource accounted by the semaphore, it can call post() with a number greater than one, representing the amount of shared resources released.
|
|
Test if the resource is free, and if it is, it decrements resource count. As tryWait() does a fast check on the semaphore, it is not considered a cancellation point; the count of waiting threads is not changed and if the resource cannot be acquired, it returns immediately.
|
|
Gets the current value of the semaphore. Notice that unless the caller can guarantee that no other thread may change this value in the meanwhile, the returned value is meaningless, as it may change before the thread has the chance to evalute it.
|
|
Wait until the resource is available, with a given timeout. This means, until the count becomes 1. If seconds is set to 0, it exits almost immediately, but tryWait() should be used instead: the test is faster and lighter, and tryWait is NOT a cancellation point (while wait is).
|
|
Wait endlessy until the resource is available. This means, until the count becomes 1. If the current thread is canceled during wait, then the cleanup handler passed as a parameter is called. By default, the cleanup action of this class unlocks the internal mutex and removes itself from the count of waiting threads. If another cleanup routine is used, Semaphore::handleCleanup() must be anyway called at its end, to ensure that waiting list is correctly updated. Anyhow, application level threads should use Thread::pushCleanup(); this method is meant only to hook subclasses or provide specialized semaphores.
|
|
Gets the current amount of threads waiting for this resource. Notice that unless the caller can guarantee that no other thread may change this value in the meanwhile, the returned value is meaningless, as it may change before the thread has the chance to evalute it.
|
|
|
|
|
|
|