std/sys/pal/unix/sync/
condvar.rs1use super::Mutex;
2use crate::cell::UnsafeCell;
3use crate::pin::Pin;
4#[cfg(not(target_os = "nto"))]
5use crate::sys::pal::time::TIMESPEC_MAX;
6#[cfg(target_os = "nto")]
7use crate::sys::pal::time::TIMESPEC_MAX_CAPPED;
8use crate::sys::pal::time::Timespec;
9use crate::time::Duration;
10
11pub struct Condvar {
12 inner: UnsafeCell<libc::pthread_cond_t>,
13}
14
15impl Condvar {
16 pub fn new() -> Condvar {
17 Condvar { inner: UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER) }
18 }
19
20 #[inline]
21 fn raw(&self) -> *mut libc::pthread_cond_t {
22 self.inner.get()
23 }
24
25 #[inline]
28 pub unsafe fn notify_one(self: Pin<&Self>) {
29 let r = unsafe { libc::pthread_cond_signal(self.raw()) };
30 debug_assert_eq!(r, 0);
31 }
32
33 #[inline]
36 pub unsafe fn notify_all(self: Pin<&Self>) {
37 let r = unsafe { libc::pthread_cond_broadcast(self.raw()) };
38 debug_assert_eq!(r, 0);
39 }
40
41 #[inline]
46 pub unsafe fn wait(self: Pin<&Self>, mutex: Pin<&Mutex>) {
47 let r = unsafe { libc::pthread_cond_wait(self.raw(), mutex.raw()) };
48 debug_assert_eq!(r, 0);
49 }
50
51 pub unsafe fn wait_timeout(&self, mutex: Pin<&Mutex>, dur: Duration) -> bool {
56 let mutex = mutex.raw();
57
58 #[cfg(target_vendor = "apple")]
68 let dur = Duration::min(dur, Duration::from_secs(1000 * 365 * 86400));
69
70 let timeout = Timespec::now(Self::CLOCK).checked_add_duration(&dur);
71
72 #[cfg(not(target_os = "nto"))]
73 let timeout = timeout.and_then(|t| t.to_timespec()).unwrap_or(TIMESPEC_MAX);
74
75 #[cfg(target_os = "nto")]
76 let timeout = timeout.and_then(|t| t.to_timespec_capped()).unwrap_or(TIMESPEC_MAX_CAPPED);
77
78 let r = unsafe { libc::pthread_cond_timedwait(self.raw(), mutex, &timeout) };
79 assert!(r == libc::ETIMEDOUT || r == 0);
80 r == 0
81 }
82}
83
84#[cfg(not(any(
85 target_os = "android",
86 target_vendor = "apple",
87 target_os = "espidf",
88 target_os = "horizon",
89 target_os = "l4re",
90 target_os = "redox",
91 target_os = "teeos",
92)))]
93impl Condvar {
94 pub const PRECISE_TIMEOUT: bool = true;
95 const CLOCK: libc::clockid_t = libc::CLOCK_MONOTONIC;
96
97 pub unsafe fn init(self: Pin<&mut Self>) {
100 use crate::mem::MaybeUninit;
101
102 struct AttrGuard<'a>(pub &'a mut MaybeUninit<libc::pthread_condattr_t>);
103 impl Drop for AttrGuard<'_> {
104 fn drop(&mut self) {
105 unsafe {
106 let result = libc::pthread_condattr_destroy(self.0.as_mut_ptr());
107 assert_eq!(result, 0);
108 }
109 }
110 }
111
112 unsafe {
113 let mut attr = MaybeUninit::<libc::pthread_condattr_t>::uninit();
114 let r = libc::pthread_condattr_init(attr.as_mut_ptr());
115 assert_eq!(r, 0);
116 let attr = AttrGuard(&mut attr);
117 let r = libc::pthread_condattr_setclock(attr.0.as_mut_ptr(), Self::CLOCK);
118 assert_eq!(r, 0);
119 let r = libc::pthread_cond_init(self.raw(), attr.0.as_ptr());
120 assert_eq!(r, 0);
121 }
122 }
123}
124
125#[cfg(any(
127 target_os = "android",
128 target_vendor = "apple",
129 target_os = "espidf",
130 target_os = "horizon",
131 target_os = "l4re",
132 target_os = "redox",
133 target_os = "teeos",
134))]
135impl Condvar {
136 pub const PRECISE_TIMEOUT: bool = false;
137 const CLOCK: libc::clockid_t = libc::CLOCK_REALTIME;
138
139 pub unsafe fn init(self: Pin<&mut Self>) {
142 if cfg!(any(target_os = "espidf", target_os = "horizon", target_os = "teeos")) {
143 let r = unsafe { libc::pthread_cond_init(self.raw(), crate::ptr::null()) };
148 assert_eq!(r, 0);
149 }
150 }
151}
152
153impl !Unpin for Condvar {}
154
155unsafe impl Sync for Condvar {}
156unsafe impl Send for Condvar {}
157
158impl Drop for Condvar {
159 #[inline]
160 fn drop(&mut self) {
161 let r = unsafe { libc::pthread_cond_destroy(self.raw()) };
162 if cfg!(target_os = "dragonfly") {
163 debug_assert!(r == 0 || r == libc::EINVAL);
168 } else {
169 debug_assert_eq!(r, 0);
170 }
171 }
172}