about summary refs log tree commit diff
path: root/library/std/src/sys/pal/wasm/atomics/thread.rs
diff options
context:
space:
mode:
authordvdsk <noreply@davidsk.dev>2025-05-31 15:33:46 +0200
committerdvdsk <noreply@davidsk.dev>2025-07-06 17:36:49 +0200
commitf24ee2c9b15dc3734e9ebd74ba0563a1e6c545db (patch)
tree6b8ace653301356d698114d42424806510998147 /library/std/src/sys/pal/wasm/atomics/thread.rs
parent35f6036521777bdc0dcea1f980be4c192962a168 (diff)
downloadrust-f24ee2c9b15dc3734e9ebd74ba0563a1e6c545db.tar.gz
rust-f24ee2c9b15dc3734e9ebd74ba0563a1e6c545db.zip
sleep_until: use clock_nanosleep where possible
Using clock nanosleep leads to more accurate sleep times on platforms
where it is supported.

To enable using clock_nanosleep this makes `sleep_until` platform
specific. That unfortunatly requires identical placeholder
implementations for the other platforms (windows/mac/wasm etc).

we will land platform specific implementations for those later. See the
`sleep_until` tracking issue.

This requires an accessors for the Instant type. As that accessor is only
used on the platforms that have clock_nanosleep it is marked as allow_unused.

32bit time_t targets do not use clock_nanosleep atm, they instead rely
on the same placeholder as the other platforms. We could make them
use clock_nanosleep too in the future using `__clock_nanosleep_time64`.

__clock_nanosleep_time64 is documented at:
https://www.gnu.org/software/libc/manual/html_node/64_002dbit-time-symbol-handling.html
Diffstat (limited to 'library/std/src/sys/pal/wasm/atomics/thread.rs')
-rw-r--r--library/std/src/sys/pal/wasm/atomics/thread.rs10
1 files changed, 9 insertions, 1 deletions
diff --git a/library/std/src/sys/pal/wasm/atomics/thread.rs b/library/std/src/sys/pal/wasm/atomics/thread.rs
index dd5aff391fd..44ce3eab109 100644
--- a/library/std/src/sys/pal/wasm/atomics/thread.rs
+++ b/library/std/src/sys/pal/wasm/atomics/thread.rs
@@ -2,7 +2,7 @@ use crate::ffi::CStr;
 use crate::io;
 use crate::num::NonZero;
 use crate::sys::unsupported;
-use crate::time::Duration;
+use crate::time::{Duration, Instant};
 
 pub struct Thread(!);
 
@@ -41,6 +41,14 @@ impl Thread {
         }
     }
 
+    pub fn sleep_until(deadline: Instant) {
+        let now = Instant::now();
+
+        if let Some(delay) = deadline.checked_duration_since(now) {
+            Self::sleep(delay);
+        }
+    }
+
     pub fn join(self) {}
 }