Skip to main content

std/sys/thread/
mod.rs

1cfg_select! {
2    target_os = "hermit" => {
3        mod hermit;
4        pub use hermit::{DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, sleep, yield_now};
5        #[expect(dead_code)]
6        mod unsupported;
7        pub use unsupported::{current_os_id, set_name};
8    }
9    target_os = "motor" => {
10        mod motor;
11        pub use motor::*;
12    }
13    all(target_vendor = "fortanix", target_env = "sgx") => {
14        mod sgx;
15        pub use sgx::{DEFAULT_MIN_STACK_SIZE, Thread, current_os_id, sleep, yield_now};
16
17        // SGX should protect in-enclave data from outside attackers, so there
18        // must not be any data leakage to the OS, particularly no 1-1 mapping
19        // between SGX thread names and OS thread names. Hence `set_name` is
20        // intentionally a no-op.
21        //
22        // Note that the internally visible SGX thread name is already provided
23        // by the platform-agnostic Rust thread code. This can be observed in
24        // the [`std::thread::tests::test_named_thread`] test, which succeeds
25        // as-is with the SGX target.
26        #[expect(dead_code)]
27        mod unsupported;
28        pub use unsupported::{available_parallelism, set_name};
29    }
30    target_os = "solid_asp3" => {
31        mod solid;
32        pub use solid::{DEFAULT_MIN_STACK_SIZE, Thread, sleep, yield_now};
33        #[expect(dead_code)]
34        mod unsupported;
35        pub use unsupported::{available_parallelism, current_os_id, set_name};
36    }
37    target_os = "teeos" => {
38        mod teeos;
39        pub use teeos::{DEFAULT_MIN_STACK_SIZE, Thread, sleep, yield_now};
40        #[expect(dead_code)]
41        mod unsupported;
42        pub use unsupported::{available_parallelism, current_os_id, set_name};
43    }
44    target_os = "uefi" => {
45        mod uefi;
46        pub use uefi::{available_parallelism, sleep};
47        #[expect(dead_code)]
48        mod unsupported;
49        pub use unsupported::{DEFAULT_MIN_STACK_SIZE, Thread, current_os_id, set_name, yield_now};
50    }
51    any(target_family = "unix", target_os = "wasi") => {
52        mod unix;
53        #[cfg(not(any(
54            target_env = "newlib",
55            target_os = "l4re",
56            target_os = "emscripten",
57            target_os = "redox",
58            target_os = "hurd",
59            target_os = "aix",
60            target_os = "wasi",
61        )))]
62        pub use unix::set_name;
63        #[cfg(any(
64            target_os = "freebsd",
65            target_os = "netbsd",
66            target_os = "linux",
67            target_os = "android",
68            target_os = "solaris",
69            target_os = "illumos",
70            target_os = "dragonfly",
71            target_os = "hurd",
72            target_os = "vxworks",
73            target_os = "wasi",
74            target_vendor = "apple",
75            target_os = "fuchsia",
76        ))]
77        pub use unix::sleep_until;
78        pub use unix::{
79            DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, current_os_id, sleep, yield_now,
80        };
81        #[expect(dead_code)]
82        mod unsupported;
83        #[cfg(any(
84            target_env = "newlib",
85            target_os = "l4re",
86            target_os = "emscripten",
87            target_os = "redox",
88            target_os = "hurd",
89            target_os = "aix",
90            target_os = "wasi",
91        ))]
92        pub use unsupported::set_name;
93    }
94    target_os = "vexos" => {
95        mod vexos;
96        pub use vexos::{sleep, sleep_until, yield_now};
97        #[expect(dead_code)]
98        mod unsupported;
99        pub use unsupported::{
100            DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, current_os_id, set_name,
101        };
102    }
103    all(target_family = "wasm", target_feature = "atomics") => {
104        mod wasm;
105        pub use wasm::sleep;
106
107        #[expect(dead_code)]
108        mod unsupported;
109        pub use unsupported::{
110            DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, current_os_id, set_name,
111            yield_now,
112        };
113    }
114    target_os = "windows" => {
115        mod windows;
116        pub use windows::{
117            DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, current_os_id, set_name,
118            set_name_wide, sleep, yield_now,
119        };
120    }
121    target_os = "xous" => {
122        mod xous;
123        pub use xous::{DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, sleep, yield_now};
124
125        #[expect(dead_code)]
126        mod unsupported;
127        pub use unsupported::{current_os_id, set_name};
128    }
129    _ => {
130        mod unsupported;
131        pub use unsupported::{
132            DEFAULT_MIN_STACK_SIZE, Thread, available_parallelism, current_os_id, set_name, sleep,
133            yield_now,
134        };
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 = "vxworks",
148    target_os = "wasi",
149    target_vendor = "apple",
150    target_os = "motor",
151    target_os = "vexos",
152    target_os = "fuchsia",
153)))]
154pub fn sleep_until(deadline: crate::time::Instant) {
155    use crate::time::Instant;
156
157    // The clock source used for `sleep` might not be the same used for `Instant`.
158    // Since this function *must not* return before the deadline, we recheck the
159    // time after every call to `sleep`. See #149935 for an example of this
160    // occurring on older Windows systems.
161    while let Some(delay) = deadline.checked_duration_since(Instant::now()) {
162        // Sleep for the estimated time remaining until the deadline.
163        //
164        // If your system has a better way of estimating the delay time or
165        // provides a way to sleep until an absolute time, specialize this
166        // function for your system.
167        sleep(delay);
168    }
169}