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