1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Remove the single_threaded/multi_threaded feature flags (#1390)

Always use parking_lot for mutexes, i.e. always be multi-threaded.

Closes #1379
This commit is contained in:
Emil Ernerfeldt
2022-03-21 22:20:37 +01:00
committed by GitHub
parent 5c68edbb15
commit 15254f8235
15 changed files with 21 additions and 120 deletions

View File

@@ -1,16 +1,10 @@
//! Helper module that wraps some Mutex types with different implementations.
//!
//! When the `single_threaded` feature is on the mutexes will panic when locked from different threads.
#[cfg(not(any(feature = "single_threaded", feature = "multi_threaded")))]
compile_error!("Either feature \"single_threaded\" or \"multi_threaded\" must be enabled.");
// ----------------------------------------------------------------------------
#[cfg(feature = "multi_threaded")]
#[cfg(not(debug_assertions))]
mod mutex_impl {
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
/// Provides interior mutability.
#[derive(Default)]
pub struct Mutex<T>(parking_lot::Mutex<T>);
@@ -30,10 +24,9 @@ mod mutex_impl {
}
}
#[cfg(feature = "multi_threaded")]
#[cfg(debug_assertions)]
mod mutex_impl {
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
/// Provides interior mutability.
#[derive(Default)]
pub struct Mutex<T>(parking_lot::Mutex<T>);
@@ -111,7 +104,6 @@ mod mutex_impl {
}
}
#[cfg(feature = "multi_threaded")]
mod rw_lock_impl {
/// The lock you get from [`RwLock::read`].
pub use parking_lot::MappedRwLockReadGuard as RwLockReadGuard;
@@ -119,7 +111,7 @@ mod rw_lock_impl {
/// The lock you get from [`RwLock::write`].
pub use parking_lot::MappedRwLockWriteGuard as RwLockWriteGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
/// Provides interior mutability.
#[derive(Default)]
pub struct RwLock<T>(parking_lot::RwLock<T>);
@@ -141,79 +133,12 @@ mod rw_lock_impl {
}
}
#[cfg(feature = "multi_threaded")]
mod arc_impl {
pub use std::sync::Arc;
}
// ----------------------------------------------------------------------------
#[cfg(not(feature = "multi_threaded"))]
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)]
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()
}
}
}
#[cfg(not(feature = "multi_threaded"))]
mod rw_lock_impl {
// `atomic_refcell` will panic if multiple threads try to access the same value
/// The lock you get from [`RwLock::read`].
pub use atomic_refcell::AtomicRef as RwLockReadGuard;
/// The lock you get from [`RwLock::write`].
pub use atomic_refcell::AtomicRefMut as RwLockWriteGuard;
/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);
impl<T> RwLock<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(atomic_refcell::AtomicRefCell::new(val))
}
#[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()
}
}
}
#[cfg(not(feature = "multi_threaded"))]
mod arc_impl {
// pub use std::rc::Rc as Arc; // TODO(emilk): optimize single threaded code by using `Rc` instead of `Arc`.
pub use std::sync::Arc;
}
// ----------------------------------------------------------------------------
pub use arc_impl::Arc;
pub use mutex_impl::{Mutex, MutexGuard};
pub use rw_lock_impl::{RwLock, RwLockReadGuard, RwLockWriteGuard};