Skip to content

Commit

Permalink
Merge pull request #280 from coolreader18/remutex-is_owned
Browse files Browse the repository at this point in the history
Add [Raw]ReentrantMutex::is_owned_by_current_thread
  • Loading branch information
Amanieu authored Feb 25, 2021
2 parents f3ea9e2 + 391cf55 commit 2b3032f
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 2b3032f

Please sign in to comment.