spin::mutex::spin

Struct SpinMutex

source
pub struct SpinMutex<T: ?Sized, R = Spin> { /* private fields */ }
Expand description

A spin lock providing mutually exclusive access to data.

§Example

use spin;

let lock = spin::mutex::SpinMutex::<_>::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::SpinMutex::<_>::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§

source§

impl<T, R> SpinMutex<T, R>

source

pub const fn new(data: T) -> Self

Creates a new SpinMutex wrapping the supplied data.

§Example
use spin::mutex::SpinMutex;

static MUTEX: SpinMutex<()> = SpinMutex::<_>::new(());

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

pub fn into_inner(self) -> T

Consumes this SpinMutex and unwraps the underlying data.

§Example
let lock = spin::mutex::SpinMutex::<_>::new(42);
assert_eq!(42, lock.into_inner());
source

pub fn as_mut_ptr(&self) -> *mut T

Returns a mutable pointer to the underlying data.

This is mostly meant to be used for applications which require manual unlocking, but where storing both the lock and the pointer to the inner data gets inefficient.

§Example
let lock = spin::mutex::SpinMutex::<_>::new(42);

unsafe {
    core::mem::forget(lock.lock());

    assert_eq!(lock.as_mut_ptr().read(), 42);
    lock.as_mut_ptr().write(58);

    lock.force_unlock();
}

assert_eq!(*lock.lock(), 58);
source§

impl<T: ?Sized, R: RelaxStrategy> SpinMutex<T, R>

source

pub fn lock(&self) -> SpinMutexGuard<'_, T>

Locks the SpinMutex 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::SpinMutex::<_>::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
}
source§

impl<T: ?Sized, R> SpinMutex<T, R>

source

pub fn is_locked(&self) -> bool

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.

source

pub unsafe fn force_unlock(&self)

Force unlock this SpinMutex.

§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.

source

pub fn try_lock(&self) -> Option<SpinMutexGuard<'_, T>>

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

§Example
let lock = spin::mutex::SpinMutex::<_>::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());
source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

Since this call borrows the SpinMutex 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::SpinMutex::<_>::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.lock(), 10);

Trait Implementations§

source§

impl<T: ?Sized + Debug, R> Debug for SpinMutex<T, R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized + Default, R> Default for SpinMutex<T, R>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T, R> From<T> for SpinMutex<T, R>

source§

fn from(data: T) -> Self

Converts to this type from the input type.
source§

impl<R: RelaxStrategy> RawMutex for SpinMutex<(), R>

source§

const INIT: Self = _

Initial value for an unlocked mutex.
source§

type GuardMarker = GuardSend

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

fn lock(&self)

Acquires this mutex, blocking the current thread until it is able to do so.
source§

fn try_lock(&self) -> bool

Attempts to acquire this mutex without blocking. Returns true if the lock was successfully acquired and false otherwise.
source§

unsafe fn unlock(&self)

Unlocks this mutex. Read more
source§

fn is_locked(&self) -> bool

Checks whether the mutex is currently locked.
source§

impl<T: ?Sized + Send, R> Send for SpinMutex<T, R>

source§

impl<T: ?Sized + Send, R> Sync for SpinMutex<T, R>

Auto Trait Implementations§

§

impl<T, R = Spin> !Freeze for SpinMutex<T, R>

§

impl<T, R = Spin> !RefUnwindSafe for SpinMutex<T, R>

§

impl<T, R> Unpin for SpinMutex<T, R>
where R: Unpin, T: Unpin + ?Sized,

§

impl<T, R> UnwindSafe for SpinMutex<T, R>
where R: UnwindSafe, T: UnwindSafe + ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.