Skip to main content

kithara_platform/sync/
condvar.rs

1#[cfg(not(target_arch = "wasm32"))]
2use parking_lot::Condvar as ParkingLotCondvar;
3
4use super::MutexGuard;
5
6#[cfg(not(target_arch = "wasm32"))]
7pub struct Condvar(ParkingLotCondvar);
8
9#[cfg(not(target_arch = "wasm32"))]
10impl Condvar {
11    #[inline]
12    #[must_use]
13    // ast-grep-ignore: style.prefer-default-derive
14    pub fn new() -> Self {
15        Self(ParkingLotCondvar::new())
16    }
17
18    #[inline]
19    pub fn notify_all(&self) {
20        self.0.notify_all();
21    }
22
23    #[inline]
24    pub fn notify_one(&self) {
25        self.0.notify_one();
26    }
27
28    #[inline]
29    pub fn wait_sync<'a, T>(&self, mut guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
30        self.0.wait(&mut guard.0);
31        guard
32    }
33
34    #[inline]
35    pub fn wait_sync_timeout<'a, T>(
36        &self,
37        mut guard: MutexGuard<'a, T>,
38        deadline: web_time::Instant,
39    ) -> MutexGuard<'a, T> {
40        let _ = self.0.wait_until(&mut guard.0, deadline);
41        guard
42    }
43}
44
45#[cfg(not(target_arch = "wasm32"))]
46impl Default for Condvar {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52#[cfg(target_arch = "wasm32")]
53type WstCondvar = wasm_safe_thread::condvar::Condvar;
54
55#[cfg(target_arch = "wasm32")]
56pub struct Condvar(WstCondvar);
57
58#[cfg(target_arch = "wasm32")]
59impl Condvar {
60    #[inline]
61    #[must_use]
62    // ast-grep-ignore: style.prefer-default-derive
63    pub fn new() -> Self {
64        Self(WstCondvar::new())
65    }
66
67    #[inline]
68    pub fn notify_all(&self) {
69        self.0.notify_all();
70    }
71
72    #[inline]
73    pub fn notify_one(&self) {
74        self.0.notify_one();
75    }
76
77    #[inline]
78    pub fn wait_sync<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
79        MutexGuard(self.0.wait_sync(guard.0))
80    }
81
82    #[inline]
83    pub fn wait_sync_timeout<'a, T>(
84        &self,
85        guard: MutexGuard<'a, T>,
86        deadline: web_time::Instant,
87    ) -> MutexGuard<'a, T> {
88        let (g, _) = self.0.wait_sync_timeout(guard.0, deadline);
89        MutexGuard(g)
90    }
91}
92
93#[cfg(target_arch = "wasm32")]
94impl Default for Condvar {
95    fn default() -> Self {
96        Self::new()
97    }
98}