|
| 1 | +use crate::ffi::{c_int, c_void}; |
| 2 | +use crate::ptr; |
| 3 | +use crate::time::Duration; |
| 4 | +use libc::{_lwp_self, clockid_t, lwpid_t, time_t, timespec, CLOCK_MONOTONIC}; |
| 5 | + |
| 6 | +extern "C" { |
| 7 | + fn ___lwp_park60( |
| 8 | + clock_id: clockid_t, |
| 9 | + flags: c_int, |
| 10 | + ts: *mut timespec, |
| 11 | + unpark: lwpid_t, |
| 12 | + hint: *const c_void, |
| 13 | + unparkhint: *const c_void, |
| 14 | + ) -> c_int; |
| 15 | + fn _lwp_unpark(lwp: lwpid_t, hint: *const c_void) -> c_int; |
| 16 | +} |
| 17 | + |
| 18 | +pub type ThreadId = lwpid_t; |
| 19 | + |
| 20 | +#[inline] |
| 21 | +pub fn current() -> ThreadId { |
| 22 | + unsafe { _lwp_self() } |
| 23 | +} |
| 24 | + |
| 25 | +#[inline] |
| 26 | +pub fn park(hint: usize) { |
| 27 | + unsafe { |
| 28 | + ___lwp_park60(0, 0, ptr::null_mut(), 0, ptr::invalid(hint), ptr::null()); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +pub fn park_timeout(dur: Duration, hint: usize) { |
| 33 | + let mut timeout = timespec { |
| 34 | + // Saturate so that the operation will definitely time out |
| 35 | + // (even if it is after the heat death of the universe). |
| 36 | + tv_sec: dur.as_secs().try_into().ok().unwrap_or(time_t::MAX), |
| 37 | + tv_nsec: dur.subsec_nanos().into(), |
| 38 | + }; |
| 39 | + |
| 40 | + // Timeout needs to be mutable since it is modified on NetBSD 9.0 and |
| 41 | + // above. |
| 42 | + unsafe { |
| 43 | + ___lwp_park60(CLOCK_MONOTONIC, 0, &mut timeout, 0, ptr::invalid(hint), ptr::null()); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[inline] |
| 48 | +pub fn unpark(tid: ThreadId, hint: usize) { |
| 49 | + unsafe { |
| 50 | + _lwp_unpark(tid, ptr::invalid(hint)); |
| 51 | + } |
| 52 | +} |
0 commit comments