]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/thread.rs
Rollup merge of #102913 - SparrowLii:import-candidate, r=compiler-errors
[rust.git] / library / std / src / sys / hermit / thread.rs
1 #![allow(dead_code)]
2
3 use super::unsupported;
4 use crate::ffi::CStr;
5 use crate::io;
6 use crate::mem;
7 use crate::num::NonZeroUsize;
8 use crate::sys::hermit::abi;
9 use crate::sys::hermit::thread_local_dtor::run_dtors;
10 use crate::time::Duration;
11
12 pub type Tid = abi::Tid;
13
14 pub struct Thread {
15     tid: Tid,
16 }
17
18 unsafe impl Send for Thread {}
19 unsafe impl Sync for Thread {}
20
21 pub const DEFAULT_MIN_STACK_SIZE: usize = 1 << 20;
22
23 impl Thread {
24     pub unsafe fn new_with_coreid(
25         stack: usize,
26         p: Box<dyn FnOnce()>,
27         core_id: isize,
28     ) -> io::Result<Thread> {
29         let p = Box::into_raw(box p);
30         let tid = abi::spawn2(
31             thread_start,
32             p as usize,
33             abi::Priority::into(abi::NORMAL_PRIO),
34             stack,
35             core_id,
36         );
37
38         return if tid == 0 {
39             // The thread failed to start and as a result p was not consumed. Therefore, it is
40             // safe to reconstruct the box so that it gets deallocated.
41             drop(Box::from_raw(p));
42             Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Unable to create thread!"))
43         } else {
44             Ok(Thread { tid: tid })
45         };
46
47         extern "C" fn thread_start(main: usize) {
48             unsafe {
49                 // Finally, let's run some code.
50                 Box::from_raw(main as *mut Box<dyn FnOnce()>)();
51
52                 // run all destructors
53                 run_dtors();
54             }
55         }
56     }
57
58     pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
59         Thread::new_with_coreid(stack, p, -1 /* = no specific core */)
60     }
61
62     #[inline]
63     pub fn yield_now() {
64         unsafe {
65             abi::yield_now();
66         }
67     }
68
69     #[inline]
70     pub fn set_name(_name: &CStr) {
71         // nope
72     }
73
74     #[inline]
75     pub fn sleep(dur: Duration) {
76         unsafe {
77             abi::usleep(dur.as_micros() as u64);
78         }
79     }
80
81     pub fn join(self) {
82         unsafe {
83             let _ = abi::join(self.tid);
84         }
85     }
86
87     #[inline]
88     pub fn id(&self) -> Tid {
89         self.tid
90     }
91
92     #[inline]
93     pub fn into_id(self) -> Tid {
94         let id = self.tid;
95         mem::forget(self);
96         id
97     }
98 }
99
100 pub fn available_parallelism() -> io::Result<NonZeroUsize> {
101     unsupported()
102 }
103
104 pub mod guard {
105     pub type Guard = !;
106     pub unsafe fn current() -> Option<Guard> {
107         None
108     }
109     pub unsafe fn init() -> Option<Guard> {
110         None
111     }
112 }