libcortex_a9: added `try_lock` for mutex.

This commit is contained in:
pca006132 2020-07-15 16:41:28 +08:00
parent 191abf6b8f
commit 074438c3c7
1 changed files with 9 additions and 0 deletions

View File

@ -48,6 +48,15 @@ impl<T> Mutex<T> {
MutexGuard { mutex: self }
}
pub fn try_lock(&self) -> Option<MutexGuard<T>> {
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);