Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.
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:
{
$rwl.writeLock();
on_exit
$rwl.writeUnlock();
# ... when this block exits the lock will be released, even in the
# case of return statements or exceptions
}
Table 4.760. RWLock Method Overview
Method | Except? | Description |
|---|---|---|
N | Creates the RWLock object. | |
Y | Destroys the RWLock object. | |
N | Creates a new RWLock object, not based on the original. | |
|
| Y | Acquires the read lock with an optional timeout value; blocks if the write lock is already acquired. Returns 0 for success, non-zero for timeout; exceptions are thrown for other errors. |
Y | 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. | |
|
| Y | Acquires the write lock with an optional timeout; blocks if either the read lock or write lock is already acquired. Returns 0 for success, non-zero for timeout; exceptions are thrown for other errors. |
Y | Releases the write lock, if any writers are waiting, then wakes one up, otherwise if any readers are waiting, wakes up all readers. | |
N | Acquires the read lock only if it can be acquired immediately. Returns 0 for success (read lock acquired, read lock count incremented) or -1 if the call would block. | |
N | Acquires the write lock only if it can be acquired immediately. Returns 0 for success (write lock acquired) or -1 if the call would block. | |
N | Returns the read lock count. | |
N | Returns the number of threads waiting on the read lock. | |
N | Returns the number of threads waiting on the write lock. |
Creates the RWLock object.
my RWLock $rwl();
Destroys the 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.
delete $rwl;
Table 4.761. Exceptions Thrown by RWLock::destructor()
err | desc |
|---|---|
| Object deleted while other threads blocked on it. |
Creates a new RWLock object, not based on the original.
my RWLock $new_rwl = $rwl.copy();
Acquires the read lock with an optional timeout value; blocks if the write lock is already acquired. The read lock may be acquired recursively, however, each call to RWLock::readLock() requires a corresponding call to RWLock::readUnlock(). An optional timeout value may be passed to this method, giving a time in milliseconds to wait for the lock to become free. Like all Qore functions and methods taking timeout values, a relative date/time value may be passed instead of an integer to make the timeout units clear.
int RWLock::readLock(timeout $timeout_ms)
$rwl.readLock();
Table 4.762. Arguments for RWLock::readLock()
Argument | Description |
|---|---|
| An optional timeout value to wait to acquire the read lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally. |
Table 4.764. Exceptions Thrown by RWLock::readLock()
err | desc |
|---|---|
| A deadlock was detected while trying to acquire the lock. |
| readLock() called while already holding the write lock, object deleted in another thread, etc. |
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.
$rwl.readUnlock();
Table 4.765. Exceptions Thrown by RWLock::readUnlock()
err | desc |
|---|---|
| readUnlock() called while not holding the read lock, object deleted in another thread, etc. |
Acquires the write lock; blocks if either the read lock or write lock is already acquired. An optional timeout value may be passed to this method, giving a time in milliseconds to wait for the lock to become free. Like all Qore functions and methods taking timeout values, a relative date/time value may be passed instead of an integer to make the timeout units clear.
To release the write lock, use RWLock::readUnlock().
int RWLock::writeLock(timeout $timeout_ms)
$rwl.writeLock();
Table 4.766. Arguments for RWLock::writeLock()
Argument | Description |
|---|---|
| An optional timeout value to wait to acquire the write lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally. |
Table 4.768. Exceptions Thrown by RWLock::writeLock()
err | desc |
|---|---|
| A deadlock was detected while trying to acquire the lock. |
| writeLock() called while holding the read lock, writeLock() called while already holding the write lock in the same thread, object deleted in another thread, etc. |
Releases the write lock, if any writers are waiting, then wakes one up, otherwise if any readers are waiting, wakes up all readers.
$rwl.writeUnlock();
Table 4.769. Exceptions Thrown by RWLock::writeUnlock()
err | desc |
|---|---|
| writeUnlock() called while not holding the write lock, object deleted in another thread, etc. |
Acquires the read lock only if it can be acquired immediately, returns 0 for success, -1 if it would block.
my int $i = $rwl.tryReadLock();
Table 4.770. Return Values for RWLock::writeUnlock()
Return Type | Description |
|---|---|
Returns 0 for success (read lock acquired, read lock count incremented) or -1 if the call would block. |
Acquires the write lock only if it can be acquired immediately, returns 0 for success, -1 if it would block.
my int $i = $rwl.tryWriteLock();
Table 4.771. Return Values for RWLock::tryWriteLock()
Return Type | Description |
|---|---|
Returns 0 for success (write lock acquired) or -1 if the call would block. |
Returns the read lock count.
$num = $rwl.numReaders();
Returns the number of threads blocked on the read lock (only non-zero while the write lock is held).
$num = $rwl.getReadWaiting();
Table 4.773. Return Values for RWLock::getReadWaiting()
Return Type | Description |
|---|---|
The number of threads blocked on the read lock. |