Skip to main content

reifydb_runtime/sync/condvar/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::time::Duration;
5
6use cfg_if::cfg_if;
7
8use crate::sync::mutex::MutexGuard;
9
10#[cfg(not(reifydb_single_threaded))]
11pub mod native;
12#[cfg(reifydb_single_threaded)]
13pub mod wasm;
14
15cfg_if! {
16	if #[cfg(not(reifydb_single_threaded))] {
17		type CondvarInner = native::CondvarInner;
18	} else {
19		type CondvarInner = wasm::CondvarInner;
20	}
21}
22
23pub struct WaitTimeoutResult {
24	timed_out: bool,
25}
26
27impl WaitTimeoutResult {
28	#[inline]
29	pub fn timed_out(&self) -> bool {
30		self.timed_out
31	}
32}
33
34#[derive(Debug)]
35pub struct Condvar {
36	inner: CondvarInner,
37}
38
39impl Condvar {
40	#[inline]
41	pub fn new() -> Self {
42		Self {
43			inner: CondvarInner::new(),
44		}
45	}
46
47	#[inline]
48	pub fn wait<'a, T>(&self, guard: &mut MutexGuard<'a, T>) {
49		self.inner.wait(guard);
50	}
51
52	#[inline]
53	pub fn wait_for<'a, T>(&self, guard: &mut MutexGuard<'a, T>, timeout: Duration) -> WaitTimeoutResult {
54		let timed_out = self.inner.wait_for(guard, timeout);
55		WaitTimeoutResult {
56			timed_out,
57		}
58	}
59
60	#[inline]
61	pub fn notify_one(&self) {
62		self.inner.notify_one();
63	}
64
65	#[inline]
66	pub fn notify_all(&self) {
67		self.inner.notify_all();
68	}
69}
70
71impl Default for Condvar {
72	#[inline]
73	fn default() -> Self {
74		Self::new()
75	}
76}