diff --git a/src/sync/Spinlock.rs b/src/sync/Spinlock.rs deleted file mode 100644 index 3f97990..0000000 --- a/src/sync/Spinlock.rs +++ /dev/null @@ -1,57 +0,0 @@ -use core::arch::asm; -use core::sync::atomic::{AtomicBool, Ordering}; -use core::cell::UnsafeCell; - -pub struct Spinlock { - locked: AtomicBool, - data: UnsafeCell, -} - -unsafe impl Sync for Spinlock {} - -impl Spinlock { - pub const fn new(data: T) -> Self { - Self { - locked: AtomicBool::new(false), - data: UnsafeCell::new(data), - } - } - - pub fn lock(&self) -> SpinlockGuard { - while let Err(_) = self.locked.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { - while self.locked.load(Ordering::Relaxed) { - // Spin until the lock is released - unsafe { asm!("pause") }; - } - } - - SpinlockGuard { - spinlock: &self, - } - } - -} - -pub struct SpinlockGuard<'a, T> { - spinlock: &'a Spinlock, -} - -impl<'a, T> core::ops::Deref for SpinlockGuard<'a, T> { - type Target = T; - - fn deref(&self) -> &T { - unsafe { &*self.spinlock.data.get() } - } -} - -impl<'a, T> core::ops::DerefMut for SpinlockGuard<'a, T> { - fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.spinlock.data.get() } - } -} - -impl<'a, T> Drop for SpinlockGuard<'a, T> { - fn drop(&mut self) { - self.spinlock.locked.store(false, Ordering::Release); - } -} diff --git a/src/sync/mod.rs b/src/sync/mod.rs index be2bfa5..3f97990 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -1 +1,57 @@ -pub(crate) mod Spinlock; \ No newline at end of file +use core::arch::asm; +use core::sync::atomic::{AtomicBool, Ordering}; +use core::cell::UnsafeCell; + +pub struct Spinlock { + locked: AtomicBool, + data: UnsafeCell, +} + +unsafe impl Sync for Spinlock {} + +impl Spinlock { + pub const fn new(data: T) -> Self { + Self { + locked: AtomicBool::new(false), + data: UnsafeCell::new(data), + } + } + + pub fn lock(&self) -> SpinlockGuard { + while let Err(_) = self.locked.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { + while self.locked.load(Ordering::Relaxed) { + // Spin until the lock is released + unsafe { asm!("pause") }; + } + } + + SpinlockGuard { + spinlock: &self, + } + } + +} + +pub struct SpinlockGuard<'a, T> { + spinlock: &'a Spinlock, +} + +impl<'a, T> core::ops::Deref for SpinlockGuard<'a, T> { + type Target = T; + + fn deref(&self) -> &T { + unsafe { &*self.spinlock.data.get() } + } +} + +impl<'a, T> core::ops::DerefMut for SpinlockGuard<'a, T> { + fn deref_mut(&mut self) -> &mut T { + unsafe { &mut *self.spinlock.data.get() } + } +} + +impl<'a, T> Drop for SpinlockGuard<'a, T> { + fn drop(&mut self) { + self.spinlock.locked.store(false, Ordering::Release); + } +}