|
4 | 4 | all(target_os = "emscripten", target_feature = "atomics"), |
5 | 5 | target_os = "openbsd", |
6 | 6 | target_os = "netbsd", |
| 7 | + target_os = "dragonfly", |
7 | 8 | ))] |
8 | 9 |
|
9 | 10 | use crate::sync::atomic::AtomicU32; |
@@ -158,6 +159,35 @@ pub fn futex_wake_all(futex: &AtomicU32) { |
158 | 159 | } |
159 | 160 | } |
160 | 161 |
|
| 162 | +#[cfg(target_os = "dragonfly")] |
| 163 | +pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool { |
| 164 | + use crate::convert::TryFrom; |
| 165 | + let r = unsafe { |
| 166 | + libc::umtx_sleep( |
| 167 | + futex as *const AtomicU32 as *const i32, |
| 168 | + expected as i32, |
| 169 | + // A timeout of 0 means infinite, so we round smaller timeouts up to 1 millisecond. |
| 170 | + // Timeouts larger than i32::MAX milliseconds saturate. |
| 171 | + timeout.map_or(0, |d| { |
| 172 | + i32::try_from(d.as_millis()).map_or(i32::MAX, |millis| millis.max(1)) |
| 173 | + }), |
| 174 | + ) |
| 175 | + }; |
| 176 | + |
| 177 | + r == 0 || super::os::errno() != libc::ETIMEDOUT |
| 178 | +} |
| 179 | + |
| 180 | +// DragonflyBSD doesn't tell us how many threads are woken up, so this doesn't return a bool. |
| 181 | +#[cfg(target_os = "dragonfly")] |
| 182 | +pub fn futex_wake(futex: &AtomicU32) { |
| 183 | + unsafe { libc::umtx_wakeup(futex as *const AtomicU32 as *const i32, 1) }; |
| 184 | +} |
| 185 | + |
| 186 | +#[cfg(target_os = "dragonfly")] |
| 187 | +pub fn futex_wake_all(futex: &AtomicU32) { |
| 188 | + unsafe { libc::umtx_wakeup(futex as *const AtomicU32 as *const i32, i32::MAX) }; |
| 189 | +} |
| 190 | + |
161 | 191 | #[cfg(target_os = "emscripten")] |
162 | 192 | extern "C" { |
163 | 193 | fn emscripten_futex_wake(addr: *const AtomicU32, count: libc::c_int) -> libc::c_int; |
|
0 commit comments