Skip to content

Commit

Permalink
Use const init instead of lazy_static (#97)
Browse files Browse the repository at this point in the history
* Remove Arc use in IterationRequest

* Use const init instead of `lazy_static`
Expyron authored Dec 8, 2022
1 parent 4644229 commit f565c62
Showing 2 changed files with 13 additions and 27 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
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};
@@ -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<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,
@@ -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();
}
}

0 comments on commit f565c62

Please sign in to comment.