Skip to main content

reifydb_runtime/sync/condvar/
native.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4//! Native Condvar implementation using parking_lot.
5
6use std::time::Duration;
7
8use parking_lot::Condvar;
9
10use crate::sync::mutex::MutexGuard;
11
12/// Native condition variable implementation wrapping Condvar.
13#[derive(Debug)]
14pub struct CondvarInner {
15	inner: Condvar,
16}
17
18impl CondvarInner {
19	/// Creates a new condition variable.
20	pub fn new() -> Self {
21		Self {
22			inner: Condvar::new(),
23		}
24	}
25
26	/// Blocks the current thread until notified.
27	pub fn wait<'a, T>(&self, guard: &mut MutexGuard<'a, T>) {
28		self.inner.wait(&mut guard.inner.inner);
29	}
30
31	/// Blocks the current thread until notified or the timeout expires.
32	/// Returns true if timed out.
33	pub fn wait_for<'a, T>(&self, guard: &mut MutexGuard<'a, T>, timeout: Duration) -> bool {
34		self.inner.wait_for(&mut guard.inner.inner, timeout).timed_out()
35	}
36
37	/// Wakes up one blocked thread.
38	pub fn notify_one(&self) {
39		self.inner.notify_one();
40	}
41
42	/// Wakes up all blocked threads.
43	pub fn notify_all(&self) {
44		self.inner.notify_all();
45	}
46}
47
48impl Default for CondvarInner {
49	fn default() -> Self {
50		Self::new()
51	}
52}