]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasm/atomics/thread.rs
Rollup merge of #87453 - ibraheemdev:i-68697, r=wesleywiser
[rust.git] / library / std / src / sys / wasm / atomics / thread.rs
1 use crate::ffi::CStr;
2 use crate::io;
3 use crate::num::NonZeroUsize;
4 use crate::sys::unsupported;
5 use crate::time::Duration;
6
7 pub struct Thread(!);
8
9 pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
10
11 impl Thread {
12     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
13     pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
14         unsupported()
15     }
16
17     pub fn yield_now() {}
18
19     pub fn set_name(_name: &CStr) {}
20
21     pub fn sleep(dur: Duration) {
22         use crate::arch::wasm32;
23         use crate::cmp;
24
25         // Use an atomic wait to block the current thread artificially with a
26         // timeout listed. Note that we should never be notified (return value
27         // of 0) or our comparison should never fail (return value of 1) so we
28         // should always only resume execution through a timeout (return value
29         // 2).
30         let mut nanos = dur.as_nanos();
31         while nanos > 0 {
32             let amt = cmp::min(i64::MAX as u128, nanos);
33             let mut x = 0;
34             let val = unsafe { wasm32::memory_atomic_wait32(&mut x, 0, amt as i64) };
35             debug_assert_eq!(val, 2);
36             nanos -= amt;
37         }
38     }
39
40     pub fn join(self) {}
41 }
42
43 pub fn available_concurrency() -> io::Result<NonZeroUsize> {
44     unsupported()
45 }
46
47 pub mod guard {
48     pub type Guard = !;
49     pub unsafe fn current() -> Option<Guard> {
50         None
51     }
52     pub unsafe fn init() -> Option<Guard> {
53         None
54     }
55 }
56
57 // We currently just use our own thread-local to store our
58 // current thread's ID, and then we lazily initialize it to something allocated
59 // from a global counter.
60 pub fn my_id() -> u32 {
61     use crate::sync::atomic::{AtomicU32, Ordering::SeqCst};
62
63     static NEXT_ID: AtomicU32 = AtomicU32::new(0);
64
65     #[thread_local]
66     static mut MY_ID: u32 = 0;
67
68     unsafe {
69         // If our thread ID isn't set yet then we need to allocate one. Do so
70         // with with a simple "atomically add to a global counter" strategy.
71         // This strategy doesn't handled what happens when the counter
72         // overflows, however, so just abort everything once the counter
73         // overflows and eventually we could have some sort of recycling scheme
74         // (or maybe this is all totally irrelevant by that point!). In any case
75         // though we're using a CAS loop instead of a `fetch_add` to ensure that
76         // the global counter never overflows.
77         if MY_ID == 0 {
78             let mut cur = NEXT_ID.load(SeqCst);
79             MY_ID = loop {
80                 let next = cur.checked_add(1).unwrap_or_else(|| crate::process::abort());
81                 match NEXT_ID.compare_exchange(cur, next, SeqCst, SeqCst) {
82                     Ok(_) => break next,
83                     Err(i) => cur = i,
84                 }
85             };
86         }
87         MY_ID
88     }
89 }