Struct spinlock::SpinLock [-] [+] [src]

pub struct SpinLock<T> {
    // some fields omitted
}

A Reader/Writer spinlock

A lock protects the underlying data of type T for shared memory concurent access. The lock semantic allows any number of concurent readers or alternatively at most one writer. A reader is given a reference to the protected data and is able to read it with the guarantee that the data will not change while it holds this reference. A writer is given a mutable reference to the protected data and is able to read and write it with the guarantee that the data will not change while it holds this reference.

The protected data must implement the markers Send and the marker Sync.

Methods

impl<T: Send + Sync> SpinLock<T>

fn new(data: T) -> SpinLock<T>

Create a new SpinLock wrapping the supplied data

fn write(&self) -> LockResult<SpinLockWriteGuard<T>>

Lock the SpinLock for exclusive access and returns a RAII guard which will drop the lock when it is dropped.

If another holder as paniced while holding a write lock on the same spinlock, the lock will be poisonned. In this case, write will fail with a PoisonError.

Panics

Panics if the thread is waiting more than MAX_WAIT nanoseconds

Examples

let spin = SpinLock::new(42);
{
    let data = spin.write().unwrap();
    *data += 1;
}
let data = spin.read().unwrap();
assert_eq!(*data, 43);

fn read(&self) -> LockResult<SpinLockReadGuard<T>>

Lock the SpinLock for shared access and returns a RAII guard which will drop the lock when it is dropped.

If another holder as paniced while holding a write lock on the same spinlock, the lock will be poisonned. In this case, write will fail with a PoisonError.

Panics

Panics if the thread is waiting more than MAX_WAIT nanoseconds

Examples

let spin = SpinLock::new(42);
let data = spin.read().unwrap();
assert_eq!(*data, 42);

fn try_read(&self) -> TryLockResult<SpinLockReadGuard<T>>

Attempt to acquire the lock with shared access.

This function will never spin, and will return immediatly if the access is contested. Returns a RAII guard if the access is successful, or TryLockError::WouldBlock if the access could not be granted.

If an exclusive holder has panic while holding the lock, the lock will be poisonned and Poisonned(PoisonError) will be returned.

Example

let spin = SpinLock::new(42);

match spin.try_read() {
    Ok(data) => assert_eq!(*data, 42),
    Err(TryLockError::WouldBlock) => (), // Sorry luke it's not your turn
    Err(Poisonned(_)) => panic!("Lock is poisonned"),
}

fn try_write(&self) -> TryLockResult<SpinLockWriteGuard<T>>

Attempt to acquire the lock with exclusive access.

This function will never spin, and will return immediatly if the access is contested. Returns a RAII guard if the access is successful, or TryLockError::WouldBlock if the access could not be granted.

If an exclusive holder has panic while holding the lock, the lock will be poisonned and Poisonned(PoisonError) will be returned.

Example

let spin = SpinLock::new(42);

match spin.try_wirte() {
    Ok(data) => {
        assert_eq!(*data, 42);
        *data += 1;
        assert_eq(*data, 43);
    },
    Err(TryLockError::WouldBlock) => (), // Sorry luke it's not your turn
    Err(Poisonned(_)) => panic!("Lock is poisonned"),
}

impl<T> SpinLock<T>

fn is_poisonned(&self) -> bool

Check if the lock is poisonned

Trait Implementations

impl<T: Send + Sync> Send for SpinLock<T>

impl<T: Send + Sync> Sync for SpinLock<T>