#include <wefts_rrwmutex.h>
Inheritance diagram for Wefts::RRWMutex:
Public Methods | |
RRWMutex () | |
Initializes the internal members of the mutex. | |
virtual void | lockRead () |
Locks the mutex for reading. | |
virtual void | promote (bool fence) |
Promote the lock from read only to readwrite. | |
virtual void | promote () |
Promote the locks from read only to readwrite. | |
virtual void | lockWrite (bool fence) |
Locks the mutex for writing. | |
virtual void | lockWrite () |
Locks the mutex for writing. | |
virtual void | unlock () |
Unlocks the mutex. | |
Private Attributes | |
volatile int | m_fence |
pthread_t | m_owner |
Read lock can be achieved as long as only read locks have been achieved; write lock can be achieved only by one thread, and only if no read locks area currently held.
In this mutex kind, write locks are reentrant. Also, a write locker can require to rise a lock fence, thus blocking any further request of new read lockers. This means that no new lock will be allowed on the mutex, and the write locker will take onwership of this as soon as the last reader that had control over it finishes its operations.
This mutex class ereditates the read lock promotion ability of its ancestor. It is also possible to promote a read-lock to write-lock and rising the fence.
All mutex classes are implemented via inline calls to maximize execution speed; also, their metods are extremely small.
|
Initializes the internal members of the mutex.
|
|
Locks the mutex for reading. Any amount of read locks can be achieved by the calling thread or by other read-locking threads. After that a read lock has been achieved, write lock requests will be blocked untill all the read lockers have relased their locks. If a write locker has risen the read fence, this function will block the calling thread also if the writer is WAITING to achieve the lock. Reimplemented from Wefts::RWMutex. |
|
Locks the mutex for writing. Equivalent to call method lockWrite( false ) (without read lock fence). Reimplemented from Wefts::RWMutex. |
|
Locks the mutex for writing. After this call has returned any request of lock (both read or write) will cause blocking of the caller until the owner of this thread releases the lock. This method is not reentrant, so never call lockWrite() twice from the same thread.
|
|
Promote the locks from read only to readwrite. Often, threads that have a read only lock on some data, after inspecting that data, find out that they need to alter it. Releasing the readLock and getting a write lock is not the optimal solution, both because it can cause unnecessary context switch, and because the situation of the data may change while the data is unlocked, requiring then a new rescan of the data to check if there is still a need for this thread to write it. Another solution may be locking for writing the whole scanning code, if there is any possibility that, sometimes, a write is also needed. But the most elegant solution is to promote a lock from read-only to read-write, so that no other writer is allowed to proceed before this thread can chage the data, but that all the other readers are done before this newly promoted thread can work.
Reimplemented from Wefts::RWMutex. |
|
Promote the lock from read only to readwrite. This methods works like the one provided in its ancestor, but it has also the ability to rise the fence, to prevent further readers to gain control of the mutex while this thread is waiting to be promoted.
|
|
Unlocks the mutex. Releases the lock held by the calling thread (be it a read-only lock or a write lock). If the mutex is locked for writing and the calling thread is not the owner of the lock, this call is ignored. Reimplemented from Wefts::RWMutex. |
|
|
|
|