Qore Programming Language Reference Manual
0.8.11.1
|
The RWLock class implements a read-write thread lock. More...
Public Member Functions | |
constructor () | |
Creates the RWLock object. More... | |
copy () | |
Creates a new RWLock object, not based on the original. More... | |
destructor () | |
Destroys the RWLock object. More... | |
int | getReadWaiting () |
Returns the number of threads waiting on the read lock. More... | |
int | getWriteWaiting () |
Returns the number of threads waiting on the write lock. More... | |
bool | lockOwner () |
Returns True if the current thread is holding either the read lock or the write lock, False if not. More... | |
int | numReaders () |
Returns the read lock count. More... | |
nothing | readLock () |
Acquires the read lock; blocks if the write lock is already acquired by another thread. More... | |
int | readLock (timeout timeout_ms) |
Acquires the read lock with a timeout value; blocks if the write lock is already acquired by another thread. More... | |
bool | readLockOwner () |
Returns True if the current thread is holding the read lock, False if not. More... | |
nothing | readUnlock () |
Decrements the read lock counter and releases the read lock if the counter is zero. If at least one thread is blocked trying to acquire the write lock and the read counter reaches zero, then one thread waiting on the write lock is woken up. More... | |
int | tryReadLock () |
Acquires the read lock only if it can be acquired immediately. More... | |
int | tryWriteLock () |
Acquires the write lock only if it can be acquired immediately. More... | |
int | writeLock (timeout timeout_ms) |
Acquires the write lock with a timeout value; blocks if the read lock is already acquired by another thread. More... | |
nothing | writeLock () |
Acquires the write lock; blocks if the read lock is already acquired by another thread. More... | |
bool | writeLockOwner () |
Returns True if the current thread is holding the write lock, False if not. More... | |
nothing | writeUnlock () |
Releases the write lock, if any readers are waiting, wakes up all readers, otherwise if any writers are waiting, then wakes one up. More... | |
Public Member Functions inherited from Qore::Thread::AbstractSmartLock | |
constructor () | |
Throws an exception if called directly; this class can only be instantiated by builtin subclasses. More... | |
string | getName () |
Returns the name of the threading class directly inheriting this class. More... | |
bool | lockOwner () |
Returns True if the calling thread owns the lock, False if not. More... | |
int | lockTID () |
Returns the TID of the thread owning the lock or -1 if the lock is currently not acquired. More... | |
The RWLock class implements a read-write thread lock.
This class inherits AbstractSmartLock, so it can be used by Condition objects, while using either the read lock or the write lock.
The RWLock class implements a read-write lock for efficient thread locking when write actions must be atomic and reads can be made in parallel if no write is in progress. When a thread holds the write lock, no other thread can grab the read or write lock. Multiple threads can hold the read lock at one time.
As with all Qore threading primitives, this class supports deadlock detection and throws exceptions when threading errors are encountered (for example, trying to free the read lock while holding the write lock, etc).
This read-write lock favors readers, so the read lock can be safely acquired recursively.
See the AutoReadLock and the AutoWriteLock classes for classes that assist in exception-safe RWLock locking.
Additionally, the on_exit statement can provide exception-safe RWLock handling at the lexical block level as in the following example:
Qore::Thread::RWLock::constructor | ( | ) |
Creates the RWLock object.
Qore::Thread::RWLock::copy | ( | ) |
Creates a new RWLock object, not based on the original.
Qore::Thread::RWLock::destructor | ( | ) |
Destroys the RWLock object.
Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and in each thread blocked on this object when it is deleted.
LOCK-ERROR | Object deleted while other threads blocked on it |
int Qore::Thread::RWLock::getReadWaiting | ( | ) |
Returns the number of threads waiting on the read lock.
int Qore::Thread::RWLock::getWriteWaiting | ( | ) |
Returns the number of threads waiting on the write lock.
bool Qore::Thread::RWLock::lockOwner | ( | ) |
int Qore::Thread::RWLock::numReaders | ( | ) |
Returns the read lock count.
nothing Qore::Thread::RWLock::readLock | ( | ) |
Acquires the read lock; blocks if the write lock is already acquired by another thread.
THREAD-DEADLOCK | A deadlock was detected while trying to acquire the lock |
LOCK-ERROR | RWLock::readLock() called while already holding the write lock, object deleted in another thread, etc. |
int Qore::Thread::RWLock::readLock | ( | timeout | timeout_ms | ) |
Acquires the read lock with a timeout value; blocks if the write lock is already acquired by another thread.
Returns 0 for success, non-zero for timeout; exceptions are thrown for other errors
timeout_ms | a timeout value to wait to acquire the read lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally (with a resolution of milliseconds) |
THREAD-DEADLOCK | A deadlock was detected while trying to acquire the lock |
LOCK-ERROR | RWLock::readLock() called while already holding the write lock, object deleted in another thread, etc. |
bool Qore::Thread::RWLock::readLockOwner | ( | ) |
nothing Qore::Thread::RWLock::readUnlock | ( | ) |
Decrements the read lock counter and releases the read lock if the counter is zero. If at least one thread is blocked trying to acquire the write lock and the read counter reaches zero, then one thread waiting on the write lock is woken up.
LOCK-ERROR | RWLock::readUnlock() called while not holding the read lock, object deleted in another thread, etc |
int Qore::Thread::RWLock::tryReadLock | ( | ) |
Acquires the read lock only if it can be acquired immediately.
int Qore::Thread::RWLock::tryWriteLock | ( | ) |
Acquires the write lock only if it can be acquired immediately.
int Qore::Thread::RWLock::writeLock | ( | timeout | timeout_ms | ) |
Acquires the write lock with a timeout value; blocks if the read lock is already acquired by another thread.
Returns 0 for success, non-zero for timeout; exceptions are thrown for other errors
timeout_ms | a timeout value to wait to acquire the write lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally (with a resolution of milliseconds) |
THREAD-DEADLOCK | A deadlock was detected while trying to acquire the lock |
LOCK-ERROR | RWLock::writeLock() called while already holding the read lock, object deleted in another thread, etc. |
nothing Qore::Thread::RWLock::writeLock | ( | ) |
Acquires the write lock; blocks if the read lock is already acquired by another thread.
THREAD-DEADLOCK | A deadlock was detected while trying to acquire the lock |
LOCK-ERROR | RWLock::writeLock() called while already holding the read lock, object deleted in another thread, etc. |
bool Qore::Thread::RWLock::writeLockOwner | ( | ) |
nothing Qore::Thread::RWLock::writeUnlock | ( | ) |
Releases the write lock, if any readers are waiting, wakes up all readers, otherwise if any writers are waiting, then wakes one up.
LOCK-ERROR | RWLock::writeUnlock() called while not holding the write lock, object deleted in another thread, etc |