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,27 +7,44 @@ 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(feature = "multi_threaded")]
#[cfg(not(debug_assertions))] #[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(feature = "multi_threaded")]
#[cfg(debug_assertions)] #[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"))] /// The lock you get from [`Mutex`].
#[derive(Default)] pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>, *const ());
struct HeldLocks(Vec<*const ()>);
#[cfg(all(debug_assertions, feature = "multi_threaded"))] #[derive(Default)]
impl HeldLocks { struct HeldLocks(Vec<*const ()>);
impl HeldLocks {
#[inline(always)]
fn insert(&mut self, lock: *const ()) { fn insert(&mut self, lock: *const ()) {
// Very few locks will ever be held at the same time, so a linear search is fast // Very few locks will ever be held at the same time, so a linear search is fast
assert!( assert!(
@ -37,24 +54,22 @@ impl HeldLocks {
self.0.push(lock); self.0.push(lock);
} }
#[inline(always)]
fn remove(&mut self, lock: *const ()) { fn remove(&mut self, lock: *const ()) {
self.0.retain(|&ptr| ptr != lock); self.0.retain(|&ptr| ptr != lock);
} }
} }
#[cfg(all(debug_assertions, feature = "multi_threaded"))] thread_local! {
thread_local! {
static HELD_LOCKS_TLS: std::cell::RefCell<HeldLocks> = Default::default(); static HELD_LOCKS_TLS: std::cell::RefCell<HeldLocks> = Default::default();
} }
#[cfg(feature = "multi_threaded")] impl<T> Mutex<T> {
impl<T> Mutex<T> {
#[inline(always)] #[inline(always)]
pub fn new(val: T) -> Self { pub fn new(val: T) -> Self {
Self(parking_lot::Mutex::new(val)) Self(parking_lot::Mutex::new(val))
} }
#[cfg(debug_assertions)]
pub fn lock(&self) -> MutexGuard<'_, T> { pub fn lock(&self) -> MutexGuard<'_, T> {
// Detect if we are recursively taking out a lock on this mutex. // Detect if we are recursively taking out a lock on this mutex.
@ -68,62 +83,47 @@ impl<T> Mutex<T> {
MutexGuard(self.0.lock(), ptr) MutexGuard(self.0.lock(), ptr)
} }
#[inline(always)]
#[cfg(not(debug_assertions))]
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.lock()
} }
}
#[cfg(debug_assertions)] impl<T> Drop for MutexGuard<'_, T> {
#[cfg(feature = "multi_threaded")]
impl<T> Drop for MutexGuard<'_, T> {
fn drop(&mut self) { fn drop(&mut self) {
let ptr = self.1; let ptr = self.1;
HELD_LOCKS_TLS.with(|held_locks| { HELD_LOCKS_TLS.with(|held_locks| {
held_locks.borrow_mut().remove(ptr); held_locks.borrow_mut().remove(ptr);
}); });
} }
} }
#[cfg(debug_assertions)] impl<T> std::ops::Deref for MutexGuard<'_, T> {
#[cfg(feature = "multi_threaded")]
impl<T> std::ops::Deref for MutexGuard<'_, T> {
type Target = T; type Target = T;
#[inline(always)] #[inline(always)]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
#[cfg(debug_assertions)] impl<T> std::ops::DerefMut for MutexGuard<'_, T> {
#[cfg(feature = "multi_threaded")]
impl<T> std::ops::DerefMut for MutexGuard<'_, T> {
#[inline(always)] #[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0 &mut self.0
} }
}
} }
// ---------------------
/// The lock you get from [`RwLock::read`].
#[cfg(feature = "multi_threaded")] #[cfg(feature = "multi_threaded")]
pub use parking_lot::RwLockReadGuard; mod rw_lock_impl {
/// The lock you get from [`RwLock::read`].
pub use parking_lot::RwLockReadGuard;
/// The lock you get from [`RwLock::write`]. /// The lock you get from [`RwLock::write`].
#[cfg(feature = "multi_threaded")] pub use parking_lot::RwLockWriteGuard;
pub use parking_lot::RwLockWriteGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled. /// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[cfg(feature = "multi_threaded")] #[derive(Default)]
#[derive(Default)] pub struct RwLock<T>(parking_lot::RwLock<T>);
pub struct RwLock<T>(parking_lot::RwLock<T>);
#[cfg(feature = "multi_threaded")] impl<T> RwLock<T> {
impl<T> RwLock<T> {
#[inline(always)] #[inline(always)]
pub fn new(val: T) -> Self { pub fn new(val: T) -> Self {
Self(parking_lot::RwLock::new(val)) Self(parking_lot::RwLock::new(val))
@ -138,22 +138,23 @@ impl<T> RwLock<T> {
pub fn write(&self) -> RwLockWriteGuard<'_, T> { pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.write() 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"))] #[cfg(not(feature = "multi_threaded"))]
impl<T> Mutex<T> { mod mutex_impl {
// `atomic_refcell` will panic if multiple threads try to access the same value
/// 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)] #[inline(always)]
pub fn new(val: T) -> Self { pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val)) Self(atomic_refcell::AtomicRefCell::new(val))
@ -164,25 +165,24 @@ impl<T> Mutex<T> {
pub fn lock(&self) -> MutexGuard<'_, T> { pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.borrow_mut() self.0.borrow_mut()
} }
}
} }
// ---------------------
/// The lock you get from [`RwLock::read`].
#[cfg(not(feature = "multi_threaded"))] #[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`]. /// The lock you get from [`RwLock::read`].
#[cfg(not(feature = "multi_threaded"))] pub use atomic_refcell::AtomicRef as RwLockReadGuard;
pub use atomic_refcell::AtomicRefMut as RwLockWriteGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled. /// The lock you get from [`RwLock::write`].
#[cfg(not(feature = "multi_threaded"))] pub use atomic_refcell::AtomicRefMut as RwLockWriteGuard;
#[derive(Default)]
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);
#[cfg(not(feature = "multi_threaded"))] /// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
impl<T> RwLock<T> { #[derive(Default)]
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);
impl<T> RwLock<T> {
#[inline(always)] #[inline(always)]
pub fn new(val: T) -> Self { pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val)) Self(atomic_refcell::AtomicRefCell::new(val))
@ -198,10 +198,14 @@ impl<T> RwLock<T> {
pub fn write(&self) -> RwLockWriteGuard<'_, T> { pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.borrow_mut() self.0.borrow_mut()
} }
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
pub use mutex_impl::{Mutex, MutexGuard};
pub use rw_lock_impl::{RwLock, RwLockReadGuard, RwLockWriteGuard};
impl<T> Clone for Mutex<T> impl<T> Clone for Mutex<T>
where where
T: Clone, T: Clone,
@ -211,6 +215,8 @@ where
} }
} }
// ----------------------------------------------------------------------------
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::mutex::Mutex; 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 emath && cargo check --no-default-features)
(cd epaint && cargo check --no-default-features --features "single_threaded") (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 egui && cargo check --no-default-features --features "multi_threaded")
(cd eframe && cargo check --no-default-features --features "egui_glow") (cd eframe && cargo check --no-default-features --features "egui_glow")
(cd epi && cargo check --no-default-features) (cd epi && cargo check --no-default-features)