Clean up epaint mutex code

This commit is contained in:
Emil Ernerfeldt 2021-10-20 22:14:16 +02:00
parent dd50cba9a7
commit 2a9037cd90
2 changed files with 165 additions and 156 deletions

View file

@ -7,201 +7,205 @@ compile_error!("Either feature \"single_threaded\" or \"multi_threaded\" must be
// ----------------------------------------------------------------------------
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[cfg(feature = "multi_threaded")]
#[derive(Default)]
pub struct Mutex<T>(parking_lot::Mutex<T>);
/// The lock you get from [`Mutex`].
#[cfg(feature = "multi_threaded")]
#[cfg(not(debug_assertions))]
pub use parking_lot::MutexGuard;
mod mutex_impl {
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct Mutex<T>(parking_lot::Mutex<T>);
/// The lock you get from [`Mutex`].
pub use parking_lot::MutexGuard;
impl<T> Mutex<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(parking_lot::Mutex::new(val))
}
#[inline(always)]
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.lock()
}
}
}
/// The lock you get from [`Mutex`].
#[cfg(feature = "multi_threaded")]
#[cfg(debug_assertions)]
pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>, *const ());
mod mutex_impl {
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct Mutex<T>(parking_lot::Mutex<T>);
#[cfg(all(debug_assertions, feature = "multi_threaded"))]
#[derive(Default)]
struct HeldLocks(Vec<*const ()>);
/// The lock you get from [`Mutex`].
pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>, *const ());
#[cfg(all(debug_assertions, feature = "multi_threaded"))]
impl HeldLocks {
fn insert(&mut self, lock: *const ()) {
// Very few locks will ever be held at the same time, so a linear search is fast
assert!(
!self.0.contains(&lock),
"Recursively locking a Mutex in the same thread is not supported"
);
self.0.push(lock);
#[derive(Default)]
struct HeldLocks(Vec<*const ()>);
impl HeldLocks {
#[inline(always)]
fn insert(&mut self, lock: *const ()) {
// Very few locks will ever be held at the same time, so a linear search is fast
assert!(
!self.0.contains(&lock),
"Recursively locking a Mutex in the same thread is not supported"
);
self.0.push(lock);
}
#[inline(always)]
fn remove(&mut self, lock: *const ()) {
self.0.retain(|&ptr| ptr != lock);
}
}
fn remove(&mut self, lock: *const ()) {
self.0.retain(|&ptr| ptr != lock);
thread_local! {
static HELD_LOCKS_TLS: std::cell::RefCell<HeldLocks> = Default::default();
}
impl<T> Mutex<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(parking_lot::Mutex::new(val))
}
pub fn lock(&self) -> MutexGuard<'_, T> {
// Detect if we are recursively taking out a lock on this mutex.
// use a pointer to the inner data as an id for this lock
let ptr = (&self.0 as *const parking_lot::Mutex<_>).cast::<()>();
// Store it in thread local storage while we have a lock guard taken out
HELD_LOCKS_TLS.with(|held_locks| {
held_locks.borrow_mut().insert(ptr);
});
MutexGuard(self.0.lock(), ptr)
}
}
impl<T> Drop for MutexGuard<'_, T> {
fn drop(&mut self) {
let ptr = self.1;
HELD_LOCKS_TLS.with(|held_locks| {
held_locks.borrow_mut().remove(ptr);
});
}
}
impl<T> std::ops::Deref for MutexGuard<'_, T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> std::ops::DerefMut for MutexGuard<'_, T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
}
#[cfg(all(debug_assertions, feature = "multi_threaded"))]
thread_local! {
static HELD_LOCKS_TLS: std::cell::RefCell<HeldLocks> = Default::default();
}
#[cfg(feature = "multi_threaded")]
impl<T> Mutex<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(parking_lot::Mutex::new(val))
}
mod rw_lock_impl {
/// The lock you get from [`RwLock::read`].
pub use parking_lot::RwLockReadGuard;
#[cfg(debug_assertions)]
pub fn lock(&self) -> MutexGuard<'_, T> {
// Detect if we are recursively taking out a lock on this mutex.
/// The lock you get from [`RwLock::write`].
pub use parking_lot::RwLockWriteGuard;
// use a pointer to the inner data as an id for this lock
let ptr = (&self.0 as *const parking_lot::Mutex<_>).cast::<()>();
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct RwLock<T>(parking_lot::RwLock<T>);
// Store it in thread local storage while we have a lock guard taken out
HELD_LOCKS_TLS.with(|held_locks| {
held_locks.borrow_mut().insert(ptr);
});
impl<T> RwLock<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(parking_lot::RwLock::new(val))
}
MutexGuard(self.0.lock(), ptr)
}
#[inline(always)]
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.0.read()
}
#[inline(always)]
#[cfg(not(debug_assertions))]
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.lock()
}
}
#[cfg(debug_assertions)]
#[cfg(feature = "multi_threaded")]
impl<T> Drop for MutexGuard<'_, T> {
fn drop(&mut self) {
let ptr = self.1;
HELD_LOCKS_TLS.with(|held_locks| {
held_locks.borrow_mut().remove(ptr);
});
}
}
#[cfg(debug_assertions)]
#[cfg(feature = "multi_threaded")]
impl<T> std::ops::Deref for MutexGuard<'_, T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(debug_assertions)]
#[cfg(feature = "multi_threaded")]
impl<T> std::ops::DerefMut for MutexGuard<'_, T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
// ---------------------
/// The lock you get from [`RwLock::read`].
#[cfg(feature = "multi_threaded")]
pub use parking_lot::RwLockReadGuard;
/// The lock you get from [`RwLock::write`].
#[cfg(feature = "multi_threaded")]
pub use parking_lot::RwLockWriteGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[cfg(feature = "multi_threaded")]
#[derive(Default)]
pub struct RwLock<T>(parking_lot::RwLock<T>);
#[cfg(feature = "multi_threaded")]
impl<T> RwLock<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(parking_lot::RwLock::new(val))
}
#[inline(always)]
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.0.read()
}
#[inline(always)]
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.write()
#[inline(always)]
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.write()
}
}
}
// ----------------------------------------------------------------------------
// `atomic_refcell` will panic if multiple threads try to access the same value
/// The lock you get from [`Mutex`].
#[cfg(not(feature = "multi_threaded"))]
pub use atomic_refcell::AtomicRefMut as MutexGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[cfg(not(feature = "multi_threaded"))]
#[derive(Default)]
pub struct Mutex<T>(atomic_refcell::AtomicRefCell<T>);
#[cfg(not(feature = "multi_threaded"))]
impl<T> Mutex<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val))
}
mod mutex_impl {
// `atomic_refcell` will panic if multiple threads try to access the same value
/// Panics if already locked.
#[inline(always)]
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.borrow_mut()
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct Mutex<T>(atomic_refcell::AtomicRefCell<T>);
/// The lock you get from [`Mutex`].
pub use atomic_refcell::AtomicRefMut as MutexGuard;
impl<T> Mutex<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val))
}
/// Panics if already locked.
#[inline(always)]
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.borrow_mut()
}
}
}
// ---------------------
/// The lock you get from [`RwLock::read`].
#[cfg(not(feature = "multi_threaded"))]
pub use atomic_refcell::AtomicRef as RwLockReadGuard;
mod rw_lock_impl {
// `atomic_refcell` will panic if multiple threads try to access the same value
/// The lock you get from [`RwLock::write`].
#[cfg(not(feature = "multi_threaded"))]
pub use atomic_refcell::AtomicRefMut as RwLockWriteGuard;
/// The lock you get from [`RwLock::read`].
pub use atomic_refcell::AtomicRef as RwLockReadGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[cfg(not(feature = "multi_threaded"))]
#[derive(Default)]
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);
/// The lock you get from [`RwLock::write`].
pub use atomic_refcell::AtomicRefMut as RwLockWriteGuard;
#[cfg(not(feature = "multi_threaded"))]
impl<T> RwLock<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val))
}
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);
#[inline(always)]
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.0.borrow()
}
impl<T> RwLock<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val))
}
/// Panics if already locked.
#[inline(always)]
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.borrow_mut()
#[inline(always)]
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.0.borrow()
}
/// Panics if already locked.
#[inline(always)]
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.borrow_mut()
}
}
}
// ----------------------------------------------------------------------------
pub use mutex_impl::{Mutex, MutexGuard};
pub use rw_lock_impl::{RwLock, RwLockReadGuard, RwLockWriteGuard};
impl<T> Clone for Mutex<T>
where
T: Clone,
@ -211,6 +215,8 @@ where
}
}
// ----------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use crate::mutex::Mutex;

View file

@ -20,6 +20,9 @@ cargo doc -p egui_web --target wasm32-unknown-unknown --lib --no-deps --all-feat
(cd emath && cargo check --no-default-features)
(cd epaint && cargo check --no-default-features --features "single_threaded")
(cd epaint && cargo check --no-default-features --features "multi_threaded")
(cd epaint && cargo check --no-default-features --features "single_threaded" --release)
(cd epaint && cargo check --no-default-features --features "multi_threaded" --release)
(cd egui && cargo check --no-default-features --features "multi_threaded")
(cd eframe && cargo check --no-default-features --features "egui_glow")
(cd epi && cargo check --no-default-features)