Note: This class is not available with the PO_NO_THREAD_CLASSES
parse option.
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.921. 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. | |
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. | |
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.
new RWLock()
$rwl = new RWLock();
Table 4.922. Arguments for RWLock::constructor()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.923. Return Values for RWLock::constructor()
Return Type | Description |
---|---|
RWLock Object | The object created. |
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 lvalue
delete $rwl;
Table 4.924. 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.
RWLock::copy()
$new_rwl = $rwl.copy();
Table 4.925. Arguments for RWLock::copy()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.926. Return Values for RWLock::copy()
Return Type | Description |
---|---|
RWLock Object | A new RWLock object, not based on the original. |
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.
RWLock::readLock([timeout_ms]
)
$rwl.readLock();
Table 4.927. Arguments for RWLock::readLock()
Argument | Type | Description |
---|---|---|
| Integer or Relative Date/Time | If an argument is present, it is interpreted as a timeout in milliseconds. Note that it's recommended to use a relative time value (i.e. |
Table 4.928. Return Values for RWLock::readLock()
Return Type | Description |
---|---|
Integer | 0 for success, -1 for failure (only returned with a timeout) |
Table 4.929. 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.
RWLock::readUnlock()
$rwl.readUnlock();
Table 4.930. Arguments for RWLock::readUnlock()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.931. Return Values for RWLock::readUnlock()
Return Type | Description |
---|---|
n/a | This method return no value. |
Table 4.932. 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().
RWLock::writeLock([timeout_ms]
)
$rwl.writeLock();
Table 4.933. Arguments for RWLock::writeLock()
Argument | Type | Description |
---|---|---|
| Integer or Relative Time | If an argument is present, it is interpreted as a timeout in milliseconds. Note that it's recommended to use a relative time value (i.e. |
Table 4.934. Return Values for RWLock::writeLock()
Return Type | Description |
---|---|
Integer | 0 for lock acquired, -1 for timeout (only when a timeout argument is passed) |
Table 4.935. 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.
RWLock::writeUnlock()
$rwl.writeUnlock();
Table 4.936. Arguments for RWLock::writeUnlock()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.937. Return Values for RWLock::writeUnlock()
Return Type | Description |
---|---|
n/a | This method return no value. |
Table 4.938. 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.
RWLock::tryReadLock()
$bool = $rwl.tryReadLock();
Table 4.939. Arguments for RWLock::writeUnlock()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.940. Return Values for RWLock::writeUnlock()
Return Type | Description |
---|---|
Integer | 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.
RWLock::tryWriteLock()
$bool = $rwl.tryWriteLock();
Table 4.941. Arguments for RWLock::tryWriteLock()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.942. Return Values for RWLock::tryWriteLock()
Return Type | Description |
---|---|
Integer | Returns 0 for success (write lock acquired) or -1 if the call would block. |
Returns the read lock count.
RWLock::numReaders()
$num = $rwl.numReaders();
Table 4.943. Arguments for RWLock::numReaders()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.944. Return Values for RWLock::numReaders()
Return Type | Description |
---|---|
Integer | The read lock count. |
Returns the number of threads blocked on the read lock (only non-zero while the write lock is held).
RWLock::getReadWaiting()
$num = $rwl.getReadWaiting();
Table 4.945. Arguments for RWLock::getReadWaiting()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.946. Return Values for RWLock::getReadWaiting()
Return Type | Description |
---|---|
Integer | The number of threads blocked on the read lock. |
Returns the number of threads blocked on the write lock.
RWLock::getWriteWaiting()
$num = $rwl.getWriteWaiting();
Table 4.947. Arguments for RWLock::getWriteWaiting()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.948. Return Values for RWLock::getWriteWaiting()
Return Type | Description |
---|---|
Integer | The number of threads blocked on the write lock. |