Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const init instead of lazy_static #97

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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"
Expand Down
37 changes: 12 additions & 25 deletions src/backend/gtk3/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use lazy_static::lazy_static;

use std::ptr;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
Expand All @@ -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(()),
}
Expand All @@ -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<Option<GtkThread>>,
request_count: Arc<AtomicUsize>,
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,
Expand All @@ -65,34 +59,27 @@ 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) {
self.thread.lock().unwrap().take();
}

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<AtomicUsize>,
}

impl IterationRequest {
fn new(request_count: Arc<AtomicUsize>) -> 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();
}
}
Expand Down