From 6a3e7d17ac1ef5c2fbe2da1e0775de3dbed801f4 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 20 Mar 2022 21:02:14 +0100 Subject: [PATCH] Use parking_lot for wrapping TextureManager --- Cargo.lock | 1 + egui/Cargo.toml | 1 + egui/src/context.rs | 11 ++++++++--- epaint/Cargo.toml | 4 ++-- epaint/src/texture_handle.rs | 8 +++----- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 30384431..90b7379d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1005,6 +1005,7 @@ dependencies = [ "ahash 0.7.6", "epaint", "nohash-hasher", + "parking_lot 0.12.0", "ron", "serde", "tracing", diff --git a/egui/Cargo.toml b/egui/Cargo.toml index b99087a0..8c892e2b 100644 --- a/egui/Cargo.toml +++ b/egui/Cargo.toml @@ -57,6 +57,7 @@ epaint = { version = "0.17.0", path = "../epaint", default-features = false } ahash = "0.7" nohash-hasher = "0.2" +parking_lot = "0.12" # Optional: ron = { version = "0.7", optional = true } diff --git a/egui/src/context.rs b/egui/src/context.rs index d34b3084..930d4e6a 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -11,8 +11,10 @@ use epaint::{mutex::*, stats::*, text::Fonts, TessellationOptions, *}; // ---------------------------------------------------------------------------- +// NOTE: we always use `parking_lot` here so we can +// use `WrappedTextureManager` from any thread. #[derive(Clone)] -struct WrappedTextureManager(Arc>); +struct WrappedTextureManager(Arc>); impl Default for WrappedTextureManager { fn default() -> Self { @@ -25,7 +27,7 @@ impl Default for WrappedTextureManager { ); assert_eq!(font_id, TextureId::default()); - Self(Arc::new(RwLock::new(tex_mngr))) + Self(Arc::new(parking_lot::RwLock::new(tex_mngr))) } } @@ -163,6 +165,9 @@ impl ContextImpl { #[derive(Clone, Default)] pub struct Context { ctx: Arc>, + // These are not inside of `ContextImpl` because we want to have thread-safe access to them + // even without running with the multi_threaded feature flag. + // See https://github.com/emilk/egui/issues/1379. repaint_info: Arc, tex_manager: WrappedTextureManager, } @@ -731,7 +736,7 @@ impl Context { /// You can show stats about the allocated textures using [`Self::texture_ui`]. /// /// This is safe to call from any thread at any time. - pub fn tex_manager(&self) -> Arc> { + pub fn tex_manager(&self) -> Arc> { self.tex_manager.0.clone() } diff --git a/epaint/Cargo.toml b/epaint/Cargo.toml index 4c6ef6a2..e81e0c2d 100644 --- a/epaint/Cargo.toml +++ b/epaint/Cargo.toml @@ -51,7 +51,7 @@ 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"] +multi_threaded = [] [dependencies] @@ -63,7 +63,7 @@ atomic_refcell = { version = "0.1", optional = true } # Used 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" serde = { version = "1", optional = true, features = ["derive", "rc"] } [dev-dependencies] diff --git a/epaint/src/texture_handle.rs b/epaint/src/texture_handle.rs index fd10de26..3e22bd97 100644 --- a/epaint/src/texture_handle.rs +++ b/epaint/src/texture_handle.rs @@ -1,8 +1,6 @@ -use crate::{ - emath::NumExt, - mutex::{Arc, RwLock}, - ImageData, ImageDelta, TextureId, TextureManager, -}; +use crate::{emath::NumExt, mutex::Arc, ImageData, ImageDelta, TextureId, TextureManager}; + +use parking_lot::RwLock; /// Used to paint images. ///