about summary refs log tree commit diff
path: root/library/std/src/sys/thread/wasm.rs
blob: e843bc992ba1b2d26cc06b7fb67e12d6cd983c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
    }
}