[][src]Struct spin::mutex::Mutex

pub struct Mutex<T: ?Sized, R = Spin> { /* fields omitted */ }

A spin-based lock providing mutually exclusive access to data.

The implementation uses either a ticket mutex or a regular spin mutex depending on whether the spin_mutex or ticket_mutex feature flag is enabled.

Example

use spin;

let lock = spin::Mutex::new(0);

// Modify the data
*lock.lock() = 2;

// Read the data
let answer = *lock.lock();
assert_eq!(answer, 2);

Thread safety example

use spin;
use std::sync::{Arc, Barrier};

let thread_count = 1000;
let spin_mutex = Arc::new(spin::Mutex::new(0));

// We use a barrier to ensure the readout happens after all writing
let barrier = Arc::new(Barrier::new(thread_count + 1));

for _ in (0..thread_count) {
    let my_barrier = barrier.clone();
    let my_lock = spin_mutex.clone();
    std::thread::spawn(move || {
        let mut guard = my_lock.lock();
        *guard += 1;

        // Release the lock to prevent a deadlock
        drop(guard);
        my_barrier.wait();
    });
}

barrier.wait();

let answer = { *spin_mutex.lock() };
assert_eq!(answer, thread_count);

Implementations

impl<T, R> Mutex<T, R>[src]

pub const fn new(value: T) -> Self[src]

Creates a new Mutex wrapping the supplied data.

Example

use spin::Mutex;

static MUTEX: Mutex<()> = Mutex::new(());

fn demo() {
    let lock = MUTEX.lock();
    // do something with lock
    drop(lock);
}

pub fn into_inner(self) -> T[src]

Consumes this Mutex and unwraps the underlying data.

Example

let lock = spin::Mutex::new(42);
assert_eq!(42, lock.into_inner());

impl<T: ?Sized, R: RelaxStrategy> Mutex<T, R>[src]

pub fn lock(&self) -> MutexGuard<'_, T>[src]

Locks the Mutex and returns a guard that permits access to the inner data.

The returned value may be dereferenced for data access and the lock will be dropped when the guard falls out of scope.

let lock = spin::Mutex::new(0);
{
    let mut data = lock.lock();
    // The lock is now locked and the data can be accessed
    *data += 1;
    // The lock is implicitly dropped at the end of the scope
}

impl<T: ?Sized, R> Mutex<T, R>[src]

pub fn is_locked(&self) -> bool[src]

Returns true if the lock is currently held.

Safety

This function provides no synchronization guarantees and so its result should be considered 'out of date' the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.

pub unsafe fn force_unlock(&self)[src]

Force unlock this Mutex.

Safety

This is extremely unsafe if the lock is not held by the current thread. However, this can be useful in some instances for exposing the lock to FFI that doesn't know how to deal with RAII.

pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>[src]

Try to lock this Mutex, returning a lock guard if successful.

Example

let lock = spin::Mutex::new(42);

let maybe_guard = lock.try_lock();
assert!(maybe_guard.is_some());

// `maybe_guard` is still held, so the second call fails
let maybe_guard2 = lock.try_lock();
assert!(maybe_guard2.is_none());

pub fn get_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the underlying data.

Since this call borrows the Mutex mutably, and a mutable reference is guaranteed to be exclusive in Rust, no actual locking needs to take place -- the mutable borrow statically guarantees no locks exist. As such, this is a 'zero-cost' operation.

Example

let mut lock = spin::Mutex::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.lock(), 10);

Trait Implementations

impl<T: ?Sized + Debug, R> Debug for Mutex<T, R>[src]

impl<T: ?Sized + Default, R> Default for Mutex<T, R>[src]

impl<T, R> From<T> for Mutex<T, R>[src]

impl<R: RelaxStrategy> RawMutex for Mutex<(), R>[src]

type GuardMarker = GuardSend

Marker type which determines whether a lock guard should be Send. Use one of the GuardSend or GuardNoSend helper types here. Read more

impl<T: ?Sized + Send, R> Send for Mutex<T, R>[src]

impl<T: ?Sized + Send, R> Sync for Mutex<T, R>[src]

Auto Trait Implementations

impl<T: ?Sized, R> Unpin for Mutex<T, R> where
    R: Unpin,
    T: Unpin
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.