Struct extra::sync::RWLock

pub struct RWLock {
    priv order_lock: Semaphore,
    priv access_lock: Sem<~[WaitQueue]>,
    priv state: std::unstable::sync::UnsafeArc,
}

A blocking, no-starvation, reader-writer lock with an associated condvar.

Failure

A task which fails while holding an rwlock will unlock the rwlock as it unwinds.

Methods

impl RWLock

fn new() -> RWLock

Create a new rwlock, with one associated condvar.

fn new_with_condvars(num_condvars: uint) -> RWLock

Create a new rwlock, with a specified number of associated condvars. Similar to mutex_with_condvars.

fn clone(&self) -> RWLock

Create a new handle to the rwlock.

fn read<U>(&self, blk: &fn() -> U) -> U

Run a function with the rwlock in read mode. Calls to 'read' from other tasks may run concurrently with this one.

fn write<U>(&self, blk: &fn() -> U) -> U

Run a function with the rwlock in write mode. No calls to 'read' or 'write' from other tasks will run concurrently with this one.

fn write_cond<U>(&self, blk: &fn(c: &Condvar) -> U) -> U

As write(), but also with a handle to a condvar. Waiting on this condvar will allow readers and writers alike to take the rwlock before the waiting task is signalled. (Note: a writer that waited and then was signalled might reacquire the lock before other waiting writers.)

fn write_downgrade<U>(&self, blk: &fn(v: RWLockWriteMode) -> U) -> U

As write(), but with the ability to atomically 'downgrade' the lock; i.e., to become a reader without letting other writers get the lock in the meantime (such as unlocking and then re-locking as a reader would do). The block takes a "write mode token" argument, which can be transformed into a "read mode token" by calling downgrade(). Example:

Example

do lock.write_downgrade |mut write_token| {
    do write_token.write_cond |condvar| {
        ... exclusive access ...
    }
    let read_token = lock.downgrade(write_token);
    do read_token.read {
        ... shared access ...
    }
}

fn downgrade<'a>(&self, token: RWLockWriteMode<'a>) -> RWLockReadMode<'a>

To be called inside of the write_downgrade block.