Skip to main content

Mutex

Struct Mutex 

Source
pub struct Mutex<T: ?Sized> { /* private fields */ }
Expand description

A mutual-exclusion lock whose wait is a future.

Uncontended, try_lock and a guard’s drop are one atomic read-modify-write each and allocate nothing. A caller takes the lock whenever it is free, even while others wait; under contention lock spins briefly within the poll (see the module docs), then queues and returns Pending. An unlock wakes the oldest waiter to try again; a waiter that loses MAX_BYPASS times is handed the lock directly, and try_lock fails until it has had it.

It does not poison: a panic in a holder unlocks it.

use nuclei::sync::Mutex;

let counter = Mutex::new(0u32);
*counter.try_lock().expect("uncontended") += 1;
assert_eq!(counter.into_inner(), 1);

Implementations§

Source§

impl<T> Mutex<T>

Source

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

An unlocked mutex holding value.

Source

pub const fn const_new(value: T) -> Self

An unlocked mutex holding value, usable in a static.

Source

pub fn into_inner(self) -> T

Consumes the mutex and returns its value.

Source§

impl<T: ?Sized> Mutex<T>

Source

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

Locks the mutex if it is free and no handoff is owed.

§Errors

TryLockError when the mutex is locked or owed to a waiter.

Source

pub fn try_lock_owned( self: Arc<Self>, ) -> Result<OwnedMutexGuard<T>, TryLockError>

try_lock through an Arc, so the guard can outlive the borrow.

§Errors

TryLockError when the mutex is locked or owed to a waiter.

Source

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

Waits for the lock. Uncontended, the first poll is one load and one atomic read-modify-write. Contended, a poll may spin for up to the lock’s spin budget before it queues, and again after a retry it lost, before it returns Pending; it is lock-free, not wait-free: see Progress.

Dropping the future withdraws the wait, and passes on a lock handed to it meanwhile.

Source

pub fn lock_owned(self: Arc<Self>) -> LockOwned<T> ⓘ

lock through an Arc, so the guard can outlive the borrow.

Source

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

lock for a thread that may block: it parks the calling thread until the lock is free. On a nuclei shard (inside one of its tasks) it does not park the thread: it runs a nested block_on, so the shard keeps running its other tasks meanwhile. Inside another runtime’s task it blocks that task’s executor thread, so do not: the task that would unlock may be the one that thread was running.

§Panics

On wasm32, where no other thread can unlock, when the lock is held: the call could never end.

Source

pub fn blocking_lock_owned(self: Arc<Self>) -> OwnedMutexGuard<T>

blocking_lock through an Arc, so the guard can outlive the borrow.

§Panics

As for blocking_lock.

Source

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

The value, borrowed mutably; &mut self proves no guard exists.

Trait Implementations§

Source§

impl<T: ?Sized + Debug> Debug for Mutex<T>

Source§

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

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

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

Source§

fn default() -> Self

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

impl<T> From<T> for Mutex<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized + Send> Send for Mutex<T>

Source§

impl<T: ?Sized + Send> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T> !UnwindSafe for Mutex<T>

§

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

§

impl<T> UnsafeUnpin for Mutex<T>
where T: UnsafeUnpin + ?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.

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

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.