Skip to content

Commit

Permalink
Add [Raw]ReentrantMutex::is_owned_by_current_thread
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Feb 18, 2021
1 parent f3ea9e2 commit 391cf55
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lock_api/src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ impl<R: RawMutex, G: GetThreadId> RawReentrantMutex<R, G> {
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<R: RawMutexFair, G: GetThreadId> RawReentrantMutex<R, G> {
Expand Down Expand Up @@ -333,6 +340,12 @@ impl<R: RawMutex, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
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
Expand Down

0 comments on commit 391cf55

Please sign in to comment.