mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-09 10:14:21 +01:00
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:
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user