1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -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

@@ -4,6 +4,7 @@ All notable changes to the epaint crate will be documented in this file.
## Unreleased
* Add `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)).
* Removed the `single_threaded/multi_threaded` flags - epaint is now always thread-safe ([#1390](https://github.com/emilk/egui/pull/1390)).
## 0.17.0 - 2022-02-22

View File

@@ -27,7 +27,7 @@ all-features = true
[features]
default = ["default_fonts", "multi_threaded"]
default = ["default_fonts"]
# implement bytemuck on most types.
convert_bytemuck = ["bytemuck", "emath/bytemuck"]
@@ -47,23 +47,16 @@ mint = ["emath/mint"]
# implement serde on most types.
serialize = ["serde", "ahash/serde", "emath/serde"]
single_threaded = ["atomic_refcell"]
# Only needed if you plan to use the same fonts from multiple threads.
# It comes with a minor performance impact.
multi_threaded = ["parking_lot"]
[dependencies]
emath = { version = "0.17.0", path = "../emath" }
ab_glyph = "0.2.11"
ahash = { version = "0.7", default-features = false, features = ["std"] }
atomic_refcell = { version = "0.1", optional = true } # Used instead of parking_lot when you are always using epaint in a single thread. About as fast as parking_lot. Panics on multi-threaded use.
bytemuck = { version = "1.7.2", optional = true, features = ["derive"] }
cint = { version = "^0.2.2", optional = true }
nohash-hasher = "0.2"
parking_lot = { version = "0.12", optional = true } # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios.
parking_lot = "0.12" # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios.
serde = { version = "1", optional = true, features = ["derive", "rc"] }
[dev-dependencies]

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};