summaryrefslogtreecommitdiff
path: root/src/sync.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sync.rs')
-rw-r--r--src/sync.rs29
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
}
}