Skip to main content

reifydb_runtime/sync/condvar/
mod.rs

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