fesa-core
7.0.0
|
#include <RWLock.h>
Public Member Functions | |
void | lockForReading () |
void | lockForWriting () |
void | unlock () |
void | unlock (int nLocksToKeep) |
bool | isReadLockHeld () const |
bool | isWriteLockHeld () const |
bool | isLockHeld () const |
const RWLockStatus & | getLockStatus () const |
Protected Member Functions | |
RWLock (bool initialize) | |
void | checkAlignment () const |
Object which allows mutual exclusion between threads. Any number of threads can simultaneously hold a lock for reading. Only one thread can hold a lock for writing, and only when no thread holds a lock for reading. The class wraps a pthread_rwlock_t and tracks locks and unlocks per thread. This allows recursive read and write locks with a single lock of the pthread_rwlock. If the first recursive lock request from the unlocked state is lockForReading a read-lock will be acquired and all subsequent lock requests may only be read-locks. If the first recursive lock request from the unlocked state is lockForWriting a write-lock will be acquired and all subsequent lock requests may be read- or write-locks.
For general use it is recommended to use the ReadLock/WriteLock classes to ensure all locks are released.
|
protected |
Constructor which can skip initialization of rwlock_.
initialize | the read-write lock is initialized only if this is true. |
|
protected |
Checks that rwlock_ is properly aligned.
FesaException | if rwlock_ is not properly aligned. |
const RWLockStatus & fesa::RWLock::getLockStatus | ( | ) | const |
Returns the status of this RWLock object.
bool fesa::RWLock::isLockHeld | ( | ) | const |
Returns true if the current thread holds a lock of any kind on this RWLock object.
bool fesa::RWLock::isReadLockHeld | ( | ) | const |
Returns true if the current thread holds a read-lock on this RWLock object.
bool fesa::RWLock::isWriteLockHeld | ( | ) | const |
Returns true if the current thread holds a write-lock on this RWLock object.
void fesa::RWLock::lockForReading | ( | ) |
Locks the read-write lock for reading. Many threads can hold a read lock at one time.
void fesa::RWLock::lockForWriting | ( | ) |
Locks the read-write lock for writing. Only one thread can hold a write lock at one time.
void fesa::RWLock::unlock | ( | ) |
Releases a read- or write-lock. An object locked for reading is unlocked when all readers have called unlock. An object locked for writing is unlocked when the writer has called unlock.
void fesa::RWLock::unlock | ( | int | nLocksToKeep | ) |
Releases all the read- and write-locks of the calling thread except the given number
nLocksToKeep | The number of locks that should not be released |