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 1871962..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,30 +24,26 @@ impl GtkGlobalMutex { } } -lazy_static! { - pub static ref GTK_MUTEX: GtkGlobalMutex = GtkGlobalMutex::new(); -} +pub static 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 {} 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 = Arc::new(AtomicUsize::new(0)); + let request_count = AtomicUsize::new(0); Self { thread, request_count, @@ -65,7 +59,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 +68,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(); } }