Skip to content

Commit

Permalink
Remove the single_threaded/multi_threaded feature flags
Browse files Browse the repository at this point in the history
Always use parking_lot for mutexes, i.e. always be multi-threaded.

Closes #1379

An alternative to #1389
  • Loading branch information
emilk committed Mar 20, 2022
1 parent 861e129 commit c7d6775
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 119 deletions.
2 changes: 1 addition & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Examples: `Vec2, Pos2, Rect, lerp, remap`

Example: `Shape::Circle { center, radius, fill, stroke }`

Depends on `emath`, [`ab_glyph`](https://crates.io/crates/ab_glyph), [`atomic_refcell`](https://crates.io/crates/atomic_refcell), [`ahash`](https://crates.io/crates/ahash).
Depends on `emath`.

### `egui_extras`
This adds additional features on top of `egui`.
Expand Down
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ On Fedora Rawhide you need to run:
* Extensible: [easy to write your own widgets for egui](https://github.com/emilk/egui/blob/master/egui_demo_lib/src/apps/demo/toggle_switch.rs)
* Modular: You should be able to use small parts of egui and combine them in new ways
* Safe: there is no `unsafe` code in egui
* Minimal dependencies: [`ab_glyph`](https://crates.io/crates/ab_glyph) [`ahash`](https://crates.io/crates/ahash) [`atomic_refcell`](https://crates.io/crates/atomic_refcell), [`nohash-hasher`](https://crates.io/crates/nohash-hasher)
* Minimal dependencies: [`ab_glyph`](https://crates.io/crates/ab_glyph) [`ahash`](https://crates.io/crates/ahash) [`nohash-hasher`](https://crates.io/crates/nohash-hasher) [`parking_lot`](https://crates.io/crates/parking_lot)

egui is *not* a framework. egui is a library you call into, not an environment you program for.

Expand Down
1 change: 0 additions & 1 deletion egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ serialize = ["egui/serialize", "serde"]

[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"single_threaded",
"tracing",
] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
Expand Down
7 changes: 1 addition & 6 deletions egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ all-features = true


[features]
default = ["default_fonts", "single_threaded"]
default = ["default_fonts"]

# add compatibility with https://crates.io/crates/cint
cint = ["epaint/cint"]
Expand All @@ -46,11 +46,6 @@ persistence = ["serde", "epaint/serialize", "ron"]
# implement serde on most types.
serialize = ["serde", "epaint/serialize"]

# multi_threaded is only needed if you plan to use the same egui::Context
# from multiple threads. It comes with a minor performance impact.
single_threaded = ["epaint/single_threaded"]
multi_threaded = ["epaint/multi_threaded"]


[dependencies]
epaint = { version = "0.17.0", path = "../epaint", default-features = false }
Expand Down
6 changes: 2 additions & 4 deletions egui_extras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ svg = ["resvg", "tiny-skia", "usvg"]


[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"single_threaded",
] }
egui = { version = "0.17.0", path = "../egui", default-features = false }
parking_lot = "0.12"

# Optional dependencies:

# Add support for loading images with the `image` crate.
# You also need to ALSO opt-in to the image formats you want to support, like so:
# image = { version = "0.24", features = ["jpeg", "png"] }
image = { version = "0.24", optional = true, default-features = false, features = [] }
image = { version = "0.24", optional = true, default-features = false }

# svg feature
resvg = { version = "0.22", optional = true }
Expand Down
1 change: 0 additions & 1 deletion egui_glium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ screen_reader = ["egui-winit/screen_reader"]
[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"convert_bytemuck",
"single_threaded",
] }
egui-winit = { version = "0.17.0", path = "../egui-winit", default-features = false }

Expand Down
1 change: 0 additions & 1 deletion egui_glow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ winit = ["egui-winit", "glutin"]
[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"convert_bytemuck",
"single_threaded",
] }
epi = { version = "0.17.0", path = "../epi", optional = true }

Expand Down
1 change: 0 additions & 1 deletion egui_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ screen_reader = ["tts"]
[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"convert_bytemuck",
"single_threaded",
"tracing",
] }
egui_glow = { version = "0.17.0", path = "../egui_glow", default-features = false }
Expand Down
11 changes: 2 additions & 9 deletions epaint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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]
Expand Down
81 changes: 3 additions & 78 deletions epaint/src/mutex.rs
Original file line number Diff line number Diff line change
@@ -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>);

Expand All @@ -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>);

Expand Down Expand Up @@ -111,15 +104,14 @@ 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;

/// 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>);

Expand All @@ -141,74 +133,7 @@ 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;
}

Expand Down
4 changes: 1 addition & 3 deletions epi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ persistence = ["ron", "serde", "egui/persistence"]


[dependencies]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"single_threaded",
] }
egui = { version = "0.17.0", path = "../egui", default-features = false }
glow = "0.11"
tracing = "0.1"

Expand Down
10 changes: 4 additions & 6 deletions sh/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ cargo doc -p egui_web --target wasm32-unknown-unknown --lib --no-deps --all-feat
cargo doc --document-private-items --no-deps --all-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 "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,serialize")
(cd epaint && cargo check --no-default-features)
(cd epaint && cargo check --no-default-features --release)
(cd egui && cargo check --no-default-features --features "serialize")
(cd eframe && cargo check --no-default-features)
(cd epi && cargo check --no-default-features)
(cd egui_demo_lib && cargo check --no-default-features)
(cd egui_extras && cargo check --no-default-features)
(cd egui_web && cargo check --no-default-features)
# (cd egui-winit && cargo check --no-default-features) # we don't pick singlethreaded or multithreaded
(cd egui-winit && cargo check --no-default-features)
(cd egui_glium && cargo check --no-default-features)
(cd egui_glow && cargo check --no-default-features)

Expand Down

0 comments on commit c7d6775

Please sign in to comment.