Struct ReentrantMutex

pub struct ReentrantMutex<R, G, T: ?Sized> { /* private fields */ }

A mutex which can be recursively locked by a single thread.

This type is identical to Mutex except for the following points:

See Mutex for more details about the underlying mutex primitive.

Implementations

impl<R, G, T> ReentrantMutex<R, G, T>

const fn from_raw(raw_mutex: R, get_thread_id: G, val: T) -> ReentrantMutex<R, G, T>

Creates a new reentrant mutex based on a pre-existing raw mutex and a helper to get the thread ID.

const fn const_new(raw_mutex: R, get_thread_id: G, val: T) -> ReentrantMutex<R, G, T>

Creates a new reentrant mutex based on a pre-existing raw mutex and a helper to get the thread ID.

This allows creating a reentrant mutex in a constant context on stable Rust.

This method is a legacy alias for from_raw.

impl<R: RawMutex, G: GetThreadId, T> ReentrantMutex<R, G, T>

const fn new(val: T) -> ReentrantMutex<R, G, T>

Creates a new reentrant mutex in an unlocked state ready for use.

fn into_inner(self) -> T

Consumes this mutex, returning the underlying data.

impl<R: RawMutex, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T>

unsafe fn make_guard_unchecked(&self) -> ReentrantMutexGuard<'_, R, G, T>

Creates a new ReentrantMutexGuard without checking if the lock is held.

Safety

This method must only be called if the thread logically holds the lock.

Calling this function when a guard has already been produced is undefined behaviour unless the guard was forgotten with mem::forget.

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

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

If the mutex is held by another thread then this function will block the local thread until it is available to acquire the mutex. If the mutex is already held by the current thread then this function will increment the lock reference count and return immediately. Upon returning, the thread is the only thread with the mutex 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.

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

Attempts to acquire this lock.

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 does not block.

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

Returns a mutable reference to the underlying data.

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

fn is_locked(&self) -> bool

Checks whether the mutex is currently locked.

fn is_owned_by_current_thread(&self) -> bool

Checks whether the mutex is currently held by the current thread.

unsafe fn force_unlock(&self)

Forcibly unlocks the mutex.

This is useful when combined with mem::forget to hold a lock without the need to maintain a ReentrantMutexGuard object alive, for example when dealing with FFI.

Safety

This method must only be called if the current thread logically owns a ReentrantMutexGuard but that guard has be discarded using mem::forget. Behavior is undefined if a mutex is unlocked when not locked.

unsafe fn raw(&self) -> &R

Returns the underlying raw mutex object.

Note that you will most likely need to import the RawMutex trait from lock_api to be able to call functions on the raw mutex.

Safety

This method is unsafe because it allows unlocking a mutex while still holding a reference to a ReentrantMutexGuard.

fn data_ptr(&self) -> *mut T

Returns a raw pointer to the underlying data.

This is useful when combined with mem::forget to hold a lock without the need to maintain a ReentrantMutexGuard object alive, for example when dealing with FFI.

Safety

You must ensure that there are no data races when dereferencing the returned pointer, for example if the current thread logically owns a ReentrantMutexGuard but that guard has been discarded using mem::forget.

impl<R: RawMutexFair, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T>

unsafe fn force_unlock_fair(&self)

Forcibly unlocks the mutex using a fair unlock protocol.

This is useful when combined with mem::forget to hold a lock without the need to maintain a ReentrantMutexGuard object alive, for example when dealing with FFI.

Safety

This method must only be called if the current thread logically owns a ReentrantMutexGuard but that guard has be discarded using mem::forget. Behavior is undefined if a mutex is unlocked when not locked.

impl<R: RawMutexTimed, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T>

fn try_lock_for(&self, timeout: R::Duration) -> Option<ReentrantMutexGuard<'_, R, G, T>>

Attempts to acquire this lock until a timeout is reached.

If the lock could not be acquired before the timeout expired, then None is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

fn try_lock_until(&self, timeout: R::Instant) -> Option<ReentrantMutexGuard<'_, R, G, T>>

Attempts to acquire this lock until a timeout is reached.

If the lock could not be acquired before the timeout expired, then None is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

Trait Implementations

impl<R: RawMutex + Send, G: GetThreadId + Send, T: ?Sized + Send> Send for ReentrantMutex<R, G, T>

impl<R: RawMutex + Sync, G: GetThreadId + Sync, T: ?Sized + Send> Sync for ReentrantMutex<R, G, T>

impl<R: RawMutex, G: GetThreadId, T> From<T> for ReentrantMutex<R, G, T>

fn from(t: T) -> ReentrantMutex<R, G, T>

impl<R: RawMutex, G: GetThreadId, T: ?Sized + Debug> Debug for ReentrantMutex<R, G, T>

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

impl<R: RawMutex, G: GetThreadId, T: ?Sized + Default> Default for ReentrantMutex<R, G, T>

fn default() -> ReentrantMutex<R, G, T>

Auto Trait Implementations

impl<R, G, T> !Freeze for ReentrantMutex<R, G, T>

impl<R, G, T> !RefUnwindSafe for ReentrantMutex<R, G, T>

impl<R, G, T> Unpin for ReentrantMutex<R, G, T> where RawReentrantMutex<R, G>: Unpin, UnsafeCell<T>: Unpin, T: ?Sized,

impl<R, G, T> UnsafeUnpin for ReentrantMutex<R, G, T> where RawReentrantMutex<R, G>: UnsafeUnpin, UnsafeCell<T>: UnsafeUnpin, T: ?Sized,

impl<R, G, T> UnwindSafe for ReentrantMutex<R, G, T> where RawReentrantMutex<R, G>: UnwindSafe, UnsafeCell<T>: UnwindSafe, T: ?Sized,

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for ReentrantMutex<R, G, T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for ReentrantMutex<R, G, T> where T: ?Sized,

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

impl<T> From<T> for ReentrantMutex<R, G, T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for ReentrantMutex<R, G, T> where U: From<T>,

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, U> TryFrom<U> for ReentrantMutex<R, G, T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for ReentrantMutex<R, G, T> where U: TryFrom<T>,

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