3 use crate::num::NonZeroUsize;
4 use crate::sys::unsupported;
5 use crate::time::Duration;
9 pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
12 // unsafe: see thread::Builder::spawn_unchecked for safety requirements
13 pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
19 pub fn set_name(_name: &CStr) {}
21 pub fn sleep(dur: Duration) {
22 use crate::arch::wasm32;
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
30 let mut nanos = dur.as_nanos();
32 let amt = cmp::min(i64::MAX as u128, nanos);
34 let val = unsafe { wasm32::memory_atomic_wait32(&mut x, 0, amt as i64) };
35 debug_assert_eq!(val, 2);
43 pub fn available_concurrency() -> io::Result<NonZeroUsize> {
49 pub unsafe fn current() -> Option<Guard> {
52 pub unsafe fn init() -> Option<Guard> {
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};
63 static NEXT_ID: AtomicU32 = AtomicU32::new(0);
66 static mut MY_ID: u32 = 0;
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.
78 let mut cur = NEXT_ID.load(SeqCst);
80 let next = cur.checked_add(1).unwrap_or_else(|| crate::process::abort());
81 match NEXT_ID.compare_exchange(cur, next, SeqCst, SeqCst) {