Skip to content

Commit 2957bc2

Browse files
committed
opt: optimize MountsMonitor thread exit to avoid long wait
1 parent 3945f90 commit 2957bc2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/mount.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
collections::{BTreeMap, HashMap},
99
sync::{
1010
atomic::{AtomicBool, Ordering},
11-
Arc, Mutex, RwLock,
11+
Arc, Condvar, Mutex, RwLock,
1212
},
1313
thread::{self, JoinHandle},
1414
time::Duration,
@@ -68,6 +68,7 @@ pub struct MountsMonitor {
6868
interval: u64,
6969
tables: Arc<RwLock<Vec<Arc<MountsRouter>>>>,
7070
running: Arc<AtomicBool>,
71+
stop_condvar: Arc<(Mutex<bool>, Condvar)>,
7172
handle: Mutex<Option<JoinHandle<()>>>,
7273
}
7374

@@ -387,6 +388,7 @@ impl MountsMonitor {
387388
interval,
388389
tables: Arc::new(RwLock::new(Vec::new())),
389390
running: Arc::new(AtomicBool::new(false)),
391+
stop_condvar: Arc::new((Mutex::new(false), Condvar::new())),
390392
handle: Mutex::new(None),
391393
}
392394
}
@@ -408,6 +410,7 @@ impl MountsMonitor {
408410

409411
self.running.store(true, Ordering::Relaxed);
410412
let running_flag = self.running.clone();
413+
let stop_condvar = self.stop_condvar.clone();
411414

412415
let core = self.core.clone();
413416
let mount_tables = self.tables.clone();
@@ -444,6 +447,14 @@ impl MountsMonitor {
444447
}
445448
}
446449
}
450+
default => {
451+
let (stop_mutex, stop_condvar) = &*stop_condvar;
452+
let stop_guard = stop_mutex.lock().unwrap();
453+
if *stop_guard {
454+
break;
455+
}
456+
let _ = stop_condvar.wait_timeout(stop_guard, Duration::from_millis(10)).unwrap();
457+
}
447458
}
448459
}
449460
});
@@ -454,6 +465,12 @@ impl MountsMonitor {
454465

455466
pub fn stop(&self) {
456467
self.running.store(false, Ordering::Relaxed);
468+
let (stop_mutex, stop_condvar) = &*self.stop_condvar;
469+
{
470+
let mut stop_guard = stop_mutex.lock().unwrap();
471+
*stop_guard = true;
472+
}
473+
stop_condvar.notify_one();
457474
if let Some(handle) = self.handle.lock().unwrap().take() {
458475
let _ = handle.join();
459476
}

0 commit comments

Comments
 (0)