diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index cdb9b5fd..6050f9b4 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -835,8 +835,10 @@ mod tests { let tracked_hwnd = tracked.0 as isize; // put back when the guard is dropped, unless something else changed it meanwhile - let handling_override = crate::settings::settings() - .scoped(|settings| &mut settings.platform.window_handling_behaviour, handling); + let handling_override = crate::settings::settings().scoped( + |settings| &mut settings.platform.window_handling_behaviour, + handling, + ); let mut border = Border::create("komorebi-border-test", tracked_hwnd, 0).expect("a border to paint"); diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index e06a3a6b..f4036938 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -421,8 +421,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result // however this pass ends, `continue` included, the thread is waiting for the next let _waiting = Waiting; // Check the wm state every time we receive a notification - let state = wm.lock(); - let _held = crate::lock_holder::held("border manager"); + let state = crate::lock_holder::lock(&wm, "border manager"); let is_paused = state.is_paused; let focused_monitor_idx = state.focused_monitor_idx(); let focused_workspace_idx = diff --git a/komorebi/src/lock_holder.rs b/komorebi/src/lock_holder.rs index c43b66ee..d1ba2e16 100644 --- a/komorebi/src/lock_holder.rs +++ b/komorebi/src/lock_holder.rs @@ -91,6 +91,38 @@ pub fn describe() -> Option { }) } +/// A lock guard together with the record of who holds it, released together: the record first, then +/// the lock, so the diagnostics never say that a thread holds the lock after it has let go. Drop it +/// (`drop(guard)`) at the point where the lock is released. +pub struct HeldGuard<'a, T> { + // field order is drop order: the record goes before the guard + _held: Held, + guard: parking_lot::MutexGuard<'a, T>, +} + +/// Locks `mutex` and records `what` as the holder until the returned guard is dropped +pub fn lock(mutex: &Mutex, what: impl Into>) -> HeldGuard<'_, T> { + let guard = mutex.lock(); + HeldGuard { + _held: held(what), + guard, + } +} + +impl std::ops::Deref for HeldGuard<'_, T> { + type Target = T; + + fn deref(&self) -> &T { + &self.guard + } +} + +impl std::ops::DerefMut for HeldGuard<'_, T> { + fn deref_mut(&mut self) -> &mut T { + &mut self.guard + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/komorebi/src/monitor_reconciliator/mod.rs b/komorebi/src/monitor_reconciliator/mod.rs index 83c0d418..990a264f 100644 --- a/komorebi/src/monitor_reconciliator/mod.rs +++ b/komorebi/src/monitor_reconciliator/mod.rs @@ -276,8 +276,7 @@ where // Keep reference to Arc for potential re-locking let wm_arc = Arc::clone(&wm); - let mut wm = wm.lock(); - let _held = crate::lock_holder::held("monitor reconciliator"); + let mut wm = crate::lock_holder::lock(&wm, "monitor reconciliator"); let initial_state = State::from(wm.as_ref()); @@ -452,7 +451,7 @@ where ); // Re-acquire locks for state restoration - wm = wm_arc.lock(); + wm = crate::lock_holder::lock(&wm_arc, "monitor reconciliator"); // Update Win32 data for all monitors for monitor in wm.monitors_mut() { @@ -535,7 +534,7 @@ where ); // Re-acquire locks for removal processing - wm = wm_arc.lock(); + wm = crate::lock_holder::lock(&wm_arc, "monitor reconciliator"); monitor_cache = MONITOR_CACHE .get_or_init(|| Mutex::new(HashMap::new())) .lock(); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 3fcac487..b7cf87e4 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -122,8 +122,7 @@ pub fn listen_for_events(wm: Arc>) { tracing::info!("listening"); loop { if let Ok(event) = receiver.recv() { - let mut guard = wm.lock(); - let _held = crate::lock_holder::held("process_event"); + let mut guard = crate::lock_holder::lock(&wm, "process_event"); match guard.process_event(event) { Ok(()) => {} Err(error) => { diff --git a/komorebi/src/reaper.rs b/komorebi/src/reaper.rs index 37f8cd81..932d4f0f 100644 --- a/komorebi/src/reaper.rs +++ b/komorebi/src/reaper.rs @@ -77,8 +77,7 @@ fn handle_notifications(wm: Arc>) -> color_eyre::Result<()> for notification in receiver { let orphan_hwnds = notification.0; - let mut wm = wm.lock(); - let _held = crate::lock_holder::held("reaper"); + let mut wm = crate::lock_holder::lock(&wm, "reaper"); let mut update_borders = false; diff --git a/komorebi/src/stackbar_manager/mod.rs b/komorebi/src/stackbar_manager/mod.rs index 34318339..4d23a8e6 100644 --- a/komorebi/src/stackbar_manager/mod.rs +++ b/komorebi/src/stackbar_manager/mod.rs @@ -95,8 +95,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result let mut stackbars_monitors = STACKBARS_MONITORS.lock(); // Check the wm state every time we receive a notification - let mut state = wm.lock(); - let _held = crate::lock_holder::held("stackbar manager"); + let mut state = crate::lock_holder::lock(&wm, "stackbar manager"); // If stackbars are disabled if matches!(STACKBAR_MODE.load(), StackbarMode::Never) diff --git a/komorebi/src/transparency_manager.rs b/komorebi/src/transparency_manager.rs index dccb9d90..ec92409a 100644 --- a/komorebi/src/transparency_manager.rs +++ b/komorebi/src/transparency_manager.rs @@ -78,8 +78,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result known_hwnds.lock().clear(); // Check the wm state every time we receive a notification - let state = wm.lock(); - let _held = crate::lock_holder::held("transparency manager"); + let state = crate::lock_holder::lock(&wm, "transparency manager"); let focused_monitor_idx = state.focused_monitor_idx();