From d89f594ba4ef48eed4aecbd761c9e3641d7b5362 Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 18 Nov 2019 02:37:59 +0100 Subject: [PATCH] cortex_a9::mutex: use AtomU32, remove powersaving behavior Mutex works properly now. --- src/cortex_a9/mutex.rs | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/cortex_a9/mutex.rs b/src/cortex_a9/mutex.rs index 461a3d2..e3fda3d 100644 --- a/src/cortex_a9/mutex.rs +++ b/src/cortex_a9/mutex.rs @@ -1,21 +1,13 @@ use core::ops::{Deref, DerefMut}; -use core::sync::atomic::{AtomicBool, Ordering}; +use core::sync::atomic::{AtomicU32, Ordering}; use core::cell::UnsafeCell; use super::asm::*; -#[inline] -fn wait_for_update() { - wfe(); -} - -#[inline] -fn signal_update() { - dsb(); - sev(); -} +const LOCKED: u32 = 1; +const UNLOCKED: u32 = 0; pub struct Mutex { - locked: AtomicBool, + locked: AtomicU32, inner: UnsafeCell, } @@ -25,27 +17,21 @@ unsafe impl Send for Mutex {} impl Mutex { pub const fn new(inner: T) -> Self { Mutex{ - locked: AtomicBool::new(false), + locked: AtomicU32::new(UNLOCKED), inner: UnsafeCell::new(inner), } } pub fn lock(&self) -> MutexGuard { - while self.locked.compare_and_swap(false, true, Ordering::Acquire) { - while self.locked.load(Ordering::Relaxed) { - wait_for_update(); - } - } - + while self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::Acquire) != UNLOCKED {} dmb(); - MutexGuard { mutex: self } } fn unlock(&self) { dmb(); - self.locked.store(false, Ordering::Release); - signal_update(); + self.locked.store(UNLOCKED, Ordering::Release); + dsb(); } }