Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/cache/latch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::utils::ShortenedMutex;
struct State {
locked: bool,
high_waiters: usize,
low_waiters: usize,
}

pub struct Latch {
Expand All @@ -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(),
Expand Down Expand Up @@ -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;

Expand All @@ -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();
}
}

Expand Down
Loading