4.26. Thread::RWLock Class

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

RWLock::constructor()

N

Creates the RWLock object.

RWLock::destructor()

Y

Destroys the RWLock object.

RWLock::copy()

N

Creates a new RWLock object, not based on the original.

nothing RWLock::readLock()

int RWLock::readLock(timeout $timeout_ms)

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.

nothing RWLock::readUnlock()

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.

nothing RWLock::writeLock()

int RWLock::writeLock(timeout $timeout_ms)

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.

nothing RWLock::writeUnlock()

Y

Releases the write lock, if any writers are waiting, then wakes one up, otherwise if any readers are waiting, wakes up all readers.

int RWLock::tryReadLock()

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.

int RWLock::tryWriteLock()

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.

int RWLock::numReaders()

N

Returns the read lock count.

int RWLock::getReadWaiting()

N

Returns the number of threads waiting on the read lock.

int RWLock::getWriteWaiting()

N

Returns the number of threads waiting on the write lock.


4.26.1. RWLock::constructor()

Synopsis

Creates the RWLock object.

Prototype

RWLock::constructor()

Example
my RWLock $rwl();

4.26.2. RWLock::destructor()

Synopsis

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.

Example
delete $rwl;

Table 4.761. Exceptions Thrown by RWLock::destructor()

err

desc

LOCK-ERROR

Object deleted while other threads blocked on it.


4.26.3. RWLock::copy()

Synopsis

Creates a new RWLock object, not based on the original.

Example
my RWLock $new_rwl = $rwl.copy();

4.26.4. RWLock::readLock()

Synopsis

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.

Prototype

nothing RWLock::readLock()

int RWLock::readLock(timeout $timeout_ms)

Example
$rwl.readLock();

Table 4.762. Arguments for RWLock::readLock()

Argument

Description

[timeout $timeout_ms]

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.763. Return Values for RWLock::readLock()

Return Type

Description

nothing or int

Only variants accepting a timeout return a value; they return 0 for success, -1 for timeout


Table 4.764. Exceptions Thrown by RWLock::readLock()

err

desc

THREAD-DEADLOCK

A deadlock was detected while trying to acquire the lock.

LOCK-ERROR

readLock() called while already holding the write lock, object deleted in another thread, etc.


4.26.5. RWLock::readUnlock()

Synopsis

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.

Prototype

nothing RWLock::readUnlock()

Example
$rwl.readUnlock();

Table 4.765. Exceptions Thrown by RWLock::readUnlock()

err

desc

LOCK-ERROR

readUnlock() called while not holding the read lock, object deleted in another thread, etc.


4.26.6. RWLock::writeLock()

Synopsis

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().

Prototype

nothing RWLock::writeLock()

int RWLock::writeLock(timeout $timeout_ms)

Example
$rwl.writeLock();

Table 4.766. Arguments for RWLock::writeLock()

Argument

Description

[timeout $timeout_ms]

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.767. Return Values for RWLock::writeLock()

Return Type

Description

nothing or int

Only variants accepting a timeout return a value; they return 0 for success, -1 for timeout


Table 4.768. Exceptions Thrown by RWLock::writeLock()

err

desc

THREAD-DEADLOCK

A deadlock was detected while trying to acquire the lock.

LOCK-ERROR

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.


4.26.7. RWLock::writeUnlock()

Synopsis

Releases the write lock, if any writers are waiting, then wakes one up, otherwise if any readers are waiting, wakes up all readers.

Prototype

nothing RWLock::writeUnlock()

Example
$rwl.writeUnlock();

Table 4.769. Exceptions Thrown by RWLock::writeUnlock()

err

desc

LOCK-ERROR

writeUnlock() called while not holding the write lock, object deleted in another thread, etc.


4.26.8. RWLock::tryReadLock()

Synopsis

Acquires the read lock only if it can be acquired immediately, returns 0 for success, -1 if it would block.

Prototype

int RWLock::tryReadLock()

Example
my int $i = $rwl.tryReadLock();

Table 4.770. Return Values for RWLock::writeUnlock()

Return Type

Description

int

Returns 0 for success (read lock acquired, read lock count incremented) or -1 if the call would block.


4.26.9. RWLock::tryWriteLock()

Synopsis

Acquires the write lock only if it can be acquired immediately, returns 0 for success, -1 if it would block.

Prototype

int RWLock::tryWriteLock()

Example
my int $i = $rwl.tryWriteLock();

Table 4.771. Return Values for RWLock::tryWriteLock()

Return Type

Description

int

Returns 0 for success (write lock acquired) or -1 if the call would block.


4.26.10. RWLock::numReaders()

Synopsis

Returns the read lock count.

Prototype

int RWLock::numReaders()

Example
$num = $rwl.numReaders();

Table 4.772. Return Values for RWLock::numReaders()

Return Type

Description

int

The read lock count.


4.26.11. RWLock::getReadWaiting()

Synopsis

Returns the number of threads blocked on the read lock (only non-zero while the write lock is held).

Prototype

int RWLock::getReadWaiting()

Example
$num = $rwl.getReadWaiting();

Table 4.773. Return Values for RWLock::getReadWaiting()

Return Type

Description

int

The number of threads blocked on the read lock.


4.26.12. RWLock::getWriteWaiting()

Synopsis

Returns the number of threads blocked on the write lock.

Prototype

int RWLock::getWriteWaiting()

Example
$num = $rwl.getWriteWaiting();

Table 4.774. Return Values for RWLock::getWriteWaiting()

Return Type

Description

int

The number of threads blocked on the write lock.