From 297b3e92d174e64b957f5cf4943b7317d11233bd Mon Sep 17 00:00:00 2001 From: Expyron <5100376+Expyron@users.noreply.github.com> Date: Thu, 8 Dec 2022 18:32:38 +0100 Subject: [PATCH 1/2] Remove Arc use in IterationRequest --- src/backend/gtk3/utils.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/backend/gtk3/utils.rs b/src/backend/gtk3/utils.rs index 1871962..67c79bf 100644 --- a/src/backend/gtk3/utils.rs +++ b/src/backend/gtk3/utils.rs @@ -30,13 +30,13 @@ lazy_static! { pub static ref GTK_MUTEX: GtkGlobalMutex = GtkGlobalMutex::new(); } -/// # Event Hadnler +/// # Event Handler /// Counts amout of iteration requests /// When amount of requests goes above 0 it spawns GtkThread and starts iteration /// When amount of requests reqches 0 it stops GtkThread, and goes idle pub struct GtkEventHandler { thread: Mutex>, - request_count: Arc, + request_count: AtomicUsize, } unsafe impl Send for GtkEventHandler {} @@ -49,7 +49,7 @@ lazy_static! { impl GtkEventHandler { fn new() -> Self { let thread = Mutex::new(None); - let request_count = Arc::new(AtomicUsize::new(0)); + let request_count = AtomicUsize::new(0); Self { thread, request_count, @@ -65,7 +65,8 @@ impl GtkEventHandler { thread.replace(GtkThread::new()); } - IterationRequest::new(self.request_count.clone()) + self.request_count.fetch_add(1, Ordering::Relaxed); + IterationRequest () } fn iteration_stop(&self) { @@ -73,26 +74,18 @@ impl GtkEventHandler { } fn request_iteration_stop(&self) { + self.request_count.fetch_sub(1, Ordering::Release); + if self.request_count.load(Ordering::Acquire) == 0 { self.iteration_stop(); } } } -pub struct IterationRequest { - request_count: Arc, -} - -impl IterationRequest { - fn new(request_count: Arc) -> Self { - request_count.fetch_add(1, Ordering::Relaxed); - Self { request_count } - } -} +pub struct IterationRequest (); impl Drop for IterationRequest { fn drop(&mut self) { - self.request_count.fetch_sub(1, Ordering::Release); GTK_EVENT_HANDLER.request_iteration_stop(); } } From 055bb821c9bc3cb188160a5c4d1dfe77b30f4180 Mon Sep 17 00:00:00 2001 From: Expyron <5100376+Expyron@users.noreply.github.com> Date: Thu, 8 Dec 2022 18:37:51 +0100 Subject: [PATCH 2/2] Use const init instead of `lazy_static` --- Cargo.toml | 3 +-- src/backend/gtk3/utils.rs | 14 ++++---------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c75f6bb..f3d8a2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ documentation = "https://docs.rs/rfd" [features] default = ["gtk3"] file-handle-inner = [] -gtk3 = ["gtk-sys", "glib-sys", "gobject-sys", "lazy_static"] +gtk3 = ["gtk-sys", "glib-sys", "gobject-sys"] xdg-portal = ["ashpd", "urlencoding", "pollster"] common-controls-v6 = ["windows/Win32_UI_Controls"] @@ -49,7 +49,6 @@ pollster = { version = "0.2", optional = true } gtk-sys = { version = "0.15.1", features = ["v3_20"], optional = true } glib-sys = { version = "0.15.1", optional = true } gobject-sys = { version = "0.15.1", optional = true } -lazy_static = { version = "1.4.0", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2.69" diff --git a/src/backend/gtk3/utils.rs b/src/backend/gtk3/utils.rs index 67c79bf..29d878f 100644 --- a/src/backend/gtk3/utils.rs +++ b/src/backend/gtk3/utils.rs @@ -1,5 +1,3 @@ -use lazy_static::lazy_static; - use std::ptr; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; @@ -14,7 +12,7 @@ unsafe impl Send for GtkGlobalMutex {} unsafe impl Sync for GtkGlobalMutex {} impl GtkGlobalMutex { - fn new() -> Self { + const fn new() -> Self { Self { locker: Mutex::new(()), } @@ -26,9 +24,7 @@ impl GtkGlobalMutex { } } -lazy_static! { - pub static ref GTK_MUTEX: GtkGlobalMutex = GtkGlobalMutex::new(); -} +pub static GTK_MUTEX: GtkGlobalMutex = GtkGlobalMutex::new(); /// # Event Handler /// Counts amout of iteration requests @@ -42,12 +38,10 @@ pub struct GtkEventHandler { unsafe impl Send for GtkEventHandler {} unsafe impl Sync for GtkEventHandler {} -lazy_static! { - pub static ref GTK_EVENT_HANDLER: GtkEventHandler = GtkEventHandler::new(); -} +pub static GTK_EVENT_HANDLER: GtkEventHandler = GtkEventHandler::new(); impl GtkEventHandler { - fn new() -> Self { + const fn new() -> Self { let thread = Mutex::new(None); let request_count = AtomicUsize::new(0); Self {