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#[derive(Debug)]
9pub struct WaiterHandle {
10	notified: Mutex<bool>,
11	condvar: Condvar,
12}
13
14impl Default for WaiterHandle {
15	fn default() -> Self {
16		Self::new()
17	}
18}
19
20impl WaiterHandle {
21	pub fn new() -> Self {
22		Self {
23			notified: Mutex::new(false),
24			condvar: Condvar::new(),
25		}
26	}
27
28	pub fn notify(&self) {
29		let mut guard = self.notified.lock();
30		*guard = true;
31		self.condvar.notify_one();
32	}
33
34	pub fn wait_timeout(&self, timeout: Duration) -> bool {
35		let mut guard = self.notified.lock();
36		if *guard {
37			return true;
38		}
39		!self.condvar.wait_for(&mut guard, timeout).timed_out()
40	}
41}