From fb1840adc3acfd0af45dc137458374a85ea6a721 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 --- 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..386fd383 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(&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(&self) -> bool { + self.raw.is_owned() + } + /// Forcibly unlocks the mutex. /// /// This is useful when combined with `mem::forget` to hold a lock without