From 30fcd7894aa7b0eb6c5d2e01918fe4c6c17d6fcb Mon Sep 17 00:00:00 2001 From: Stephen Marz Date: Mon, 27 Apr 2020 09:46:59 -0400 Subject: [PATCH] Change lock to try_lock to better align with Rust's verbiage --- risc_v/src/lock.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/risc_v/src/lock.rs b/risc_v/src/lock.rs index f00a731..eb3e69b 100644 --- a/risc_v/src/lock.rs +++ b/risc_v/src/lock.rs @@ -26,7 +26,8 @@ impl<'a> Mutex { &self.state } - pub fn lock(&mut self) -> bool { + /// Try to lock the Mutex. If the mutex is already locked, this function returns false, otherwise it will return true if the mutex was acquired. + pub fn try_lock(&mut self) -> bool { unsafe { let state: MutexState; llvm_asm!("amoswap.w.aq $0, $1, ($2)\n" : "=r"(state) : "r"(1), "r"(self) :: "volatile"); @@ -42,16 +43,17 @@ impl<'a> Mutex { /// Never use a sleep lock for the process list. Sleeping requires /// the process list to function, so you'll deadlock if you do. pub fn sleep_lock(&mut self) { - while !self.lock() { + while !self.try_lock() { syscall_sleep(DEFAULT_LOCK_SLEEP); } } /// Can safely be used inside of an interrupt context. pub fn spin_lock(&mut self) { - while !self.lock() {} + while !self.try_lock() {} } + /// Unlock a mutex without regard for its previous state. pub fn unlock(&mut self) { unsafe { llvm_asm!("amoswap.w.rl zero, zero, ($0)" :: "r"(self) :: "volatile");