Skip to content
Snippets Groups Projects
Commit 8e8443d1 authored by Eemeli's avatar Eemeli
Browse files

async lock is_locked

parent 4a4b70dc
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,10 @@ impl<T: ?Sized> Mutex<T> { ...@@ -32,6 +32,10 @@ impl<T: ?Sized> Mutex<T> {
self.value.get_mut() self.value.get_mut()
} }
pub fn is_locked(&self) -> bool {
self.lock.is_locked()
}
pub fn try_lock(&self) -> Option<MutexGuard<T>> { pub fn try_lock(&self) -> Option<MutexGuard<T>> {
if self.lock.try_lock() { if self.lock.try_lock() {
Some(unsafe { self.guard() }) Some(unsafe { self.guard() })
...@@ -109,6 +113,10 @@ impl Lock { ...@@ -109,6 +113,10 @@ impl Lock {
} }
} }
pub fn is_locked(&self) -> bool {
self.state.load(Ordering::Acquire) == LOCKED
}
pub fn try_lock(&self) -> bool { pub fn try_lock(&self) -> bool {
self.state self.state
.compare_exchange(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed) .compare_exchange(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed)
...@@ -121,7 +129,7 @@ impl Lock { ...@@ -121,7 +129,7 @@ impl Lock {
.compare_exchange(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed) .compare_exchange(UNLOCKED, LOCKED, Ordering::Acquire, Ordering::Relaxed)
.is_err() .is_err()
{ {
while self.state.load(Ordering::Acquire) == LOCKED { while self.is_locked() {
core::hint::spin_loop(); core::hint::spin_loop();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment