diff options
| author | Christian Cunningham <c@localhost> | 2022-08-26 19:28:41 -0700 |
|---|---|---|
| committer | Christian Cunningham <c@localhost> | 2022-08-26 19:28:41 -0700 |
| commit | 4b940520d5395ceb73ba3cb9706fc89039708626 (patch) | |
| tree | 062ad21b61d0621745e06acf200aa8628a11ddca /src/sync.rs | |
| parent | 1516372cf6ad6829fa846f1cd49110cc8dad1962 (diff) | |
Fix mutex acquire
Diffstat (limited to 'src/sync.rs')
| -rw-r--r-- | src/sync.rs | 29 |
1 files changed, 5 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 } } |
