Skip to content

Commit 80e1dec

Browse files
authored
Rollup merge of #96860 - semarie:openbsd-futex-time64, r=cuviper
openbsd: convert futex timeout managment to Timespec usage unbreak openbsd build after #96657 r? cuviper please note I made `Timespec::zero()` public to be able to use it. OpenBSD is using relative timeout for `futex(2)` and I don't find simple way to use `Timespec` this way.
2 parents 43dabbf + 42f8e1f commit 80e1dec

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
2525
//
2626
// Overflows are rounded up to an infinite timeout (None).
2727
let timespec = timeout
28-
.and_then(|d| Some(Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d)?))
28+
.and_then(|d| Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d))
2929
.and_then(|t| t.to_timespec());
3030

3131
loop {
@@ -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

+2-1
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

@@ -125,6 +125,7 @@ impl Timespec {
125125
Some(Timespec::new(secs, nsec as i64))
126126
}
127127

128+
#[allow(dead_code)]
128129
pub fn to_timespec(&self) -> Option<libc::timespec> {
129130
Some(libc::timespec {
130131
tv_sec: self.tv_sec.try_into().ok()?,

0 commit comments

Comments
 (0)