From 8915f171fe3815233bb4062d30cc6a11e1a9b246 Mon Sep 17 00:00:00 2001 From: qwp0905 Date: Wed, 6 May 2026 11:17:17 +0900 Subject: [PATCH] add counting latch low level waiters --- src/cache/latch.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cache/latch.rs b/src/cache/latch.rs index 0162c57..6dcb05d 100644 --- a/src/cache/latch.rs +++ b/src/cache/latch.rs @@ -5,6 +5,7 @@ use crate::utils::ShortenedMutex; struct State { locked: bool, high_waiters: usize, + low_waiters: usize, } pub struct Latch { @@ -18,6 +19,7 @@ impl Latch { state: Mutex::new(State { locked: false, high_waiters: 0, + low_waiters: 0, }), high_cv: Condvar::new(), low_cv: Condvar::new(), @@ -45,7 +47,9 @@ impl Latch { { let mut state = self.state.l(); while state.locked || state.high_waiters > 0 { + state.low_waiters += 1; state = self.low_cv.wait(state).unwrap(); + state.low_waiters -= 1; } state.locked = true; @@ -56,10 +60,12 @@ impl Latch { let mut state = self.state.l(); state.locked = false; if state.high_waiters > 0 { - self.high_cv.notify_one(); - } else { - self.low_cv.notify_one(); + return self.high_cv.notify_one(); } + if state.low_waiters == 0 { + return; + } + self.low_cv.notify_one(); } }