Use parking_lot for wrapping TextureManager

This commit is contained in:
Emil Ernerfeldt 2022-03-20 21:02:14 +01:00
parent 079490b30d
commit 6a3e7d17ac
5 changed files with 15 additions and 10 deletions

1
Cargo.lock generated
View file

@ -1005,6 +1005,7 @@ dependencies = [
"ahash 0.7.6",
"epaint",
"nohash-hasher",
"parking_lot 0.12.0",
"ron",
"serde",
"tracing",

View file

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

View file

@ -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<RwLock<epaint::TextureManager>>);
struct WrappedTextureManager(Arc<parking_lot::RwLock<epaint::TextureManager>>);
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<RwLock<ContextImpl>>,
// 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<RepaintInfo>,
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<RwLock<epaint::textures::TextureManager>> {
pub fn tex_manager(&self) -> Arc<parking_lot::RwLock<epaint::textures::TextureManager>> {
self.tex_manager.0.clone()
}

View file

@ -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]

View file

@ -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.
///