Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add [Raw]ReentrantMutex::is_owned #280

Merged
merged 1 commit into from Feb 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lock_api/src/remutex.rs
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