Skip to content

Commit

Permalink
Rollup merge of rust-lang#96860 - semarie:openbsd-futex-time64, r=cuv…
Browse files Browse the repository at this point in the history
…iper

openbsd: convert futex timeout managment to Timespec usage

unbreak openbsd build after rust-lang#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.
  • Loading branch information
matthiaskrgr authored May 12, 2022
2 parents 43dabbf + 42f8e1f commit 80e1dec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
16 changes: 7 additions & 9 deletions library/std/src/sys/unix/futex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
//
// Overflows are rounded up to an infinite timeout (None).
let timespec = timeout
.and_then(|d| Some(Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d)?))
.and_then(|d| Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d))
.and_then(|t| t.to_timespec());

loop {
Expand Down Expand Up @@ -136,15 +136,13 @@ pub fn futex_wake_all(futex: &AtomicU32) {

#[cfg(target_os = "openbsd")]
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
use super::time::Timespec;
use crate::ptr::{null, null_mut};
let timespec = timeout.and_then(|d| {
Some(libc::timespec {
// Sleep forever if the timeout is longer than fits in a timespec.
tv_sec: d.as_secs().try_into().ok()?,
// This conversion never truncates, as subsec_nanos is always <1e9.
tv_nsec: d.subsec_nanos() as _,
})
});

// Overflows are rounded up to an infinite timeout (None).
let timespec = timeout
.and_then(|d| Timespec::zero().checked_add_duration(&d))
.and_then(|t| t.to_timespec());

let r = unsafe {
libc::futex(
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl fmt::Debug for SystemTime {
}

impl Timespec {
const fn zero() -> Timespec {
pub const fn zero() -> Timespec {
Timespec { tv_sec: 0, tv_nsec: 0 }
}

Expand Down Expand Up @@ -125,6 +125,7 @@ impl Timespec {
Some(Timespec::new(secs, nsec as i64))
}

#[allow(dead_code)]
pub fn to_timespec(&self) -> Option<libc::timespec> {
Some(libc::timespec {
tv_sec: self.tv_sec.try_into().ok()?,
Expand Down

0 comments on commit 80e1dec

Please sign in to comment.