From 391cf555e94c05450cea2e88b42c95c6631ab163 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Wed, 17 Feb 2021 21:00:45 -0600 Subject: [PATCH] Add [Raw]ReentrantMutex::is_owned_by_current_thread --- lock_api/src/remutex.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lock_api/src/remutex.rs b/lock_api/src/remutex.rs index 09833b06..b49ec6c0 100644 --- a/lock_api/src/remutex.rs +++ b/lock_api/src/remutex.rs @@ -135,6 +135,13 @@ impl RawReentrantMutex { pub fn is_locked(&self) -> bool { self.mutex.is_locked() } + + /// Checks whether the mutex is currently held by the current thread. + #[inline] + pub fn is_owned_by_current_thread(&self) -> bool { + let id = self.get_thread_id.nonzero_thread_id().get(); + self.owner.load(Ordering::Relaxed) == id + } } impl RawReentrantMutex { @@ -333,6 +340,12 @@ impl ReentrantMutex { self.raw.is_locked() } + /// Checks whether the mutex is currently held by the current thread. + #[inline] + pub fn is_owned_by_current_thread(&self) -> bool { + self.raw.is_owned_by_current_thread() + } + /// Forcibly unlocks the mutex. /// /// This is useful when combined with `mem::forget` to hold a lock without