From f75d02d66975bed3636e89f56077bbc53fe5f74d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Wed, 11 May 2022 04:50:23 +0000 Subject: [PATCH 1/3] openbsd: convert futex timeout managment to Timespec usage --- library/std/src/sys/unix/futex.rs | 14 ++++++-------- library/std/src/sys/unix/time.rs | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/library/std/src/sys/unix/futex.rs b/library/std/src/sys/unix/futex.rs index 8d5ad18997d07..d70108e7bd627 100644 --- a/library/std/src/sys/unix/futex.rs +++ b/library/std/src/sys/unix/futex.rs @@ -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) -> 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( diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 333182bdad4de..df95f1494fd56 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -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 } } From 3cadc11d838d5f07b2849ba148dfb83c006fb70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Wed, 11 May 2022 04:50:48 +0000 Subject: [PATCH 2/3] avoid using both Some() and ? on linux/android/freebsd code --- library/std/src/sys/unix/futex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/futex.rs b/library/std/src/sys/unix/futex.rs index d70108e7bd627..8d05cb44b9479 100644 --- a/library/std/src/sys/unix/futex.rs +++ b/library/std/src/sys/unix/futex.rs @@ -25,7 +25,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option) - // // 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 { From 42f8e1f879487e3b0a48b9635eac11aaf7b672a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Wed, 11 May 2022 04:51:09 +0000 Subject: [PATCH 3/3] to_timespec could be unused by some targets --- library/std/src/sys/unix/time.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index df95f1494fd56..f99c453a3a85b 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -125,6 +125,7 @@ impl Timespec { Some(Timespec::new(secs, nsec as i64)) } + #[allow(dead_code)] pub fn to_timespec(&self) -> Option { Some(libc::timespec { tv_sec: self.tv_sec.try_into().ok()?,