The class provides a mutex that allows for many readers or one writer.
A flag, that is set in the constructor, determines whether waiting readers or waiting writers are given priority if the mutex is unlocked. The behaviour is as follows:
If readers are preferred, new readers will be allowed if the mutex is currently not locked for writing. A write lock can only be aquired if no readers hold or are waiting for a lock.
If writers are preferred, a read lock can only be aquired when no writers are requesting or holding locks.
A possible enhancement is to allow for recursive locks of the mutex. This may also include to allow a thread, currently holding a read lock, to aquire a write lock.
Definition at line 166 of file mutex.h.
typedef std::deque<LockType> OSB_LIB::RwMutex::LockQueue [private] |
enum OSB_LIB::RwMutex::LockType [private] |
Priority defines whether waiting readers or writers are perferred or none.
OSB_LIB::RwMutex::RwMutex | ( | Priority | priority = prioRead , |
|
const pthread_mutexattr_t * | attr = 0 | |||
) | [explicit] |
Constructor with priority setting.
priority | Flag if priority is given to waiting writers or readers or in the order of requests. | |
attr | Posix mutex attributes used for initalization of `mutex_', see pthread_mutex_init for details. |
OSB_LIB::RwMutex::~RwMutex | ( | ) |
Destructor: Destroys the mutex and the conditions.
OSB_LIB::RwMutex::RwMutex | ( | const RwMutex & | ) | [private] |
Prevent copying: not implemented.
void OSB_LIB::RwMutex::addWait | ( | LockType | lt | ) | [private] |
Add a waiting request.
bool OSB_LIB::RwMutex::canReadLock | ( | ) | [private] |
Check if read lock is possible.
bool OSB_LIB::RwMutex::canWriteLock | ( | ) | [private] |
Check if write lock is possible.
Prevent assignment: not implemented.
int OSB_LIB::RwMutex::readLock | ( | ) |
Get a read lock.
The function gets a read lock. It blocks until the lock is successfully aquired or an error occurs.
Referenced by OSB_LIB::ResourceList< R >::readLock(), and OSB_LIB::RwShared< T >::readLock().
int OSB_LIB::RwMutex::readUnlock | ( | ) |
Release a read lock.
Referenced by OSB_LIB::ResourceList< R >::readLock(), and OSB_LIB::RwShared< T >::readUnlock().
void OSB_LIB::RwMutex::rmWait | ( | LockType | lt | ) | [private] |
Remove a waiting request.
void OSB_LIB::RwMutex::sendCondition | ( | ) | [private] |
Signal read or write condition.
int OSB_LIB::RwMutex::tryReadLock | ( | ) |
Attempts to get a read lock without waiting.
The function tries to get a read lock. It returns immediately if this is not possible, e.g. because a writer currently owns the lock.
The lock will succeed under the following conditions: ToDo.
Referenced by OSB_LIB::RwShared< T >::tryReadLock().
int OSB_LIB::RwMutex::tryWriteLock | ( | ) |
Attempts to get a write lock without waiting.
The function tries to get a write lock. It returns immediately if this is not possible, e.g., because one or more readers are currently holding a lock.
The lock will succeed under the following conditions: ToDo.
Referenced by OSB_LIB::RwShared< T >::tryWriteLock().
int OSB_LIB::RwMutex::writeLock | ( | ) |
Get a write lock.
The function gets a write lock. It blocks until the lock is successfully aquired or an error occurs.
Referenced by OSB_LIB::ResourceList< R >::writeLock(), and OSB_LIB::RwShared< T >::writeLock().
int OSB_LIB::RwMutex::writeUnlock | ( | ) |
Release a write lock.
Referenced by OSB_LIB::ResourceList< R >::writeLock(), and OSB_LIB::RwShared< T >::writeUnlock().
pthread_cond_t OSB_LIB::RwMutex::condRead_ [private] |
pthread_cond_t OSB_LIB::RwMutex::condWrite_ [private] |
int OSB_LIB::RwMutex::curReaders_ [private] |
int OSB_LIB::RwMutex::curWriters_ [private] |
LockQueue OSB_LIB::RwMutex::lockQueue_ [private] |
pthread_mutex_t OSB_LIB::RwMutex::mutex_ [private] |
Priority OSB_LIB::RwMutex::priority_ [private] |