From 4b940520d5395ceb73ba3cb9706fc89039708626 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Fri, 26 Aug 2022 19:28:41 -0700 Subject: Fix mutex acquire --- src/sync.rs | 29 +++++------------------------ src/util/queue.rs | 6 ++++++ 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/sync.rs b/src/sync.rs index a0e0f5d..d41d849 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -31,6 +31,7 @@ pub mod interface { /// /// Initializable type pub trait Initializable { + /// # Initialize structure fn init(&mut self); } } @@ -59,6 +60,7 @@ impl NullLock { } impl NullLock { + /// # Initialize NullLock pub fn init(&self) { use interface::Mutex; self.lock(|initializable| { @@ -84,6 +86,7 @@ impl interface::Mutex for NullLock { use core::sync::atomic::{AtomicBool, Ordering}; +/// # Spinlock pub struct SpinLock where T: Sized, @@ -120,34 +123,12 @@ impl interface::Mutex for SpinLock { type Data = T; /// # Lock fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut T) -> R) -> R { - loop { - // Loop until acquired the lock - match self - .lock - .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) - { - Ok(_) => { - break; - } - _ => {} - } - } + while let false = self.lock.swap(true, Ordering::SeqCst) {} let data = unsafe { &mut *self.data.get() }; let res = f(data); // Release the lock after finished with the underlying data - loop { - // Loop until acquired the lock - match self - .lock - .compare_exchange(true, false, Ordering::Release, Ordering::Relaxed) - { - Ok(_) => { - break; - } - _ => {} - } - } + self.lock.store(false, Ordering::SeqCst); res } } diff --git a/src/util/queue.rs b/src/util/queue.rs index 56122f7..ac23011 100644 --- a/src/util/queue.rs +++ b/src/util/queue.rs @@ -1,8 +1,14 @@ +//! # Queue type use super::node::Node; +/// # Queue Trait pub trait Queue<'a> { + /// # Data encapsulated type Data; + /// # Initialize fn init(&self); + /// # Pop fn pop(&self) -> Option<&mut Node<'a, Self::Data>>; + /// # Push fn push(&self, freed_item: &mut Node<'a, Self::Data>); } -- cgit v1.2.1