Remove unlock method and move it to guard's drop.

This makes the interface safe and prevents unlocking while still holding the guard
This commit is contained in:
krishvishal
2025-03-27 18:45:05 +05:30
parent af6e9cd2c2
commit dcd92954f4

View File

@@ -6,7 +6,7 @@ use std::{
#[derive(Debug)]
pub struct SpinLock<T> {
lock: AtomicBool,
locked: AtomicBool,
value: UnsafeCell<T>,
}
@@ -16,7 +16,7 @@ pub struct SpinLockGuard<'a, T> {
impl<'a, T> Drop for SpinLockGuard<'a, T> {
fn drop(&mut self) {
self.lock.unlock();
self.lock.locked.store(false, Ordering::Release);
}
}
@@ -40,21 +40,17 @@ unsafe impl<T> Sync for SpinLock<T> {}
impl<T> SpinLock<T> {
pub fn new(value: T) -> Self {
Self {
lock: AtomicBool::new(false),
locked: AtomicBool::new(false),
value: UnsafeCell::new(value),
}
}
pub fn lock(&self) -> SpinLockGuard<T> {
while self.lock.swap(true, Ordering::Acquire) {
while self.locked.swap(true, Ordering::Acquire) {
std::hint::spin_loop();
}
SpinLockGuard { lock: self }
}
pub fn unlock(&self) {
self.lock.store(false, Ordering::Release);
}
}
#[cfg(test)]