std/sys/thread/
mod.rs

1cfg_select! {
2    target_os = "hermit" => {
3        mod hermit;
4        pub use hermit::{Thread, available_parallelism, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
5        #[expect(dead_code)]
6        mod unsupported;
7        pub use unsupported::{current_os_id, set_name};
8    }
9    all(target_vendor = "fortanix", target_env = "sgx") => {
10        mod sgx;
11        pub use sgx::{Thread, current_os_id, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
12
13        // SGX should protect in-enclave data from outside attackers, so there
14        // must not be any data leakage to the OS, particularly no 1-1 mapping
15        // between SGX thread names and OS thread names. Hence `set_name` is
16        // intentionally a no-op.
17        //
18        // Note that the internally visible SGX thread name is already provided
19        // by the platform-agnostic Rust thread code. This can be observed in
20        // the [`std::thread::tests::test_named_thread`] test, which succeeds
21        // as-is with the SGX target.
22        #[expect(dead_code)]
23        mod unsupported;
24        pub use unsupported::{available_parallelism, set_name};
25    }
26    target_os = "solid_asp3" => {
27        mod solid;
28        pub use solid::{Thread, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
29        #[expect(dead_code)]
30        mod unsupported;
31        pub use unsupported::{available_parallelism, current_os_id, set_name};
32    }
33    target_os = "teeos" => {
34        mod teeos;
35        pub use teeos::{Thread, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
36        #[expect(dead_code)]
37        mod unsupported;
38        pub use unsupported::{available_parallelism, current_os_id, set_name};
39    }
40    target_os = "uefi" => {
41        mod uefi;
42        pub use uefi::{available_parallelism, sleep};
43        #[expect(dead_code)]
44        mod unsupported;
45        pub use unsupported::{Thread, current_os_id, set_name, yield_now, DEFAULT_MIN_STACK_SIZE};
46    }
47    target_family = "unix" => {
48        mod unix;
49        pub use unix::{Thread, available_parallelism, current_os_id, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
50        #[cfg(not(any(
51            target_env = "newlib",
52            target_os = "l4re",
53            target_os = "emscripten",
54            target_os = "redox",
55            target_os = "hurd",
56            target_os = "aix",
57        )))]
58        pub use unix::set_name;
59        #[cfg(any(
60            target_os = "freebsd",
61            target_os = "netbsd",
62            target_os = "linux",
63            target_os = "android",
64            target_os = "solaris",
65            target_os = "illumos",
66            target_os = "dragonfly",
67            target_os = "hurd",
68            target_os = "fuchsia",
69            target_os = "vxworks",
70        ))]
71        pub use unix::sleep_until;
72        #[expect(dead_code)]
73        mod unsupported;
74        #[cfg(any(
75            target_env = "newlib",
76            target_os = "l4re",
77            target_os = "emscripten",
78            target_os = "redox",
79            target_os = "hurd",
80            target_os = "aix",
81        ))]
82        pub use unsupported::set_name;
83    }
84    target_os = "vexos" => {
85        mod vexos;
86        pub use vexos::{sleep, yield_now};
87        #[expect(dead_code)]
88        mod unsupported;
89        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, DEFAULT_MIN_STACK_SIZE};
90    }
91    all(target_os = "wasi", target_env = "p1") => {
92        mod wasip1;
93        pub use wasip1::{DEFAULT_MIN_STACK_SIZE, sleep, yield_now};
94        #[cfg(target_feature = "atomics")]
95        pub use wasip1::{Thread, available_parallelism};
96        #[expect(dead_code)]
97        mod unsupported;
98        pub use unsupported::{current_os_id, set_name};
99        #[cfg(not(target_feature = "atomics"))]
100        pub use unsupported::{Thread, available_parallelism};
101    }
102    all(target_os = "wasi", target_env = "p2") => {
103        mod wasip2;
104        pub use wasip2::{sleep, sleep_until};
105        #[expect(dead_code)]
106        mod unsupported;
107        // Note that unlike WASIp1 even if the wasm `atomics` feature is enabled
108        // there is no support for threads, not even experimentally, not even in
109        // wasi-libc. Thus this is unconditionally unsupported.
110        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, yield_now, DEFAULT_MIN_STACK_SIZE};
111    }
112    all(target_family = "wasm", target_feature = "atomics") => {
113        mod wasm;
114        pub use wasm::sleep;
115
116        #[expect(dead_code)]
117        mod unsupported;
118        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, yield_now, DEFAULT_MIN_STACK_SIZE};
119    }
120    target_os = "windows" => {
121        mod windows;
122        pub use windows::{Thread, available_parallelism, current_os_id, set_name, set_name_wide, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
123    }
124    target_os = "xous" => {
125        mod xous;
126        pub use xous::{Thread, available_parallelism, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
127
128        #[expect(dead_code)]
129        mod unsupported;
130        pub use unsupported::{current_os_id, set_name};
131    }
132    _ => {
133        mod unsupported;
134        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
135    }
136}
137
138#[cfg(not(any(
139    target_os = "freebsd",
140    target_os = "netbsd",
141    target_os = "linux",
142    target_os = "android",
143    target_os = "solaris",
144    target_os = "illumos",
145    target_os = "dragonfly",
146    target_os = "hurd",
147    target_os = "fuchsia",
148    target_os = "vxworks",
149    all(target_os = "wasi", target_env = "p2"),
150)))]
151pub fn sleep_until(deadline: crate::time::Instant) {
152    use crate::time::Instant;
153
154    let now = Instant::now();
155
156    if let Some(delay) = deadline.checked_duration_since(now) {
157        sleep(delay);
158    }
159}