Skip to content

Commit 9299e69

Browse files
committed
Round timeouts up to infinite in futex_wait on DragonFlyBSD.
1 parent 8ee9b93 commit 9299e69

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

library/std/src/sys/unix/futex.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,15 @@ pub fn futex_wake_all(futex: &AtomicU32) {
185185
#[cfg(target_os = "dragonfly")]
186186
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
187187
use crate::convert::TryFrom;
188+
189+
// A timeout of 0 means infinite.
190+
// We round smaller timeouts up to 1 millisecond.
191+
// Overflows are rounded up to an infinite timeout.
192+
let timeout_ms =
193+
timeout.and_then(|d| Some(i32::try_from(d.as_millis()).ok()?.max(1))).unwrap_or(0);
194+
188195
let r = unsafe {
189-
libc::umtx_sleep(
190-
futex as *const AtomicU32 as *const i32,
191-
expected as i32,
192-
// A timeout of 0 means infinite, so we round smaller timeouts up to 1 millisecond.
193-
// Timeouts larger than i32::MAX milliseconds saturate.
194-
timeout.map_or(0, |d| {
195-
i32::try_from(d.as_millis()).map_or(i32::MAX, |millis| millis.max(1))
196-
}),
197-
)
196+
libc::umtx_sleep(futex as *const AtomicU32 as *const i32, expected as i32, timeout_ms)
198197
};
199198

200199
r == 0 || super::os::errno() != libc::ETIMEDOUT

0 commit comments

Comments
 (0)