Skip to content

Commit f75d02d

Browse files
committed
openbsd: convert futex timeout managment to Timespec usage
1 parent 532be94 commit f75d02d

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,13 @@ pub fn futex_wake_all(futex: &AtomicU32) {
136136

137137
#[cfg(target_os = "openbsd")]
138138
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
139+
use super::time::Timespec;
139140
use crate::ptr::{null, null_mut};
140-
let timespec = timeout.and_then(|d| {
141-
Some(libc::timespec {
142-
// Sleep forever if the timeout is longer than fits in a timespec.
143-
tv_sec: d.as_secs().try_into().ok()?,
144-
// This conversion never truncates, as subsec_nanos is always <1e9.
145-
tv_nsec: d.subsec_nanos() as _,
146-
})
147-
});
141+
142+
// Overflows are rounded up to an infinite timeout (None).
143+
let timespec = timeout
144+
.and_then(|d| Timespec::zero().checked_add_duration(&d))
145+
.and_then(|t| t.to_timespec());
148146

149147
let r = unsafe {
150148
libc::futex(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl fmt::Debug for SystemTime {
5151
}
5252

5353
impl Timespec {
54-
const fn zero() -> Timespec {
54+
pub const fn zero() -> Timespec {
5555
Timespec { tv_sec: 0, tv_nsec: 0 }
5656
}
5757

0 commit comments

Comments
 (0)