Skip to main content

ReentrantMutex

Struct ReentrantMutex 

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

A mutex the owner holding it can lock again without waiting.

Each lock by the holding owner deepens a count; the mutex unlocks when the last of its guards drops, and then wakes the oldest waiting owner, with the bounded bypass of Mutex.

A guard gives &T, since two guards of one owner are alive at once; mutate through a Cell or RefCell inside. Guards and lock futures borrow their Owner, so they stay on its thread (see the Owner docs): a task that holds or awaits one is not Send meanwhile.

use nuclei::sync::{Owner, ReentrantMutex};
use std::cell::Cell;

let lock = ReentrantMutex::new(Cell::new(0));
let me = Owner::new();
let a = lock.try_lock(&me).expect("free");
let b = lock.try_lock(&me).expect("re-entered");
b.set(a.get() + 1);
drop((a, b));
assert_eq!(lock.into_inner().get(), 1);

Implementations§

Source§

impl<T> ReentrantMutex<T>

Source

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

An unlocked mutex holding value.

Source

pub fn into_inner(self) -> T

Consumes the mutex and returns its value.

Source§

impl<T: ?Sized> ReentrantMutex<T>

Source

pub fn try_lock<'a>( &'a self, owner: &'a Owner, ) -> Option<ReentrantMutexGuard<'a, T>>

Locks the mutex if owner holds it, or if it is free and no handoff is owed.

Source

pub fn lock<'a>(&'a self, owner: &'a Owner) -> ReentrantMutexLock<'a, T> ⓘ

Waits for the lock, at once if owner already holds it. A contended wait is lock-free, not wait-free: see Progress.

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

Source§

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

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

impl<T: Default> Default for ReentrantMutex<T>

Source§

fn default() -> Self

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

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

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for ReentrantMutex<T>

§

impl<T> !RefUnwindSafe for ReentrantMutex<T>

§

impl<T> !UnwindSafe for ReentrantMutex<T>

§

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

§

impl<T> UnsafeUnpin for ReentrantMutex<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<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.