From 074438c3c7becd30a08a59490754f3120a0650f0 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Wed, 15 Jul 2020 16:41:28 +0800 Subject: [PATCH] libcortex_a9: added `try_lock` for mutex. --- libcortex_a9/src/mutex.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libcortex_a9/src/mutex.rs b/libcortex_a9/src/mutex.rs index 9d3c6c6..8d84105 100644 --- a/libcortex_a9/src/mutex.rs +++ b/libcortex_a9/src/mutex.rs @@ -48,6 +48,15 @@ impl Mutex { MutexGuard { mutex: self } } + pub fn try_lock(&self) -> Option> { + if self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::Acquire) != UNLOCKED { + None + } else { + dmb(); + Some(MutexGuard { mutex: self }) + } + } + fn unlock(&self) { dmb(); self.locked.store(UNLOCKED, Ordering::Release);