Struct extra::arc::RWArc

pub struct RWArc<T> {
    priv x: std::unstable::sync::UnsafeArc,
}

A dual-mode Arc protected by a reader-writer lock. The data can be accessed mutably or immutably, and immutably-accessing tasks may run concurrently.

Unlike mutex_arcs, rw_arcs are safe, because they cannot be nested.

Methods

impl<T: std::kinds::Freeze + std::kinds::Send> RWArc<T>

fn new(user_data: T) -> RWArc<T>

Create a reader/writer Arc with the supplied data.

fn new_with_condvars(user_data: T, num_condvars: uint) -> RWArc<T>

Create a reader/writer Arc with the supplied data and a specified number of condvars (as sync::RWLock::new_with_condvars).

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

Access the underlying data mutably. Locks the rwlock in write mode; other readers and writers will block.

Failure

Failing while inside the Arc will unlock the Arc while unwinding, so that other tasks won't block forever. As MutexArc.access, it will also poison the Arc, so subsequent readers and writers will both also fail.

fn write_cond<'x, 'c, U>(&self, blk: &fn(x: &'x mut T, c: &'c Condvar) -> U) -> U

As write(), but with a condvar, as sync::rwlock.write_cond().

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

Access the underlying data immutably. May run concurrently with other reading tasks.

Failure

Failing will unlock the Arc while unwinding. However, unlike all other access modes, this will not poison the Arc.

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

As write(), but with the ability to atomically 'downgrade' the lock. See sync::rwlock.write_downgrade(). The RWWriteMode token must be used to obtain the &mut T, and can be transformed into a RWReadMode token by calling downgrade(), after which a &T can be obtained instead.

Example

do arc.write_downgrade |mut write_token| {
    do write_token.write_cond |state, condvar| {
        ... exclusive access with mutable state ...
    }
    let read_token = arc.downgrade(write_token);
    do read_token.read |state| {
        ... shared access with immutable state ...
    }
}

fn downgrade<'a>(&self, token: RWWriteMode<'a, T>) -> RWReadMode<'a, T>

To be called inside of the write_downgrade block.

fn unwrap(self) -> T

Retrieves the data, blocking until all other references are dropped, exactly as arc::unwrap.

Will additionally fail if another task has failed while accessing the arc in write mode.

Trait Implementations

impl<T: std::kinds::Freeze + std::kinds::Send> std::clone::Clone for RWArc<T>

fn clone(&self) -> RWArc<T>

Duplicate a rwlock-protected Arc. See arc::clone for more details.