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    all(target_os = "wasi", target_env = "p1") => {
85        mod wasip1;
86        pub use wasip1::{DEFAULT_MIN_STACK_SIZE, sleep, yield_now};
87        #[cfg(target_feature = "atomics")]
88        pub use wasip1::{Thread, available_parallelism};
89        #[expect(dead_code)]
90        mod unsupported;
91        pub use unsupported::{current_os_id, set_name};
92        #[cfg(not(target_feature = "atomics"))]
93        pub use unsupported::{Thread, available_parallelism};
94    }
95    all(target_os = "wasi", target_env = "p2") => {
96        mod wasip2;
97        pub use wasip2::{sleep, sleep_until};
98        #[expect(dead_code)]
99        mod unsupported;
100        // Note that unlike WASIp1 even if the wasm `atomics` feature is enabled
101        // there is no support for threads, not even experimentally, not even in
102        // wasi-libc. Thus this is unconditionally unsupported.
103        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, yield_now, DEFAULT_MIN_STACK_SIZE};
104    }
105    all(target_family = "wasm", target_feature = "atomics") => {
106        mod wasm;
107        pub use wasm::sleep;
108
109        #[expect(dead_code)]
110        mod unsupported;
111        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, yield_now, DEFAULT_MIN_STACK_SIZE};
112    }
113    target_os = "windows" => {
114        mod windows;
115        pub use windows::{Thread, available_parallelism, current_os_id, set_name, set_name_wide, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
116    }
117    target_os = "xous" => {
118        mod xous;
119        pub use xous::{Thread, available_parallelism, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
120
121        #[expect(dead_code)]
122        mod unsupported;
123        pub use unsupported::{current_os_id, set_name};
124    }
125    _ => {
126        mod unsupported;
127        pub use unsupported::{Thread, available_parallelism, current_os_id, set_name, sleep, yield_now, DEFAULT_MIN_STACK_SIZE};
128    }
129}
130
131#[cfg(not(any(
132    target_os = "freebsd",
133    target_os = "netbsd",
134    target_os = "linux",
135    target_os = "android",
136    target_os = "solaris",
137    target_os = "illumos",
138    target_os = "dragonfly",
139    target_os = "hurd",
140    target_os = "fuchsia",
141    target_os = "vxworks",
142    all(target_os = "wasi", target_env = "p2"),
143)))]
144pub fn sleep_until(deadline: crate::time::Instant) {
145    use crate::time::Instant;
146
147    let now = Instant::now();
148
149    if let Some(delay) = deadline.checked_duration_since(now) {
150        sleep(delay);
151    }
152}