Clean up epaint mutex code
This commit is contained in:
parent
dd50cba9a7
commit
2a9037cd90
2 changed files with 165 additions and 156 deletions
|
@ -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(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 ()>);
|
||||||
fn insert(&mut self, lock: *const ()) {
|
|
||||||
// Very few locks will ever be held at the same time, so a linear search is fast
|
impl HeldLocks {
|
||||||
assert!(
|
#[inline(always)]
|
||||||
!self.0.contains(&lock),
|
fn insert(&mut self, lock: *const ()) {
|
||||||
"Recursively locking a Mutex in the same thread is not supported"
|
// Very few locks will ever be held at the same time, so a linear search is fast
|
||||||
);
|
assert!(
|
||||||
self.0.push(lock);
|
!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 ()) {
|
thread_local! {
|
||||||
self.0.retain(|&ptr| ptr != lock);
|
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")]
|
#[cfg(feature = "multi_threaded")]
|
||||||
impl<T> Mutex<T> {
|
mod rw_lock_impl {
|
||||||
#[inline(always)]
|
/// The lock you get from [`RwLock::read`].
|
||||||
pub fn new(val: T) -> Self {
|
pub use parking_lot::RwLockReadGuard;
|
||||||
Self(parking_lot::Mutex::new(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
/// The lock you get from [`RwLock::write`].
|
||||||
pub fn lock(&self) -> MutexGuard<'_, T> {
|
pub use parking_lot::RwLockWriteGuard;
|
||||||
// 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
|
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
|
||||||
let ptr = (&self.0 as *const parking_lot::Mutex<_>).cast::<()>();
|
#[derive(Default)]
|
||||||
|
pub struct RwLock<T>(parking_lot::RwLock<T>);
|
||||||
|
|
||||||
// Store it in thread local storage while we have a lock guard taken out
|
impl<T> RwLock<T> {
|
||||||
HELD_LOCKS_TLS.with(|held_locks| {
|
#[inline(always)]
|
||||||
held_locks.borrow_mut().insert(ptr);
|
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)]
|
#[inline(always)]
|
||||||
#[cfg(not(debug_assertions))]
|
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
|
||||||
pub fn lock(&self) -> MutexGuard<'_, T> {
|
self.0.write()
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// `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 {
|
||||||
#[inline(always)]
|
// `atomic_refcell` will panic if multiple threads try to access the same value
|
||||||
pub fn new(val: T) -> Self {
|
|
||||||
Self(atomic_refcell::AtomicRefCell::new(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Panics if already locked.
|
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
|
||||||
#[inline(always)]
|
#[derive(Default)]
|
||||||
pub fn lock(&self) -> MutexGuard<'_, T> {
|
pub struct Mutex<T>(atomic_refcell::AtomicRefCell<T>);
|
||||||
self.0.borrow_mut()
|
|
||||||
|
/// 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"))]
|
#[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)]
|
||||||
#[inline(always)]
|
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);
|
||||||
pub fn new(val: T) -> Self {
|
|
||||||
Self(atomic_refcell::AtomicRefCell::new(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
impl<T> RwLock<T> {
|
||||||
pub fn read(&self) -> RwLockReadGuard<'_, T> {
|
#[inline(always)]
|
||||||
self.0.borrow()
|
pub fn new(val: T) -> Self {
|
||||||
}
|
Self(atomic_refcell::AtomicRefCell::new(val))
|
||||||
|
}
|
||||||
|
|
||||||
/// Panics if already locked.
|
#[inline(always)]
|
||||||
#[inline(always)]
|
pub fn read(&self) -> RwLockReadGuard<'_, T> {
|
||||||
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
|
self.0.borrow()
|
||||||
self.0.borrow_mut()
|
}
|
||||||
|
|
||||||
|
/// 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>
|
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;
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue