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

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

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

  • Locking multiple times from the same thread will work correctly instead of deadlocking.
  • ReentrantMutexGuard does not give mutable references to the locked data. Use a RefCell if you need this.

See Mutex for more details about the underlying mutex primitive.

Implementations§

§

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

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

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

pub fn into_inner(self) -> T

Consumes this mutex, returning the underlying data.

§

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

pub 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.

§

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

pub 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.

pub 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.

pub 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.

pub 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.

pub fn is_locked(&self) -> bool

Checks whether the mutex is currently locked.

pub fn is_owned_by_current_thread(&self) -> bool

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

pub 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.

pub 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.

pub 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, G, T> ReentrantMutex<R, G, T>
where R: RawMutexFair, G: GetThreadId, T: ?Sized,

pub 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, G, T> ReentrantMutex<R, G, T>
where R: RawMutexTimed, G: GetThreadId, T: ?Sized,

pub fn try_lock_for( &self, timeout: <R as RawMutexTimed>::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.

pub fn try_lock_until( &self, timeout: <R as RawMutexTimed>::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, G, T> Debug for ReentrantMutex<R, G, T>
where R: RawMutex, G: GetThreadId, T: Debug + ?Sized,

§

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

Formats the value using the given formatter. Read more
§

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

§

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

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

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

§

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

Converts to this type from the input type.
§

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

§

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

Auto Trait Implementations§

§

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

§

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

§

impl<R, G, T: ?Sized> UnwindSafe for ReentrantMutex<R, G, T>
where G: UnwindSafe, R: UnwindSafe, 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
§

impl<T> Any for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

§

fn type_name(&self) -> &'static str

§

impl<T> AnySync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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
§

impl<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
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.

§

impl<T> FromFd for T
where T: From<OwnedFd>,

§

fn from_fd(owned_fd: OwnedFd) -> T

👎Deprecated since 1.0.0: FromFd::from_fd is replaced by From<OwnedFd>::from
Constructs a new instance of Self from the given file descriptor. Read more
§

fn from_into_fd<Owned>(into_owned: Owned) -> Self
where Owned: Into<OwnedFd>, Self: Sized + From<OwnedFd>,

Constructs a new instance of Self from the given file descriptor converted from into_owned. Read more
§

impl<T> FromFilelike for T
where T: From<OwnedFd>,

§

fn from_filelike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given filelike object. Read more
§

fn from_into_filelike<Owned>(owned: Owned) -> T
where Owned: IntoFilelike,

Constructs a new instance of Self from the given filelike object converted from into_owned. Read more
§

impl<T> FromSocketlike for T
where T: From<OwnedFd>,

§

fn from_socketlike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given socketlike object.
§

fn from_into_socketlike<Owned>(owned: Owned) -> T
where Owned: IntoSocketlike,

Constructs a new instance of Self from the given socketlike object converted from into_owned.
source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> 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.

§

impl<T> LayoutRaw for T

§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Gets the layout of the type.
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
§

impl<T> Upcastable for T
where T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

upcast boxed dyn
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more