diff options
| -rw-r--r-- | src/sync.rs | 29 | ||||
| -rw-r--r-- | 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<T> NullLock<T> { } impl<T: interface::Initializable> NullLock<T> { + /// # Initialize NullLock pub fn init(&self) { use interface::Mutex; self.lock(|initializable| { @@ -84,6 +86,7 @@ impl<T> interface::Mutex for NullLock<T> { use core::sync::atomic::{AtomicBool, Ordering}; +/// # Spinlock pub struct SpinLock<T> where T: Sized, @@ -120,34 +123,12 @@ impl<T> interface::Mutex for SpinLock<T> { 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>); } |
