diff options
| author | bors <bors@rust-lang.org> | 2025-09-11 22:31:43 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-09-11 22:31:43 +0000 |
| commit | 2a9bacf6187685931d52346a0ecff2e52bdc91cc (patch) | |
| tree | 89917948fb14e749c6441b3d7b7fd98fb015577a /library/std/src/sys/thread/wasm.rs | |
| parent | 5eda692e73f37dcbe2437ce878db7bb71f323e74 (diff) | |
| parent | f049b6147c15496d70b69490dbcaef4f824f1cdf (diff) | |
| download | rust-2a9bacf6187685931d52346a0ecff2e52bdc91cc.tar.gz rust-2a9bacf6187685931d52346a0ecff2e52bdc91cc.zip | |
Auto merge of #145177 - joboet:move-pal-thread, r=ibraheemdev
std: move `thread` into `sys` Part of https://github.com/rust-lang/rust/issues/117276.
Diffstat (limited to 'library/std/src/sys/thread/wasm.rs')
| -rw-r--r-- | library/std/src/sys/thread/wasm.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/library/std/src/sys/thread/wasm.rs b/library/std/src/sys/thread/wasm.rs new file mode 100644 index 00000000000..e843bc992ba --- /dev/null +++ b/library/std/src/sys/thread/wasm.rs @@ -0,0 +1,23 @@ +use crate::cmp; +use crate::time::Duration; + +pub fn sleep(dur: Duration) { + #[cfg(target_arch = "wasm32")] + use core::arch::wasm32 as wasm; + #[cfg(target_arch = "wasm64")] + use core::arch::wasm64 as wasm; + + // Use an atomic wait to block the current thread artificially with a + // timeout listed. Note that we should never be notified (return value + // of 0) or our comparison should never fail (return value of 1) so we + // should always only resume execution through a timeout (return value + // 2). + let mut nanos = dur.as_nanos(); + while nanos > 0 { + let amt = cmp::min(i64::MAX as u128, nanos); + let mut x = 0; + let val = unsafe { wasm::memory_atomic_wait32(&mut x, 0, amt as i64) }; + debug_assert_eq!(val, 2); + nanos -= amt; + } +} |
