pub struct ReentrantMutex<T> {
    mutex: Mutex,
    owner: AtomicUsize,
    lock_count: UnsafeCell<u32>,
    data: T,
}
Expand description

A reentrant mutual exclusion

This mutex will block other threads waiting for the lock to become available. The thread which has already locked the mutex can lock it multiple times without blocking, preventing a common source of deadlocks.

This is used by stdout().lock() and friends.

Implementation details

The ‘owner’ field tracks which thread has locked the mutex.

We use current_thread_unique_ptr() as the thread identifier, which is just the address of a thread local variable.

If owner is set to the identifier of the current thread, we assume the mutex is already locked and instead of locking it again, we increment lock_count.

When unlocking, we decrement lock_count, and only unlock the mutex when it reaches zero.

lock_count is protected by the mutex and only accessed by the thread that has locked the mutex, so needs no synchronization.

owner can be checked by other threads that want to see if they already hold the lock, so needs to be atomic. If it compares equal, we’re on the same thread that holds the mutex and memory access can use relaxed ordering since we’re not dealing with multiple threads. If it’s not equal, synchronization is left to the mutex, making relaxed memory ordering for the owner field fine in all cases.

Fields§

§mutex: Mutex§owner: AtomicUsize§lock_count: UnsafeCell<u32>§data: T

Implementations§

source§

impl<T> ReentrantMutex<T>

source

pub const fn new(t: T) -> ReentrantMutex<T>

Creates a new reentrant mutex in an unlocked state.

source

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

Acquires a mutex, blocking the current thread until it is able to do so.

This function will block the caller until it is available to acquire the mutex. Upon returning, the thread is the only thread with the mutex held. When the thread calling this method already holds the lock, the call shall succeed without blocking.

Errors

If another user of this mutex panicked while holding the mutex, then this call will return failure if the mutex would otherwise be acquired.

source

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

Attempts to acquire this lock.

If the lock could not be acquired at this time, then Err is returned. Otherwise, an RAII guard is returned.

This function does not block.

Errors

If another user of this mutex panicked while holding the mutex, then this call will return failure if the mutex would otherwise be acquired.

source

unsafe fn increment_lock_count(&self)

Trait Implementations§

Auto Trait Implementations§

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.