From 570860ec5f783ee42d7649c8b094596258bba814 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 14 Oct 2020 00:29:11 +0200 Subject: [PATCH] [refactor] Create helper struct Mutex (with double-lock detection) --- egui/src/context.rs | 56 ++++++++++++++++------------------------- egui/src/lib.rs | 1 + egui/src/mutex.rs | 49 ++++++++++++++++++++++++++++++++++++ egui/src/paint/font.rs | 7 ++++-- egui/src/paint/fonts.rs | 2 +- egui/src/ui.rs | 6 ++--- egui_web/src/lib.rs | 2 +- 7 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 egui/src/mutex.rs diff --git a/egui/src/context.rs b/egui/src/context.rs index 2463fa1c..921ec60b 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -3,12 +3,14 @@ use std::sync::{ Arc, }; -use { - ahash::AHashMap, - parking_lot::{Mutex, MutexGuard}, -}; +use ahash::AHashMap; -use crate::{animation_manager::AnimationManager, paint::*, *}; +use crate::{ + animation_manager::AnimationManager, + mutex::{Mutex, MutexGuard}, + paint::*, + *, +}; #[derive(Clone, Copy, Default)] struct PaintStats { @@ -60,15 +62,15 @@ pub struct Context { impl Clone for Context { fn clone(&self) -> Self { Context { - options: Mutex::new(lock(&self.options, "options").clone()), + options: self.options.clone(), fonts: self.fonts.clone(), memory: self.memory.clone(), animation_manager: self.animation_manager.clone(), input: self.input.clone(), - graphics: Mutex::new(self.graphics.lock().clone()), - output: Mutex::new(self.output.lock().clone()), - used_ids: Mutex::new(self.used_ids.lock().clone()), - paint_stats: Mutex::new(*self.paint_stats.lock()), + graphics: self.graphics.clone(), + output: self.output.clone(), + used_ids: self.used_ids.clone(), + paint_stats: self.paint_stats.clone(), repaint_requests: self.repaint_requests.load(SeqCst).into(), } } @@ -84,15 +86,15 @@ impl Context { } pub fn memory(&self) -> MutexGuard<'_, Memory> { - lock(&self.memory, "memory") + self.memory.lock() } pub fn graphics(&self) -> MutexGuard<'_, GraphicLayers> { - lock(&self.graphics, "graphics") + self.graphics.lock() } pub fn output(&self) -> MutexGuard<'_, Output> { - lock(&self.output, "output") + self.output.lock() } /// Call this if there is need to repaint the UI, i.e. if you are showing an animation. @@ -127,15 +129,15 @@ impl Context { /// Will become active at the start of the next frame. /// `pixels_per_point` will be ignored (overwritten at start of each frame with the contents of input) pub fn set_fonts(&self, font_definitions: FontDefinitions) { - lock(&self.options, "options").font_definitions = font_definitions; + self.options.lock().font_definitions = font_definitions; } pub fn style(&self) -> Arc