Struct extra::arc::MutexArc

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

An Arc with mutable data protected by a blocking mutex.

Methods

impl<T: std::kinds::Send> MutexArc<T>

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

Create a mutex-protected Arc with the supplied data.

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

Create a mutex-protected Arc with the supplied data and a specified number of condvars (as sync::Mutex::new_with_condvars).

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

Access the underlying mutable data with mutual exclusion from other tasks. The argument closure will be run with the mutex locked; all other tasks wishing to access the data will block until the closure finishes running.

The reason this function is 'unsafe' is because it is possible to construct a circular reference among multiple Arcs by mutating the underlying data. This creates potential for deadlock, but worse, this will guarantee a memory leak of all involved Arcs. Using MutexArcs inside of other Arcs is safe in absence of circular references.

If you wish to nest MutexArcs, one strategy for ensuring safety at runtime is to add a "nesting level counter" inside the stored data, and when traversing the arcs, assert that they monotonically decrease.

Failure

Failing while inside the Arc will unlock the Arc while unwinding, so that other tasks won't block forever. It will also poison the Arc: any tasks that subsequently try to access it (including those already blocked on the mutex) will also fail immediately.

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

As unsafe_access(), but with a condvar, as sync::mutex.lock_cond().

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.

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

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

As unsafe_access.

The difference between access and unsafe_access is that the former forbids mutexes to be nested. While unsafe_access can be used on MutexArcs without freezable interiors, this safe version of access requires the Freeze bound, which prohibits access on MutexArcs which might contain nested MutexArcs inside.

The purpose of this is to offer a safe implementation of MutexArc to be used instead of RWArc in cases where no readers are needed and sightly better performance is required.

Both methods have the same failure behaviour as unsafe_access and unsafe_access_cond.

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

As unsafe_access_cond but safe and Freeze.

Trait Implementations

impl<T: std::kinds::Send> std::clone::Clone for MutexArc<T>

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

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