]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasm/thread.rs
Simplify SaveHandler trait
[rust.git] / src / libstd / sys / wasm / thread.rs
1 use crate::ffi::CStr;
2 use crate::io;
3 use crate::sys::{unsupported, Void};
4 use crate::time::Duration;
5
6 pub struct Thread(Void);
7
8 pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
9
10 impl Thread {
11     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
12     pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>)
13         -> io::Result<Thread>
14     {
15         unsupported()
16     }
17
18     pub fn yield_now() {
19         // do nothing
20     }
21
22     pub fn set_name(_name: &CStr) {
23         // nope
24     }
25
26     #[cfg(not(target_feature = "atomics"))]
27     pub fn sleep(_dur: Duration) {
28         panic!("can't sleep");
29     }
30
31     #[cfg(target_feature = "atomics")]
32     pub fn sleep(dur: Duration) {
33         use crate::arch::wasm32;
34         use crate::cmp;
35
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
40         // 2).
41         let mut nanos = dur.as_nanos();
42         while nanos > 0 {
43             let amt = cmp::min(i64::max_value() as u128, nanos);
44             let mut x = 0;
45             let val = unsafe { wasm32::i32_atomic_wait(&mut x, 0, amt as i64) };
46             debug_assert_eq!(val, 2);
47             nanos -= amt;
48         }
49     }
50
51     pub fn join(self) {
52         match self.0 {}
53     }
54 }
55
56 pub mod guard {
57     pub type Guard = !;
58     pub unsafe fn current() -> Option<Guard> { None }
59     pub unsafe fn init() -> Option<Guard> { None }
60 }
61
62 cfg_if::cfg_if! {
63     if #[cfg(all(target_feature = "atomics", feature = "wasm-bindgen-threads"))] {
64         #[link(wasm_import_module = "__wbindgen_thread_xform__")]
65         extern {
66             fn __wbindgen_current_id() -> u32;
67             fn __wbindgen_tcb_get() -> u32;
68             fn __wbindgen_tcb_set(ptr: u32);
69         }
70         pub fn my_id() -> u32 {
71             unsafe { __wbindgen_current_id() }
72         }
73
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 {
78             use crate::mem;
79             assert_eq!(mem::size_of::<*mut u8>(), mem::size_of::<u32>());
80             unsafe { __wbindgen_tcb_get() as *mut u8 }
81         }
82
83         pub fn tcb_set(ptr: *mut u8) {
84             unsafe { __wbindgen_tcb_set(ptr as u32); }
85         }
86
87         // FIXME: still need something for hooking exiting a thread to free
88         // data...
89
90     } else if #[cfg(target_feature = "atomics")] {
91         pub fn my_id() -> u32 {
92             panic!("thread ids not implemented on wasm with atomics yet")
93         }
94
95         pub fn tcb_get() -> *mut u8 {
96             panic!("thread local data not implemented on wasm with atomics yet")
97         }
98
99         pub fn tcb_set(_ptr: *mut u8) {
100             panic!("thread local data not implemented on wasm with atomics yet")
101         }
102     } else {
103         // stubbed out because no functions actually access these intrinsics
104         // unless atomics are enabled
105     }
106 }