mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Add panic-on-poison to no-std std_shims::sync::Mutex
We already had this behavior on `std`. It was omitted when no-`std` due to deferring to `spin::Mutex`, which does not track poisoning at all. This increases the parity of the two. Part of https://github.com/serai-dex/serai/issues/698.
This commit is contained in:
@@ -6,12 +6,63 @@ pub use std::sync::{Arc, Weak};
|
|||||||
|
|
||||||
mod mutex_shim {
|
mod mutex_shim {
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
pub use spin::{Mutex, MutexGuard};
|
mod spin_mutex {
|
||||||
|
use core::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
// We wrap this in an `Option` so we can consider `None` as poisoned
|
||||||
|
pub(super) struct Mutex<T>(spin::Mutex<Option<T>>);
|
||||||
|
|
||||||
|
/// An acquired view of a `Mutex`.
|
||||||
|
pub struct MutexGuard<'mutex, T> {
|
||||||
|
mutex: spin::MutexGuard<'mutex, Option<T>>,
|
||||||
|
// This is `Some` for the lifetime of this guard, and is only represented as an `Option` due
|
||||||
|
// to needing to move it on `Drop` (which solely gives us a mutable reference to `self`)
|
||||||
|
value: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Mutex<T> {
|
||||||
|
pub(super) const fn new(value: T) -> Self {
|
||||||
|
Self(spin::Mutex::new(Some(value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn lock(&self) -> MutexGuard<'_, T> {
|
||||||
|
let mut mutex = self.0.lock();
|
||||||
|
// Take from the `Mutex` so future acquisitions will see `None` unless this is restored
|
||||||
|
let value = mutex.take();
|
||||||
|
// Check the prior acquisition did in fact restore the value
|
||||||
|
if value.is_none() {
|
||||||
|
panic!("locking a `spin::Mutex` held by a thread which panicked");
|
||||||
|
}
|
||||||
|
MutexGuard { mutex, value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for MutexGuard<'_, T> {
|
||||||
|
type Target = T;
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
self.value.as_ref().expect("no value yet checked upon lock acquisition")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T> DerefMut for MutexGuard<'_, T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
|
self.value.as_mut().expect("no value yet checked upon lock acquisition")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'mutex, T> Drop for MutexGuard<'mutex, T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Restore the value
|
||||||
|
*self.mutex = self.value.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
pub use spin_mutex::*;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::sync::{Mutex, MutexGuard};
|
pub use std::sync::{Mutex, MutexGuard};
|
||||||
|
|
||||||
/// A shimmed `Mutex` with an API mutual to `spin` and `std`.
|
/// A shimmed `Mutex` with an API mutual to `spin` and `std`.
|
||||||
#[derive(Default, Debug)]
|
|
||||||
pub struct ShimMutex<T>(Mutex<T>);
|
pub struct ShimMutex<T>(Mutex<T>);
|
||||||
impl<T> ShimMutex<T> {
|
impl<T> ShimMutex<T> {
|
||||||
/// Construct a new `Mutex`.
|
/// Construct a new `Mutex`.
|
||||||
@@ -21,8 +72,9 @@ mod mutex_shim {
|
|||||||
|
|
||||||
/// Acquire a lock on the contents of the `Mutex`.
|
/// Acquire a lock on the contents of the `Mutex`.
|
||||||
///
|
///
|
||||||
/// On no-`std` environments, this may spin until the lock is acquired. On `std` environments,
|
/// This will panic if the `Mutex` was poisoned.
|
||||||
/// this may panic if the `Mutex` was poisoned.
|
///
|
||||||
|
/// On no-`std` environments, the implementation presumably defers to that of a spin lock.
|
||||||
pub fn lock(&self) -> MutexGuard<'_, T> {
|
pub fn lock(&self) -> MutexGuard<'_, T> {
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
let res = self.0.lock().unwrap();
|
let res = self.0.lock().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user