]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/thread.rs
Rollup merge of #74204 - ayazhafiz:i/74120, r=eddyb
[rust.git] / library / std / src / sys / wasi / thread.rs
1 use crate::ffi::CStr;
2 use crate::io;
3 use crate::mem;
4 use crate::sys::{unsupported, Void};
5 use crate::time::Duration;
6
7 pub struct Thread(Void);
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         let ret = unsafe { wasi::sched_yield() };
19         debug_assert_eq!(ret, Ok(()));
20     }
21
22     pub fn set_name(_name: &CStr) {
23         // nope
24     }
25
26     pub fn sleep(dur: Duration) {
27         let nanos = dur.as_nanos();
28         assert!(nanos <= u64::MAX as u128);
29
30         const USERDATA: wasi::Userdata = 0x0123_45678;
31
32         let clock = wasi::SubscriptionClock {
33             id: wasi::CLOCKID_MONOTONIC,
34             timeout: nanos as u64,
35             precision: 0,
36             flags: 0,
37         };
38
39         let in_ = wasi::Subscription {
40             userdata: USERDATA,
41             r#type: wasi::EVENTTYPE_CLOCK,
42             u: wasi::SubscriptionU { clock },
43         };
44         unsafe {
45             let mut event: wasi::Event = mem::zeroed();
46             let res = wasi::poll_oneoff(&in_, &mut event, 1);
47             match (res, event) {
48                 (
49                     Ok(1),
50                     wasi::Event {
51                         userdata: USERDATA, error: 0, r#type: wasi::EVENTTYPE_CLOCK, ..
52                     },
53                 ) => {}
54                 _ => panic!("thread::sleep(): unexpected result of poll_oneoff"),
55             }
56         }
57     }
58
59     pub fn join(self) {
60         match self.0 {}
61     }
62 }
63
64 pub mod guard {
65     pub type Guard = !;
66     pub unsafe fn current() -> Option<Guard> {
67         None
68     }
69     pub unsafe fn init() -> Option<Guard> {
70         None
71     }
72 }