|
| 1 | +//! Thread parking without `futex` using the `pthread` synchronization primitives. |
| 2 | +
|
| 3 | +#![cfg(not(any( |
| 4 | + target_os = "linux", |
| 5 | + target_os = "android", |
| 6 | + all(target_os = "emscripten", target_feature = "atomics") |
| 7 | +)))] |
| 8 | + |
| 9 | +use crate::cell::UnsafeCell; |
| 10 | +use crate::marker::PhantomPinned; |
| 11 | +use crate::pin::Pin; |
| 12 | +use crate::ptr::addr_of_mut; |
| 13 | +use crate::sync::atomic::AtomicUsize; |
| 14 | +use crate::sync::atomic::Ordering::SeqCst; |
| 15 | +use crate::time::Duration; |
| 16 | + |
| 17 | +const EMPTY: usize = 0; |
| 18 | +const PARKED: usize = 1; |
| 19 | +const NOTIFIED: usize = 2; |
| 20 | + |
| 21 | +unsafe fn lock(lock: *mut libc::pthread_mutex_t) { |
| 22 | + let r = libc::pthread_mutex_lock(lock); |
| 23 | + debug_assert_eq!(r, 0); |
| 24 | +} |
| 25 | + |
| 26 | +unsafe fn unlock(lock: *mut libc::pthread_mutex_t) { |
| 27 | + let r = libc::pthread_mutex_unlock(lock); |
| 28 | + debug_assert_eq!(r, 0); |
| 29 | +} |
| 30 | + |
| 31 | +unsafe fn notify_one(cond: *mut libc::pthread_cond_t) { |
| 32 | + let r = libc::pthread_cond_signal(cond); |
| 33 | + debug_assert_eq!(r, 0); |
| 34 | +} |
| 35 | + |
| 36 | +unsafe fn wait(cond: *mut libc::pthread_cond_t, lock: *mut libc::pthread_mutex_t) { |
| 37 | + let r = libc::pthread_cond_wait(cond, lock); |
| 38 | + debug_assert_eq!(r, 0); |
| 39 | +} |
| 40 | + |
| 41 | +const TIMESPEC_MAX: libc::timespec = |
| 42 | + libc::timespec { tv_sec: <libc::time_t>::MAX, tv_nsec: 1_000_000_000 - 1 }; |
| 43 | + |
| 44 | +fn saturating_cast_to_time_t(value: u64) -> libc::time_t { |
| 45 | + if value > <libc::time_t>::MAX as u64 { <libc::time_t>::MAX } else { value as libc::time_t } |
| 46 | +} |
| 47 | + |
| 48 | +// This implementation is used on systems that support pthread_condattr_setclock |
| 49 | +// where we configure the condition variable to use the monotonic clock (instead of |
| 50 | +// the default system clock). This approach avoids all problems that result |
| 51 | +// from changes made to the system time. |
| 52 | +#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "espidf")))] |
| 53 | +unsafe fn wait_timeout( |
| 54 | + cond: *mut libc::pthread_cond_t, |
| 55 | + lock: *mut libc::pthread_mutex_t, |
| 56 | + dur: Duration, |
| 57 | +) { |
| 58 | + use crate::mem; |
| 59 | + |
| 60 | + let mut now: libc::timespec = mem::zeroed(); |
| 61 | + let r = libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now); |
| 62 | + assert_eq!(r, 0); |
| 63 | + // Nanosecond calculations can't overflow because both values are below 1e9. |
| 64 | + let nsec = dur.subsec_nanos() + now.tv_nsec as u32; |
| 65 | + let sec = saturating_cast_to_time_t(dur.as_secs()) |
| 66 | + .checked_add((nsec / 1_000_000_000) as libc::time_t) |
| 67 | + .and_then(|s| s.checked_add(now.tv_sec)); |
| 68 | + let nsec = nsec % 1_000_000_000; |
| 69 | + let timeout = |
| 70 | + sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX); |
| 71 | + let r = libc::pthread_cond_timedwait(cond, lock, &timeout); |
| 72 | + assert!(r == libc::ETIMEDOUT || r == 0); |
| 73 | +} |
| 74 | + |
| 75 | +// This implementation is modeled after libcxx's condition_variable |
| 76 | +// https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46 |
| 77 | +// https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367 |
| 78 | +#[cfg(any(target_os = "macos", target_os = "ios", target_os = "espidf"))] |
| 79 | +unsafe fn wait_timeout( |
| 80 | + cond: *mut libc::pthread_cond_t, |
| 81 | + lock: *mut libc::pthread_mutex_t, |
| 82 | + mut dur: Duration, |
| 83 | +) { |
| 84 | + use crate::ptr; |
| 85 | + |
| 86 | + // 1000 years |
| 87 | + let max_dur = Duration::from_secs(1000 * 365 * 86400); |
| 88 | + |
| 89 | + if dur > max_dur { |
| 90 | + // OSX implementation of `pthread_cond_timedwait` is buggy |
| 91 | + // with super long durations. When duration is greater than |
| 92 | + // 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait` |
| 93 | + // in macOS Sierra return error 316. |
| 94 | + // |
| 95 | + // This program demonstrates the issue: |
| 96 | + // https://gist.github.com/stepancheg/198db4623a20aad2ad7cddb8fda4a63c |
| 97 | + // |
| 98 | + // To work around this issue, and possible bugs of other OSes, timeout |
| 99 | + // is clamped to 1000 years, which is allowable per the API of `park_timeout` |
| 100 | + // because of spurious wakeups. |
| 101 | + dur = max_dur; |
| 102 | + } |
| 103 | + |
| 104 | + let mut sys_now = libc::timeval { tv_sec: 0, tv_usec: 0 }; |
| 105 | + let r = libc::gettimeofday(&mut sys_now, ptr::null_mut()); |
| 106 | + debug_assert_eq!(r, 0); |
| 107 | + let nsec = dur.subsec_nanos() as libc::c_long + (sys_now.tv_usec * 1000) as libc::c_long; |
| 108 | + let extra = (nsec / 1_000_000_000) as libc::time_t; |
| 109 | + let nsec = nsec % 1_000_000_000; |
| 110 | + let seconds = saturating_cast_to_time_t(dur.as_secs()); |
| 111 | + let timeout = sys_now |
| 112 | + .tv_sec |
| 113 | + .checked_add(extra) |
| 114 | + .and_then(|s| s.checked_add(seconds)) |
| 115 | + .map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec }) |
| 116 | + .unwrap_or(TIMESPEC_MAX); |
| 117 | + // And wait! |
| 118 | + let r = libc::pthread_cond_timedwait(cond, lock, &timeout); |
| 119 | + debug_assert!(r == libc::ETIMEDOUT || r == 0); |
| 120 | +} |
| 121 | + |
| 122 | +pub struct Parker { |
| 123 | + state: AtomicUsize, |
| 124 | + lock: UnsafeCell<libc::pthread_mutex_t>, |
| 125 | + cvar: UnsafeCell<libc::pthread_cond_t>, |
| 126 | + // The `pthread` primitives require a stable address, so make this struct `!Unpin`. |
| 127 | + _pinned: PhantomPinned, |
| 128 | +} |
| 129 | + |
| 130 | +impl Parker { |
| 131 | + /// Construct the UNIX parker in-place. |
| 132 | + /// |
| 133 | + /// # Safety |
| 134 | + /// The constructed parker must never be moved. |
| 135 | + pub unsafe fn new(parker: *mut Parker) { |
| 136 | + // Use the default mutex implementation to allow for simpler initialization. |
| 137 | + // This could lead to undefined behaviour when deadlocking. This is avoided |
| 138 | + // by not deadlocking. Note in particular the unlocking operation before any |
| 139 | + // panic, as code after the panic could try to park again. |
| 140 | + addr_of_mut!((*parker).state).write(AtomicUsize::new(EMPTY)); |
| 141 | + addr_of_mut!((*parker).lock).write(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER)); |
| 142 | + |
| 143 | + cfg_if::cfg_if! { |
| 144 | + if #[cfg(any( |
| 145 | + target_os = "macos", |
| 146 | + target_os = "ios", |
| 147 | + target_os = "l4re", |
| 148 | + target_os = "android", |
| 149 | + target_os = "redox" |
| 150 | + ))] { |
| 151 | + addr_of_mut!((*parker).cvar).write(UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER)); |
| 152 | + } else if #[cfg(target_os = "espidf")] { |
| 153 | + let r = libc::pthread_cond_init(addr_of_mut!((*parker).cvar).cast(), crate::ptr::null()); |
| 154 | + assert_eq!(r, 0); |
| 155 | + } else { |
| 156 | + use crate::mem::MaybeUninit; |
| 157 | + let mut attr = MaybeUninit::<libc::pthread_condattr_t>::uninit(); |
| 158 | + let r = libc::pthread_condattr_init(attr.as_mut_ptr()); |
| 159 | + assert_eq!(r, 0); |
| 160 | + let r = libc::pthread_condattr_setclock(attr.as_mut_ptr(), libc::CLOCK_MONOTONIC); |
| 161 | + assert_eq!(r, 0); |
| 162 | + let r = libc::pthread_cond_init(addr_of_mut!((*parker).cvar).cast(), attr.as_ptr()); |
| 163 | + assert_eq!(r, 0); |
| 164 | + let r = libc::pthread_condattr_destroy(attr.as_mut_ptr()); |
| 165 | + assert_eq!(r, 0); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // This implementation doesn't require `unsafe`, but other implementations |
| 171 | + // may assume this is only called by the thread that owns the Parker. |
| 172 | + pub unsafe fn park(self: Pin<&Self>) { |
| 173 | + // If we were previously notified then we consume this notification and |
| 174 | + // return quickly. |
| 175 | + if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + // Otherwise we need to coordinate going to sleep |
| 180 | + lock(self.lock.get()); |
| 181 | + match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) { |
| 182 | + Ok(_) => {} |
| 183 | + Err(NOTIFIED) => { |
| 184 | + // We must read here, even though we know it will be `NOTIFIED`. |
| 185 | + // This is because `unpark` may have been called again since we read |
| 186 | + // `NOTIFIED` in the `compare_exchange` above. We must perform an |
| 187 | + // acquire operation that synchronizes with that `unpark` to observe |
| 188 | + // any writes it made before the call to unpark. To do that we must |
| 189 | + // read from the write it made to `state`. |
| 190 | + let old = self.state.swap(EMPTY, SeqCst); |
| 191 | + |
| 192 | + unlock(self.lock.get()); |
| 193 | + |
| 194 | + assert_eq!(old, NOTIFIED, "park state changed unexpectedly"); |
| 195 | + return; |
| 196 | + } // should consume this notification, so prohibit spurious wakeups in next park. |
| 197 | + Err(_) => { |
| 198 | + unlock(self.lock.get()); |
| 199 | + |
| 200 | + panic!("inconsistent park state") |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + loop { |
| 205 | + wait(self.cvar.get(), self.lock.get()); |
| 206 | + |
| 207 | + match self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst) { |
| 208 | + Ok(_) => break, // got a notification |
| 209 | + Err(_) => {} // spurious wakeup, go back to sleep |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + unlock(self.lock.get()); |
| 214 | + } |
| 215 | + |
| 216 | + // This implementation doesn't require `unsafe`, but other implementations |
| 217 | + // may assume this is only called by the thread that owns the Parker. Use |
| 218 | + // `Pin` to guarantee a stable address for the mutex and condition variable. |
| 219 | + pub unsafe fn park_timeout(self: Pin<&Self>, dur: Duration) { |
| 220 | + // Like `park` above we have a fast path for an already-notified thread, and |
| 221 | + // afterwards we start coordinating for a sleep. |
| 222 | + // return quickly. |
| 223 | + if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + lock(self.lock.get()); |
| 228 | + match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) { |
| 229 | + Ok(_) => {} |
| 230 | + Err(NOTIFIED) => { |
| 231 | + // We must read again here, see `park`. |
| 232 | + let old = self.state.swap(EMPTY, SeqCst); |
| 233 | + unlock(self.lock.get()); |
| 234 | + |
| 235 | + assert_eq!(old, NOTIFIED, "park state changed unexpectedly"); |
| 236 | + return; |
| 237 | + } // should consume this notification, so prohibit spurious wakeups in next park. |
| 238 | + Err(_) => { |
| 239 | + unlock(self.lock.get()); |
| 240 | + panic!("inconsistent park_timeout state") |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + // Wait with a timeout, and if we spuriously wake up or otherwise wake up |
| 245 | + // from a notification we just want to unconditionally set the state back to |
| 246 | + // empty, either consuming a notification or un-flagging ourselves as |
| 247 | + // parked. |
| 248 | + wait_timeout(self.cvar.get(), self.lock.get(), dur); |
| 249 | + |
| 250 | + match self.state.swap(EMPTY, SeqCst) { |
| 251 | + NOTIFIED => unlock(self.lock.get()), // got a notification, hurray! |
| 252 | + PARKED => unlock(self.lock.get()), // no notification, alas |
| 253 | + n => { |
| 254 | + unlock(self.lock.get()); |
| 255 | + panic!("inconsistent park_timeout state: {n}") |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + pub fn unpark(self: Pin<&Self>) { |
| 261 | + // To ensure the unparked thread will observe any writes we made |
| 262 | + // before this call, we must perform a release operation that `park` |
| 263 | + // can synchronize with. To do that we must write `NOTIFIED` even if |
| 264 | + // `state` is already `NOTIFIED`. That is why this must be a swap |
| 265 | + // rather than a compare-and-swap that returns if it reads `NOTIFIED` |
| 266 | + // on failure. |
| 267 | + match self.state.swap(NOTIFIED, SeqCst) { |
| 268 | + EMPTY => return, // no one was waiting |
| 269 | + NOTIFIED => return, // already unparked |
| 270 | + PARKED => {} // gotta go wake someone up |
| 271 | + _ => panic!("inconsistent state in unpark"), |
| 272 | + } |
| 273 | + |
| 274 | + // There is a period between when the parked thread sets `state` to |
| 275 | + // `PARKED` (or last checked `state` in the case of a spurious wake |
| 276 | + // up) and when it actually waits on `cvar`. If we were to notify |
| 277 | + // during this period it would be ignored and then when the parked |
| 278 | + // thread went to sleep it would never wake up. Fortunately, it has |
| 279 | + // `lock` locked at this stage so we can acquire `lock` to wait until |
| 280 | + // it is ready to receive the notification. |
| 281 | + // |
| 282 | + // Releasing `lock` before the call to `notify_one` means that when the |
| 283 | + // parked thread wakes it doesn't get woken only to have to wait for us |
| 284 | + // to release `lock`. |
| 285 | + unsafe { |
| 286 | + lock(self.lock.get()); |
| 287 | + unlock(self.lock.get()); |
| 288 | + notify_one(self.cvar.get()); |
| 289 | + } |
| 290 | + } |
| 291 | +} |
| 292 | + |
| 293 | +impl Drop for Parker { |
| 294 | + fn drop(&mut self) { |
| 295 | + unsafe { |
| 296 | + libc::pthread_cond_destroy(self.cvar.get_mut()); |
| 297 | + libc::pthread_mutex_destroy(self.lock.get_mut()); |
| 298 | + } |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +unsafe impl Sync for Parker {} |
| 303 | +unsafe impl Send for Parker {} |
0 commit comments