]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/thread.rs
Merge commit '645ef505da378b6f810b1567806d1bcc2856395f' into clippyup
[rust.git] / library / std / src / sys / wasi / thread.rs
1 #![deny(unsafe_op_in_unsafe_fn)]
2
3 use crate::ffi::CStr;
4 use crate::io;
5 use crate::mem;
6 use crate::sys::{unsupported, Void};
7 use crate::time::Duration;
8
9 pub struct Thread(Void);
10
11 pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
12
13 impl Thread {
14     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
15     pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
16         unsupported()
17     }
18
19     pub fn yield_now() {
20         let ret = unsafe { wasi::sched_yield() };
21         debug_assert_eq!(ret, Ok(()));
22     }
23
24     pub fn set_name(_name: &CStr) {
25         // nope
26     }
27
28     pub fn sleep(dur: Duration) {
29         let nanos = dur.as_nanos();
30         assert!(nanos <= u64::MAX as u128);
31
32         const USERDATA: wasi::Userdata = 0x0123_45678;
33
34         let clock = wasi::SubscriptionClock {
35             id: wasi::CLOCKID_MONOTONIC,
36             timeout: nanos as u64,
37             precision: 0,
38             flags: 0,
39         };
40
41         let in_ = wasi::Subscription {
42             userdata: USERDATA,
43             r#type: wasi::EVENTTYPE_CLOCK,
44             u: wasi::SubscriptionU { clock },
45         };
46         unsafe {
47             let mut event: wasi::Event = mem::zeroed();
48             let res = wasi::poll_oneoff(&in_, &mut event, 1);
49             match (res, event) {
50                 (
51                     Ok(1),
52                     wasi::Event {
53                         userdata: USERDATA, error: 0, r#type: wasi::EVENTTYPE_CLOCK, ..
54                     },
55                 ) => {}
56                 _ => panic!("thread::sleep(): unexpected result of poll_oneoff"),
57             }
58         }
59     }
60
61     pub fn join(self) {
62         match self.0 {}
63     }
64 }
65
66 pub mod guard {
67     pub type Guard = !;
68     pub unsafe fn current() -> Option<Guard> {
69         None
70     }
71     pub unsafe fn init() -> Option<Guard> {
72         None
73     }
74 }