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(all(not(reifydb_single_threaded), not(loom)))]
10pub mod host;
11#[cfg(loom)]
12pub mod loom;
13#[cfg(reifydb_single_threaded)]
14pub mod wasm;
15
16cfg_if! {
17	if #[cfg(loom)] {
18		type CondvarInner = loom::CondvarInner;
19	} else if #[cfg(not(reifydb_single_threaded))] {
20		type CondvarInner = host::CondvarInner;
21	} else {
22		type CondvarInner = wasm::CondvarInner;
23	}
24}
25
26pub struct WaitTimeoutResult {
27	timed_out: bool,
28}
29
30impl WaitTimeoutResult {
31	#[inline]
32	pub fn timed_out(&self) -> bool {
33		self.timed_out
34	}
35}
36
37#[derive(Debug)]
38pub struct Condvar {
39	inner: CondvarInner,
40}
41
42impl Condvar {
43	#[inline]
44	pub fn new() -> Self {
45		Self {
46			inner: CondvarInner::new(),
47		}
48	}
49
50	#[inline]
51	pub fn wait<'a, T>(&self, guard: &mut MutexGuard<'a, T>) {
52		self.inner.wait(guard);
53	}
54
55	#[inline]
56	pub fn wait_for<'a, T>(&self, guard: &mut MutexGuard<'a, T>, timeout: Duration) -> WaitTimeoutResult {
57		let timed_out = self.inner.wait_for(guard, timeout.to_std());
58		WaitTimeoutResult {
59			timed_out,
60		}
61	}
62
63	#[inline]
64	pub fn notify_one(&self) {
65		self.inner.notify_one();
66	}
67
68	#[inline]
69	pub fn notify_all(&self) {
70		self.inner.notify_all();
71	}
72}
73
74impl Default for Condvar {
75	#[inline]
76	fn default() -> Self {
77		Self::new()
78	}
79}