3 use crate::sys::{unsupported, Void};
4 use crate::time::Duration;
6 pub struct Thread(Void);
8 pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
11 // unsafe: see thread::Builder::spawn_unchecked for safety requirements
12 pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>)
22 pub fn set_name(_name: &CStr) {
26 #[cfg(not(target_feature = "atomics"))]
27 pub fn sleep(_dur: Duration) {
28 panic!("can't sleep");
31 #[cfg(target_feature = "atomics")]
32 pub fn sleep(dur: Duration) {
33 use crate::arch::wasm32;
36 // Use an atomic wait to block the current thread artificially with a
37 // timeout listed. Note that we should never be notified (return value
38 // of 0) or our comparison should never fail (return value of 1) so we
39 // should always only resume execution through a timeout (return value
41 let mut nanos = dur.as_nanos();
43 let amt = cmp::min(i64::max_value() as u128, nanos);
45 let val = unsafe { wasm32::i32_atomic_wait(&mut x, 0, amt as i64) };
46 debug_assert_eq!(val, 2);
58 pub unsafe fn current() -> Option<Guard> { None }
59 pub unsafe fn init() -> Option<Guard> { None }
63 if #[cfg(all(target_feature = "atomics", feature = "wasm-bindgen-threads"))] {
64 #[link(wasm_import_module = "__wbindgen_thread_xform__")]
66 fn __wbindgen_current_id() -> u32;
67 fn __wbindgen_tcb_get() -> u32;
68 fn __wbindgen_tcb_set(ptr: u32);
70 pub fn my_id() -> u32 {
71 unsafe { __wbindgen_current_id() }
74 // These are currently only ever used in `thread_local_atomics.rs`, if
75 // you'd like to use them be sure to update that and make sure everyone
76 // agrees what's what.
77 pub fn tcb_get() -> *mut u8 {
79 assert_eq!(mem::size_of::<*mut u8>(), mem::size_of::<u32>());
80 unsafe { __wbindgen_tcb_get() as *mut u8 }
83 pub fn tcb_set(ptr: *mut u8) {
84 unsafe { __wbindgen_tcb_set(ptr as u32); }
87 // FIXME: still need something for hooking exiting a thread to free
90 } else if #[cfg(target_feature = "atomics")] {
91 pub fn my_id() -> u32 {
92 panic!("thread ids not implemented on wasm with atomics yet")
95 pub fn tcb_get() -> *mut u8 {
96 panic!("thread local data not implemented on wasm with atomics yet")
99 pub fn tcb_set(_ptr: *mut u8) {
100 panic!("thread local data not implemented on wasm with atomics yet")
103 // stubbed out because no functions actually access these intrinsics
104 // unless atomics are enabled