Skip to content

Commit 91ea82b

Browse files
committed
feat: drop trait for lock structures to prevent race conditions
1 parent e04c1af commit 91ea82b

1 file changed

Lines changed: 27 additions & 38 deletions

File tree

src-tauri/src/lib.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,50 @@ use tauri::{
2424
AppHandle, Manager,
2525
};
2626

27-
static HANDLE: OnceLock<Mutex<AppHandle>> = OnceLock::new();
27+
pub struct AppHandleWrapper(Mutex<AppHandle>);
28+
29+
impl Drop for AppHandleWrapper {
30+
fn drop(&mut self) {
31+
let (_lock, cvar) = &*HANDLE_CONDVAR;
32+
cvar.notify_all();
33+
}
34+
}
35+
36+
static HANDLE: OnceLock<AppHandleWrapper> = OnceLock::new();
2837
lazy_static! {
2938
static ref HANDLE_CONDVAR: (Mutex<bool>, Condvar) = (Mutex::new(false), Condvar::new());
3039
}
31-
static TRAY_ID: OnceLock<TrayIconId> = OnceLock::new();
40+
#[derive(Debug)]
41+
pub struct TrayIdWrapper(TrayIconId);
42+
43+
impl Drop for TrayIdWrapper {
44+
fn drop(&mut self) {
45+
let (_lock, cvar) = &*TRAY_CONDVAR;
46+
cvar.notify_all();
47+
}
48+
}
49+
50+
static TRAY_ID: OnceLock<TrayIdWrapper> = OnceLock::new();
3251
lazy_static! {
3352
static ref TRAY_CONDVAR: (Mutex<bool>, Condvar) = (Mutex::new(false), Condvar::new());
3453
}
3554
static CONFIG: OnceLock<UserConfig> = OnceLock::new();
3655
static FIRST_RUN: OnceLock<bool> = OnceLock::new();
3756

3857
fn init_app_handle(handle: AppHandle) {
39-
HANDLE.get_or_init(|| Mutex::new(handle));
58+
HANDLE.get_or_init(|| AppHandleWrapper(Mutex::new(handle)));
4059
let (lock, cvar) = &*HANDLE_CONDVAR;
4160
let mut started = lock.lock().expect("failed to lock HANDLE_CONDVAR");
4261
*started = true;
4362
cvar.notify_all();
4463
}
4564

4665
pub(crate) fn get_app_handle() -> &'static Mutex<AppHandle> {
47-
HANDLE.get().expect("HANDLE not initialized")
66+
&HANDLE.get().expect("HANDLE not initialized").0
4867
}
4968

5069
fn init_tray_id(id: TrayIconId) {
51-
TRAY_ID.set(id).expect("failed to set TRAY_ID");
70+
TRAY_ID.set(TrayIdWrapper(id)).expect("failed to set TRAY_ID");
5271
let (lock, cvar) = &*TRAY_CONDVAR;
5372
let mut initialized = lock.lock().expect("failed to lock TRAY_CONDVAR");
5473
*initialized = true;
@@ -61,7 +80,7 @@ pub(crate) fn get_tray_id() -> &'static TrayIconId {
6180
while !*initialized {
6281
initialized = cvar.wait(initialized).expect("failed to wait for TRAY_ID");
6382
}
64-
TRAY_ID.get().expect("TRAY_ID not initialized")
83+
&TRAY_ID.get().expect("TRAY_ID not initialized").0
6584
}
6685

6786
pub(crate) fn is_first_run() -> &'static bool {
@@ -72,8 +91,6 @@ pub fn handle_first_run() {
7291
let first_run = is_first_run();
7392
if *first_run {
7493
thread::spawn(|| {
75-
// TODO: debug and remove the sleep
76-
thread::sleep(Duration::from_secs(1));
7794
let app = &*get_app_handle().lock().expect("failed to get app handle");
7895
app.notification()
7996
.builder()
@@ -359,36 +376,8 @@ pub fn run() {
359376
}
360377
}
361378

362-
let first_run = is_first_run();
363-
if *first_run {
364-
thread::spawn(|| {
365-
// TODO: debug and remove the sleep
366-
thread::sleep(Duration::from_secs(1));
367-
let app = &*get_app_handle().lock().expect("failed to get app handle");
368-
app.notification()
369-
.builder()
370-
.title("Aw-Tauri")
371-
.body("Aw-Tauri is running in the background")
372-
.show()
373-
.unwrap();
374-
});
375-
}
376-
thread::spawn(|| {
377-
let config_path = get_config_path();
378-
let watcher =
379-
SpecificFileWatcher::new(config_path.parent().unwrap(), "single_instance.lock")
380-
.expect("Failed to create file watcher");
381-
loop {
382-
if watcher.wait_for_file().is_ok() {
383-
remove_file(config_path.parent().unwrap().join("single_instance.lock"))
384-
.expect("Failed to remove lock file");
385-
let app = &*get_app_handle().lock().expect("failed to get app handle");
386-
if let Some(window) = app.webview_windows().get("main") {
387-
window.show().unwrap();
388-
}
389-
}
390-
}
391-
});
379+
handle_first_run();
380+
listen_for_lockfile();
392381
Ok(())
393382
})
394383
.on_window_event(|window, event| {

0 commit comments

Comments
 (0)