Skip to content

Commit

Permalink
Add ReentrantMutex::is_locked()
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed May 18, 2020
1 parent 607adf5 commit de2f68f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lock_api/src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl<R: RawMutex, G: GetThreadId> RawReentrantMutex<R, G> {
self.mutex.unlock();
}
}

#[inline]
fn is_locked(&self) -> bool {
let id = self.get_thread_id.nonzero_thread_id().get();
self.owner.load(Ordering::Relaxed) == id || self.mutex.is_locked()
}
}

impl<R: RawMutexFair, G: GetThreadId> RawReentrantMutex<R, G> {
Expand Down Expand Up @@ -272,6 +278,12 @@ impl<R: RawMutex, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
unsafe { &mut *self.data.get() }
}

/// Checks whether the mutex is currently locked.
#[inline]
pub fn is_locked(&self) -> bool {
self.raw.is_locked()
}

/// Forcibly unlocks the mutex.
///
/// This is useful when combined with `mem::forget` to hold a lock without
Expand Down

0 comments on commit de2f68f

Please sign in to comment.