|
|
|
@ -1,9 +1,3 @@
|
|
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
|
|
use crossbeam_utils::Backoff;
|
|
|
|
|
|
|
|
|
|
/// Calls a function and aborts if it panics.
|
|
|
|
|
///
|
|
|
|
|
/// This is useful in unsafe code where we can't recover from panics.
|
|
|
|
@ -58,71 +52,6 @@ pub fn random(n: u32) -> u32 {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A simple spinlock.
|
|
|
|
|
pub struct Spinlock<T> {
|
|
|
|
|
flag: AtomicBool,
|
|
|
|
|
value: UnsafeCell<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Send for Spinlock<T> {}
|
|
|
|
|
unsafe impl<T: Send> Sync for Spinlock<T> {}
|
|
|
|
|
|
|
|
|
|
impl<T> Spinlock<T> {
|
|
|
|
|
/// Returns a new spinlock initialized with `value`.
|
|
|
|
|
pub const fn new(value: T) -> Spinlock<T> {
|
|
|
|
|
Spinlock {
|
|
|
|
|
flag: AtomicBool::new(false),
|
|
|
|
|
value: UnsafeCell::new(value),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Locks the spinlock.
|
|
|
|
|
pub fn lock(&self) -> SpinlockGuard<'_, T> {
|
|
|
|
|
let backoff = Backoff::new();
|
|
|
|
|
while self.flag.swap(true, Ordering::Acquire) {
|
|
|
|
|
backoff.snooze();
|
|
|
|
|
}
|
|
|
|
|
SpinlockGuard { parent: self }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Attempts to lock the spinlock.
|
|
|
|
|
pub fn try_lock(&self) -> Option<SpinlockGuard<'_, T>> {
|
|
|
|
|
if self.flag.swap(true, Ordering::Acquire) {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(SpinlockGuard { parent: self })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A guard holding a spinlock locked.
|
|
|
|
|
pub struct SpinlockGuard<'a, T> {
|
|
|
|
|
parent: &'a Spinlock<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Send for SpinlockGuard<'_, T> {}
|
|
|
|
|
unsafe impl<T: Sync> Sync for SpinlockGuard<'_, T> {}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Drop for SpinlockGuard<'a, T> {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.parent.flag.store(false, Ordering::Release);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Deref for SpinlockGuard<'a, T> {
|
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
|
unsafe { &*self.parent.value.get() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
|
unsafe { &mut *self.parent.value.get() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Add additional context to errors
|
|
|
|
|
pub(crate) trait Context {
|
|
|
|
|
fn context(self, message: impl Fn() -> String) -> Self;
|
|
|
|
@ -338,3 +267,76 @@ macro_rules! extension_trait {
|
|
|
|
|
extension_trait!($($tail)*);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg_default! {
|
|
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
|
|
use crossbeam_utils::Backoff;
|
|
|
|
|
|
|
|
|
|
/// A simple spinlock.
|
|
|
|
|
pub struct Spinlock<T> {
|
|
|
|
|
flag: AtomicBool,
|
|
|
|
|
value: UnsafeCell<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Send for Spinlock<T> {}
|
|
|
|
|
unsafe impl<T: Send> Sync for Spinlock<T> {}
|
|
|
|
|
|
|
|
|
|
impl<T> Spinlock<T> {
|
|
|
|
|
/// Returns a new spinlock initialized with `value`.
|
|
|
|
|
pub const fn new(value: T) -> Spinlock<T> {
|
|
|
|
|
Spinlock {
|
|
|
|
|
flag: AtomicBool::new(false),
|
|
|
|
|
value: UnsafeCell::new(value),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Locks the spinlock.
|
|
|
|
|
pub fn lock(&self) -> SpinlockGuard<'_, T> {
|
|
|
|
|
let backoff = Backoff::new();
|
|
|
|
|
while self.flag.swap(true, Ordering::Acquire) {
|
|
|
|
|
backoff.snooze();
|
|
|
|
|
}
|
|
|
|
|
SpinlockGuard { parent: self }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Attempts to lock the spinlock.
|
|
|
|
|
pub fn try_lock(&self) -> Option<SpinlockGuard<'_, T>> {
|
|
|
|
|
if self.flag.swap(true, Ordering::Acquire) {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(SpinlockGuard { parent: self })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A guard holding a spinlock locked.
|
|
|
|
|
pub struct SpinlockGuard<'a, T> {
|
|
|
|
|
parent: &'a Spinlock<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Send for SpinlockGuard<'_, T> {}
|
|
|
|
|
unsafe impl<T: Sync> Sync for SpinlockGuard<'_, T> {}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Drop for SpinlockGuard<'a, T> {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.parent.flag.store(false, Ordering::Release);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Deref for SpinlockGuard<'a, T> {
|
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
|
unsafe { &*self.parent.value.get() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
|
unsafe { &mut *self.parent.value.get() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|