Skip to content

Commit

Permalink
chore: replace mutex with rwlock
Browse files Browse the repository at this point in the history
  • Loading branch information
efJerryYang committed Aug 17, 2024
1 parent 39db112 commit dbf5861
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.vscode/
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uiohook-rs"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
description = "uiohook-rs is a Rust wrapper for the libuiohook, providing cross-platform keyboard and mouse hooking capabilities."
license = "GPL-3.0"
Expand Down
18 changes: 9 additions & 9 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use self::mouse::MouseEvent;
use self::wheel::WheelEvent;
// use std::ptr::addr_of_mut;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Once};
use std::sync::{Arc, RwLock, Once};
use std::thread;

pub mod keyboard;
pub mod mouse;
pub mod wheel;

static INIT: Once = Once::new();
static mut GLOBAL_HANDLER: Option<Arc<Mutex<dyn EventHandler>>> = None;
static mut GLOBAL_HANDLER: Option<Arc<RwLock<dyn EventHandler>>> = None;

/// Trait for handling uiohook events.
pub trait EventHandler: Send + Sync {
Expand All @@ -28,9 +28,9 @@ pub trait EventHandler: Send + Sync {

/// Main struct for interacting with uiohook.
pub struct Uiohook {
event_handler: Arc<Mutex<dyn EventHandler>>,
event_handler: Arc<RwLock<dyn EventHandler>>,
running: Arc<AtomicBool>,
thread_handle: Mutex<Option<thread::JoinHandle<()>>>,
thread_handle: RwLock<Option<thread::JoinHandle<()>>>,
}


Expand Down Expand Up @@ -58,9 +58,9 @@ impl Uiohook {
/// ```
pub fn new<H: EventHandler + 'static>(event_handler: H) -> Self {
Self {
event_handler: Arc::new(Mutex::new(event_handler)),
event_handler: Arc::new(RwLock::new(event_handler)),
running: Arc::new(AtomicBool::new(false)),
thread_handle: Mutex::new(None),
thread_handle: RwLock::new(None),
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl Uiohook {
}
});

*self.thread_handle.lock().unwrap() = Some(thread);
*self.thread_handle.write().unwrap() = Some(thread);

Ok(())
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Uiohook {

let result = unsafe { bindings::hook_stop() };

if let Some(thread) = self.thread_handle.lock().unwrap().take() {
if let Some(thread) = self.thread_handle.write().unwrap().take() {
thread.join().map_err(|_| UiohookError::Failure)?;
}

Expand Down Expand Up @@ -347,7 +347,7 @@ unsafe extern "C" fn dispatch_proc_wrapper(event: *mut bindings::uiohook_event)
fn dispatch_proc(event: &bindings::uiohook_event) {
if let Some(handler) = unsafe { GLOBAL_HANDLER.as_ref() } {
let event = UiohookEvent::from_raw_event(event);
if let Ok(guard) = handler.lock() {
if let Ok(guard) = handler.read() {
guard.handle_event(&event);
}
}
Expand Down

0 comments on commit dbf5861

Please sign in to comment.