Skip to main content

reifydb_runtime/sync/
waiter.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::time::Duration;
5
6use crate::sync::{condvar::Condvar, mutex::Mutex};
7
8/// Handle for waiting on a specific version to complete
9#[derive(Debug)]
10pub struct WaiterHandle {
11	notified: Mutex<bool>,
12	condvar: Condvar,
13}
14
15impl Default for WaiterHandle {
16	fn default() -> Self {
17		Self::new()
18	}
19}
20
21impl WaiterHandle {
22	pub fn new() -> Self {
23		Self {
24			notified: Mutex::new(false),
25			condvar: Condvar::new(),
26		}
27	}
28
29	pub fn notify(&self) {
30		let mut guard = self.notified.lock();
31		*guard = true;
32		self.condvar.notify_one();
33	}
34
35	pub fn wait_timeout(&self, timeout: Duration) -> bool {
36		let mut guard = self.notified.lock();
37		if *guard {
38			return true;
39		}
40		!self.condvar.wait_for(&mut guard, timeout).timed_out()
41	}
42}