Struct pairlock::PairLock[][src]

pub struct PairLock<T> { /* fields omitted */ }

A reader-writer lock with wait-free reads.

Does not have poisoning.

Examples

let ac = Arc::new(PairLock::new_arc(load_config()));
let ac2 = ac.clone();
thread::spawn(move|| {
    loop {
        use_config(ac2.get());;
    }
});
loop {
    thread::sleep(Duration::from_secs(60));
    ac.set(Arc::new(load_config()));
}

Methods

impl<T> PairLock<T>
[src]

Creates a new PairLock.

Creates a new PairLock with init as the active value and T's default value as the inactive.

Creates a new PairLock with init as the active value and its .clone() as the inactive.

View the active value of this PairLock inside a closure.

Views should be short-lived to avoid blocking subsequent updates.

Reads must be performed inside a closure, because preventing memory unsafety in the face of repeated mem::forget()s of a read guard is non-trivial.

Will never block in any way, and should run in constant time.

Returns a clone of the active value.

Will never block in any way, and should run in constant time.

Locks the inactive value, giving exclusive access to it through a RAII guard that will make it active when the guard is dropped.

Will block the thread waiting for reads of the inactive value or other updates to finish.

Panicing while holding the guard does not poison the lock.

Examples

Using the lock as a counter

let counter = PairLock::with_default(1);
let mut guard = counter.update();
*guard = UpdateGuard::active(&guard) + 1;
drop(guard);
assert_eq!(counter.read(), 2);

Reusing an allocation while updating

let lock = PairLock::with_default(vec!["foo","bar"]);
let mut guard = lock.update();
{
    let (mutable, active) = UpdateGuard::both(&mut guard);
    mutable.clone_from(active);
    mutable.push("baz");
}
drop(guard);
lock.view(|v| assert_eq!(v[..], ["foo","bar","baz"][..]) );

Doing nothing with the guard, and still changing the value of the lock:

let lock = PairLock::new("foo", "bar");
assert_eq!(lock.read(), "foo");
let _ = lock.update();
assert_eq!(lock.read(), "bar");

Attempts to lock the inactive value, giving exclusive access to it through a RAII guard that will make it active when the guard is dropped.

Errors

Returns an error instead of blocking the thread.
The error tells which phase of aquiring the update lock that failed.

Examples

let pl = PairLock::new(String::new(), String::new());
let _guard = pl.try_update().unwrap();
assert_eq!(pl.try_update(), Err(TryUpdateError::OtherUpdate));

Stores a new value in the PairLock, returning the previously inactive value.

Will block if another update/replace/set is in progress. if there are reads of the second last value that haven't finished yet.

Consumes the PairLock and returns the active and inactive values.

Examples

let lock = PairLock::new(true, false);
let (active, inactive) = lock.into_inner();

Given exclusive access this method returns mutable references to both the active and inactive value.

Examples

let mut lock = PairLock::new(true, false);
let (&mut active, &mut inactive) = lock.get_mut_both();

Given exclusive access this method returns a mutable reference to the active value.

Given exclusive access this method returns a mutable reference to the inactive value.

impl<T> PairLock<Arc<T>>
[src]

Puts value into an Arc<T> and creates a new PairLock<Arc<T>> with it.

impl<T: ?Sized> PairLock<Arc<T>>
[src]

Returns a clone of the active Arc<T>.

Will never block in any way, and should run in constant time.

impl<T: Copy> PairLock<T>
[src]

Returns a copy of the active value.

Will never block in any way, and should run in constant time.

Trait Implementations

impl<T: Send> Send for PairLock<T>
[src]

impl<T: Send + Sync> Sync for PairLock<T>
[src]

T must be Send because a shared reference can replace stored values.

impl<T: Debug> Debug for PairLock<T>
[src]

Formats the value using the given formatter. Read more

impl<T: Default> Default for PairLock<T>
[src]

Returns the "default value" for a type. Read more

impl<T: Clone> Clone for PairLock<T>
[src]

Returns a new PairLock initialized with the current Arc in self.

Does not clone the content of the Arc.

Performs copy-assignment from source. Read more