Struct mycelium_util::sync::spin::Mutex

source ·
pub struct Mutex<T> { /* private fields */ }
Expand description

A spinlock-based mutual exclusion lock for protecting shared data

This mutex will spin with an exponential backoff while waiting for the lock to become available. Each mutex has a type parameter which represents the data that it is protecting. The data can only be accessed through the RAII guards returned from lock and try_lock, which guarantees that the data is only ever accessed when the mutex is locked.

§Fairness

This is not a fair mutex.

§Loom-specific behavior

When cfg(loom) is enabled, this mutex will use Loom’s simulated atomics, checked UnsafeCell, and simulated spin loop hints.

Implementations§

source§

impl<T> Mutex<T>

source

pub const fn new(data: T) -> Mutex<T>

Available on non-loom only.

Returns a new Mutex protecting the provided data.

The returned Mutex is in an unlocked state, ready for use.

§Examples
use maitake_sync::spin::Mutex;

let mutex = Mutex::new(0);
source

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

Attempts to acquire this lock without spinning

If the lock could not be acquired at this time, then None is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

This function will never spin.

source

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

Acquires a mutex, spinning until it is locked.

This function will spin until the mutex is available to lock. Upon returning, the thread is the only thread with the lock held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.

source

pub unsafe fn force_unlock(&self)

Forcibly unlock the mutex.

If a lock is currently held, it will be released, regardless of who’s holding it. Of course, this is outrageously, disgustingly unsafe and you should never do it.

§Safety

This deliberately violates mutual exclusion.

Only call this method when it is guaranteed that no stack frame that has previously locked the mutex will ever continue executing. Essentially, this is only okay to call when the kernel is oopsing and all code running on other cores has already been killed.

source

pub fn into_inner(self) -> T

Consumes this Mutex, returning the guarded data.

source

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

Returns a mutable reference to the underlying data.

Since this call borrows the Mutex mutably, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist.

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

Trait Implementations§

source§

impl<T> Debug for Mutex<T>
where T: Debug,

source§

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

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

impl<T> Default for Mutex<T>
where T: Default,

source§

fn default() -> Mutex<T>

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

impl<T> Send for Mutex<T>
where T: Send,

source§

impl<T> Sync for Mutex<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T> Unpin for Mutex<T>
where T: Unpin,

§

impl<T> UnwindSafe for Mutex<T>
where T: UnwindSafe,

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<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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>,

§

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>,

§

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.