Struct ReentrantMutex
pub struct ReentrantMutex<R, G, T>where
T: ?Sized,{
raw: RawReentrantMutex<R, G>,
data: UnsafeCell<T>,
}
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 aRefCell
if you need this.
See Mutex
for more details about the underlying mutex
primitive.
Fields§
§raw: RawReentrantMutex<R, G>
§data: UnsafeCell<T>
Implementations§
§impl<R, G, T> ReentrantMutex<R, G, T>where
R: RawMutex,
G: GetThreadId,
impl<R, G, T> ReentrantMutex<R, G, T>where
R: RawMutex,
G: GetThreadId,
pub const fn new(val: T) -> ReentrantMutex<R, G, T>
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
pub fn into_inner(self) -> T
Consumes this mutex, returning the underlying data.
§impl<R, G, T> ReentrantMutex<R, G, T>
impl<R, G, T> ReentrantMutex<R, G, T>
pub const fn from_raw(
raw_mutex: R,
get_thread_id: G,
val: T,
) -> ReentrantMutex<R, G, T>
pub 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.
pub const fn const_new(
raw_mutex: R,
get_thread_id: G,
val: 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.
This method is a legacy alias for from_raw
.
§impl<R, G, T> ReentrantMutex<R, G, T>
impl<R, G, T> ReentrantMutex<R, G, T>
pub unsafe fn make_guard_unchecked(&self) -> ReentrantMutexGuard<'_, R, G, T>
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>
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>>
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
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_owned_by_current_thread(&self) -> bool
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)
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
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
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
.
pub unsafe fn make_arc_guard_unchecked(
self: &Arc<ReentrantMutex<R, G, T>>,
) -> ArcReentrantMutexGuard<R, G, T>
pub unsafe fn make_arc_guard_unchecked( self: &Arc<ReentrantMutex<R, G, T>>, ) -> ArcReentrantMutexGuard<R, G, T>
Creates a new ArcReentrantMutexGuard
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_arc(
self: &Arc<ReentrantMutex<R, G, T>>,
) -> ArcReentrantMutexGuard<R, G, T>
pub fn lock_arc( self: &Arc<ReentrantMutex<R, G, T>>, ) -> ArcReentrantMutexGuard<R, G, T>
Acquires a reentrant mutex through an Arc
.
This method is similar to the lock
method; however, it requires the ReentrantMutex
to be inside of an
Arc
and the resulting mutex guard has no lifetime requirements.
pub fn try_lock_arc(
self: &Arc<ReentrantMutex<R, G, T>>,
) -> Option<ArcReentrantMutexGuard<R, G, T>>
pub fn try_lock_arc( self: &Arc<ReentrantMutex<R, G, T>>, ) -> Option<ArcReentrantMutexGuard<R, G, T>>
Attempts to acquire a reentrant mutex through an Arc
.
This method is similar to the try_lock
method; however, it requires the ReentrantMutex
to be inside
of an Arc
and the resulting mutex guard has no lifetime requirements.
§impl<R, G, T> ReentrantMutex<R, G, T>
impl<R, G, T> ReentrantMutex<R, G, T>
pub unsafe fn force_unlock_fair(&self)
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>
impl<R, G, T> ReentrantMutex<R, G, T>
pub fn try_lock_for(
&self,
timeout: <R as RawMutexTimed>::Duration,
) -> Option<ReentrantMutexGuard<'_, R, G, T>>
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>>
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.
pub fn try_lock_arc_for(
self: &Arc<ReentrantMutex<R, G, T>>,
timeout: <R as RawMutexTimed>::Duration,
) -> Option<ArcReentrantMutexGuard<R, G, T>>
pub fn try_lock_arc_for( self: &Arc<ReentrantMutex<R, G, T>>, timeout: <R as RawMutexTimed>::Duration, ) -> Option<ArcReentrantMutexGuard<R, G, T>>
Attempts to acquire this lock until a timeout is reached, through an Arc
.
This method is similar to the try_lock_for
method; however, it requires the ReentrantMutex
to be
inside of an Arc
and the resulting mutex guard has no lifetime requirements.
pub fn try_lock_arc_until(
self: &Arc<ReentrantMutex<R, G, T>>,
timeout: <R as RawMutexTimed>::Instant,
) -> Option<ArcReentrantMutexGuard<R, G, T>>
pub fn try_lock_arc_until( self: &Arc<ReentrantMutex<R, G, T>>, timeout: <R as RawMutexTimed>::Instant, ) -> Option<ArcReentrantMutexGuard<R, G, T>>
Attempts to acquire this lock until a timeout is reached, through an Arc
.
This method is similar to the try_lock_until
method; however, it requires the ReentrantMutex
to be
inside of an Arc
and the resulting mutex guard has no lifetime requirements.
Trait Implementations§
§impl<R, G, T> Debug for ReentrantMutex<R, G, T>
impl<R, G, T> Debug for ReentrantMutex<R, G, T>
§impl<R, G, T> Default for ReentrantMutex<R, G, T>
impl<R, G, T> Default for ReentrantMutex<R, G, T>
§fn default() -> ReentrantMutex<R, G, T>
fn default() -> ReentrantMutex<R, G, T>
§impl<'de, R, G, T> Deserialize<'de> for ReentrantMutex<R, G, T>
impl<'de, R, G, T> Deserialize<'de> for ReentrantMutex<R, G, T>
§fn deserialize<D>(
deserializer: D,
) -> Result<ReentrantMutex<R, G, T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<ReentrantMutex<R, G, T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<R, G, T> From<T> for ReentrantMutex<R, G, T>where
R: RawMutex,
G: GetThreadId,
impl<R, G, T> From<T> for ReentrantMutex<R, G, T>where
R: RawMutex,
G: GetThreadId,
§fn from(t: T) -> ReentrantMutex<R, G, T>
fn from(t: T) -> ReentrantMutex<R, G, T>
§impl<R, G, T> Serialize for ReentrantMutex<R, G, T>
impl<R, G, T> Serialize for ReentrantMutex<R, G, T>
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<R, G, T> Send for ReentrantMutex<R, G, T>
impl<R, G, T> Sync for 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>
impl<R, G, T> UnwindSafe for ReentrantMutex<R, G, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
§impl<T> NoneValue for Twhere
T: Default,
impl<T> NoneValue for Twhere
T: Default,
type NoneType = T
§fn null_value() -> T
fn null_value() -> T
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.