Skip to main content

std/sys/thread/
unix.rs

1#[cfg(not(any(
2    target_env = "newlib",
3    target_os = "l4re",
4    target_os = "emscripten",
5    target_os = "redox",
6    target_os = "hurd",
7    target_os = "aix",
8    target_os = "wasi",
9)))]
10use crate::ffi::CStr;
11use crate::mem::{self, DropGuard, ManuallyDrop};
12use crate::num::NonZero;
13#[cfg(all(target_os = "linux", target_env = "gnu"))]
14use crate::sys::weak::dlsym;
15#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto",))]
16use crate::sys::weak::weak;
17use crate::thread::ThreadInit;
18use crate::time::Duration;
19use crate::{cmp, io, ptr, sys};
20#[cfg(not(any(
21    target_os = "l4re",
22    target_os = "vxworks",
23    target_os = "espidf",
24    target_os = "nuttx"
25)))]
26pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
27#[cfg(target_os = "l4re")]
28pub const DEFAULT_MIN_STACK_SIZE: usize = 1024 * 1024;
29#[cfg(target_os = "vxworks")]
30pub const DEFAULT_MIN_STACK_SIZE: usize = 256 * 1024;
31#[cfg(any(target_os = "espidf", target_os = "nuttx"))]
32pub const DEFAULT_MIN_STACK_SIZE: usize = 0; // 0 indicates that the stack size configured in the ESP-IDF/NuttX menuconfig system should be used
33
34pub struct Thread {
35    id: libc::pthread_t,
36}
37
38// Some platforms may have pthread_t as a pointer in which case we still want
39// a thread to be Send/Sync
40unsafe impl Send for Thread {}
41unsafe impl Sync for Thread {}
42
43impl Thread {
44    // unsafe: see thread::Builder::spawn_unchecked for safety requirements
45    #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
46    pub unsafe fn new(stack: usize, init: Box<ThreadInit>) -> io::Result<Thread> {
47        // FIXME: remove this block once wasi-sdk is updated with the fix from
48        // https://github.com/WebAssembly/wasi-libc/pull/716
49        // WASI does not support threading via pthreads. While wasi-libc provides
50        // pthread stubs, pthread_create returns EAGAIN, which causes confusing
51        // errors. We return UNSUPPORTED_PLATFORM directly instead.
52        if cfg!(all(target_os = "wasi", not(target_feature = "atomics"))) {
53            return Err(io::Error::UNSUPPORTED_PLATFORM);
54        }
55
56        let data = init;
57        let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
58        assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
59        let mut attr = DropGuard::new(&mut attr, |attr| {
60            assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0)
61        });
62
63        #[cfg(any(target_os = "espidf", target_os = "nuttx"))]
64        if stack > 0 {
65            // Only set the stack if a non-zero value is passed
66            // 0 is used as an indication that the default stack size configured in the ESP-IDF/NuttX menuconfig system should be used
67            assert_eq!(
68                libc::pthread_attr_setstacksize(
69                    attr.as_mut_ptr(),
70                    cmp::max(stack, min_stack_size(attr.as_ptr()))
71                ),
72                0
73            );
74        }
75
76        #[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
77        {
78            let stack_size = cmp::max(stack, min_stack_size(attr.as_ptr()));
79
80            match libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) {
81                0 => {}
82                n => {
83                    assert_eq!(n, libc::EINVAL);
84                    // EINVAL means |stack_size| is either too small or not a
85                    // multiple of the system page size. Because it's definitely
86                    // >= PTHREAD_STACK_MIN, it must be an alignment issue.
87                    // Round up to the nearest page and try again.
88                    let page_size = sys::os::page_size();
89                    let stack_size =
90                        (stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
91
92                    // Some libc implementations, e.g. musl, place an upper bound
93                    // on the stack size, in which case we can only gracefully return
94                    // an error here.
95                    if libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) != 0 {
96                        return Err(io::const_error!(
97                            io::ErrorKind::InvalidInput,
98                            "invalid stack size"
99                        ));
100                    }
101                }
102            };
103        }
104
105        let data = Box::into_raw(data);
106        let mut native: libc::pthread_t = mem::zeroed();
107        let ret = libc::pthread_create(&mut native, attr.as_ptr(), thread_start, data as *mut _);
108        return if ret == 0 {
109            Ok(Thread { id: native })
110        } else {
111            // The thread failed to start and as a result `data` was not consumed.
112            // Therefore, it is safe to reconstruct the box so that it gets deallocated.
113            drop(Box::from_raw(data));
114            Err(io::Error::from_raw_os_error(ret))
115        };
116
117        extern "C" fn thread_start(data: *mut libc::c_void) -> *mut libc::c_void {
118            unsafe {
119                // SAFETY: we are simply recreating the box that was leaked earlier.
120                let init = Box::from_raw(data as *mut ThreadInit);
121                let rust_start = init.init();
122
123                // Now that the thread information is set, set up our stack
124                // overflow handler.
125                let _handler = sys::stack_overflow::Handler::new();
126
127                rust_start();
128            }
129            ptr::null_mut()
130        }
131    }
132
133    pub fn join(self) {
134        let id = self.into_id();
135        let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };
136        assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret));
137    }
138
139    #[cfg(not(target_os = "wasi"))]
140    pub fn id(&self) -> libc::pthread_t {
141        self.id
142    }
143
144    pub fn into_id(self) -> libc::pthread_t {
145        ManuallyDrop::new(self).id
146    }
147}
148
149impl Drop for Thread {
150    fn drop(&mut self) {
151        let ret = unsafe { libc::pthread_detach(self.id) };
152        debug_assert_eq!(ret, 0);
153    }
154}
155
156pub fn available_parallelism() -> io::Result<NonZero<usize>> {
157    cfg_select! {
158        any(
159            target_os = "android",
160            target_os = "emscripten",
161            target_os = "fuchsia",
162            target_os = "hurd",
163            target_os = "linux",
164            target_os = "aix",
165            target_vendor = "apple",
166            target_os = "cygwin",
167        ) => {
168            #[allow(unused_assignments)]
169            #[allow(unused_mut)]
170            let mut quota = usize::MAX;
171
172            #[cfg(any(target_os = "android", target_os = "linux"))]
173            {
174                quota = cgroups::quota().max(1);
175                let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
176                unsafe {
177                    if libc::sched_getaffinity(0, size_of::<libc::cpu_set_t>(), &mut set) == 0 {
178                        let count = libc::CPU_COUNT(&set) as usize;
179                        let count = count.min(quota);
180
181                        // According to sched_getaffinity's API it should always be non-zero, but
182                        // some old MIPS kernels were buggy and zero-initialized the mask if
183                        // none was explicitly set.
184                        // In that case we use the sysconf fallback.
185                        if let Some(count) = NonZero::new(count) {
186                            return Ok(count)
187                        }
188                    }
189                }
190            }
191            match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
192                -1 => Err(io::Error::last_os_error()),
193                0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
194                cpus => {
195                    let count = cpus as usize;
196                    // Cover the unusual situation where we were able to get the quota but not the affinity mask
197                    let count = count.min(quota);
198                    Ok(unsafe { NonZero::new_unchecked(count) })
199                }
200            }
201        }
202        any(
203           target_os = "freebsd",
204           target_os = "dragonfly",
205           target_os = "openbsd",
206           target_os = "netbsd",
207        ) => {
208            use crate::ptr;
209
210            #[cfg(target_os = "freebsd")]
211            {
212                let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
213                unsafe {
214                    if libc::cpuset_getaffinity(
215                        libc::CPU_LEVEL_WHICH,
216                        libc::CPU_WHICH_PID,
217                        -1,
218                        size_of::<libc::cpuset_t>(),
219                        &mut set,
220                    ) == 0 {
221                        let count = libc::CPU_COUNT(&set) as usize;
222                        if count > 0 {
223                            return Ok(NonZero::new_unchecked(count));
224                        }
225                    }
226                }
227            }
228
229            #[cfg(target_os = "netbsd")]
230            {
231                unsafe {
232                    let set = libc::_cpuset_create();
233                    if !set.is_null() {
234                        let mut count: usize = 0;
235                        if libc::pthread_getaffinity_np(libc::pthread_self(), libc::_cpuset_size(set), set) == 0 {
236                            for i in 0..libc::cpuid_t::MAX {
237                                match libc::_cpuset_isset(i, set) {
238                                    -1 => break,
239                                    0 => continue,
240                                    _ => count = count + 1,
241                                }
242                            }
243                        }
244                        libc::_cpuset_destroy(set);
245                        if let Some(count) = NonZero::new(count) {
246                            return Ok(count);
247                        }
248                    }
249                }
250            }
251
252            let mut cpus: libc::c_uint = 0;
253            let mut cpus_size = size_of_val(&cpus);
254
255            unsafe {
256                cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
257            }
258
259            // Fallback approach in case of errors or no hardware threads.
260            if cpus < 1 {
261                let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
262                let res = unsafe {
263                    libc::sysctl(
264                        mib.as_mut_ptr(),
265                        2,
266                        (&raw mut cpus) as *mut _,
267                        (&raw mut cpus_size) as *mut _,
268                        ptr::null_mut(),
269                        0,
270                    )
271                };
272
273                // Handle errors if any.
274                if res == -1 {
275                    return Err(io::Error::last_os_error());
276                } else if cpus == 0 {
277                    return Err(io::Error::UNKNOWN_THREAD_COUNT);
278                }
279            }
280
281            Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
282        }
283        target_os = "nto" => {
284            unsafe {
285                use libc::_syspage_ptr;
286                if _syspage_ptr.is_null() {
287                    Err(io::const_error!(io::ErrorKind::NotFound, "no syspage available"))
288                } else {
289                    let cpus = (*_syspage_ptr).num_cpu;
290                    NonZero::new(cpus as usize)
291                        .ok_or(io::Error::UNKNOWN_THREAD_COUNT)
292                }
293            }
294        }
295        any(target_os = "solaris", target_os = "illumos") => {
296            let mut cpus = 0u32;
297            if unsafe { libc::pset_info(libc::PS_MYID, core::ptr::null_mut(), &mut cpus, core::ptr::null_mut()) } != 0 {
298                return Err(io::Error::UNKNOWN_THREAD_COUNT);
299            }
300            Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
301        }
302        target_os = "haiku" => {
303            // system_info cpu_count field gets the static data set at boot time with `smp_set_num_cpus`
304            // `get_system_info` calls then `smp_get_num_cpus`
305            unsafe {
306                let mut sinfo: libc::system_info = crate::mem::zeroed();
307                let res = libc::get_system_info(&mut sinfo);
308
309                if res != libc::B_OK {
310                    return Err(io::Error::UNKNOWN_THREAD_COUNT);
311                }
312
313                Ok(NonZero::new_unchecked(sinfo.cpu_count as usize))
314            }
315        }
316        target_os = "vxworks" => {
317            // Note: there is also `vxCpuConfiguredGet`, closer to _SC_NPROCESSORS_CONF
318            // expectations than the actual cores availability.
319
320            // SAFETY: `vxCpuEnabledGet` always fetches a mask with at least one bit set
321            unsafe{
322                let set = libc::vxCpuEnabledGet();
323                Ok(NonZero::new_unchecked(set.count_ones() as usize))
324            }
325        }
326        _ => {
327            // FIXME: implement on Redox, l4re
328            Err(io::const_error!(io::ErrorKind::Unsupported, "getting the number of hardware threads is not supported on the target platform"))
329        }
330    }
331}
332
333pub fn current_os_id() -> Option<u64> {
334    // Most Unix platforms have a way to query an integer ID of the current thread, all with
335    // slightly different spellings.
336    //
337    // The OS thread ID is used rather than `pthread_self` so as to match what will be displayed
338    // for process inspection (debuggers, trace, `top`, etc.).
339    cfg_select! {
340        // Most platforms have a function returning a `pid_t` or int, which is an `i32`.
341        any(target_os = "android", target_os = "linux") => {
342            use crate::sys::pal::weak::syscall;
343
344            // `libc::gettid` is only available on glibc 2.30+, but the syscall is available
345            // since Linux 2.4.11.
346            syscall!(fn gettid() -> libc::pid_t;);
347
348            // SAFETY: FFI call with no preconditions.
349            let id: libc::pid_t = unsafe { gettid() };
350            Some(id as u64)
351        }
352        target_os = "nto" => {
353            // SAFETY: FFI call with no preconditions.
354            let id: libc::pid_t = unsafe { libc::gettid() };
355            Some(id as u64)
356        }
357        target_os = "openbsd" => {
358            // SAFETY: FFI call with no preconditions.
359            let id: libc::pid_t = unsafe { libc::getthrid() };
360            Some(id as u64)
361        }
362        target_os = "freebsd" => {
363            // SAFETY: FFI call with no preconditions.
364            let id: libc::c_int = unsafe { libc::pthread_getthreadid_np() };
365            Some(id as u64)
366        }
367        target_os = "netbsd" => {
368            // SAFETY: FFI call with no preconditions.
369            let id: libc::lwpid_t = unsafe { libc::_lwp_self() };
370            Some(id as u64)
371        }
372        any(target_os = "illumos", target_os = "solaris") => {
373            // On Illumos and Solaris, the `pthread_t` is the same as the OS thread ID.
374            // SAFETY: FFI call with no preconditions.
375            let id: libc::pthread_t = unsafe { libc::pthread_self() };
376            Some(id as u64)
377        }
378        target_vendor = "apple" => {
379            // Apple allows querying arbitrary thread IDs, `thread=NULL` queries the current thread.
380            let mut id = 0u64;
381            // SAFETY: `thread_id` is a valid pointer, no other preconditions.
382            let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) };
383            if status == 0 {
384                Some(id)
385            } else {
386                None
387            }
388        }
389        // Other platforms don't have an OS thread ID or don't have a way to access it.
390        _ => None,
391    }
392}
393
394#[cfg(any(
395    target_os = "linux",
396    target_os = "nto",
397    target_os = "solaris",
398    target_os = "illumos",
399    target_os = "vxworks",
400    target_os = "cygwin",
401    target_vendor = "apple",
402))]
403fn truncate_cstr<const MAX_WITH_NUL: usize>(cstr: &CStr) -> [libc::c_char; MAX_WITH_NUL] {
404    let mut result = [0; MAX_WITH_NUL];
405    for (src, dst) in cstr.to_bytes().iter().zip(&mut result[..MAX_WITH_NUL - 1]) {
406        *dst = *src as libc::c_char;
407    }
408    result
409}
410
411#[cfg(target_os = "android")]
412pub fn set_name(name: &CStr) {
413    const PR_SET_NAME: libc::c_int = 15;
414    unsafe {
415        let res = libc::prctl(
416            PR_SET_NAME,
417            name.as_ptr(),
418            0 as libc::c_ulong,
419            0 as libc::c_ulong,
420            0 as libc::c_ulong,
421        );
422        // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
423        debug_assert_eq!(res, 0);
424    }
425}
426
427#[cfg(any(
428    target_os = "linux",
429    target_os = "freebsd",
430    target_os = "dragonfly",
431    target_os = "nuttx",
432    target_os = "cygwin"
433))]
434pub fn set_name(name: &CStr) {
435    unsafe {
436        cfg_select! {
437            any(target_os = "linux", target_os = "cygwin") => {
438                // Linux and Cygwin limits the allowed length of the name.
439                const TASK_COMM_LEN: usize = 16;
440                let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
441            }
442            _ => {
443                // FreeBSD, DragonFly BSD and NuttX do not enforce length limits.
444            }
445        };
446        // Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20 for Linux,
447        // FreeBSD 12.2 and 13.0, and DragonFly BSD 6.0.
448        let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
449        // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
450        debug_assert_eq!(res, 0);
451    }
452}
453
454#[cfg(target_os = "openbsd")]
455pub fn set_name(name: &CStr) {
456    unsafe {
457        libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr());
458    }
459}
460
461#[cfg(target_vendor = "apple")]
462pub fn set_name(name: &CStr) {
463    unsafe {
464        let name = truncate_cstr::<{ libc::MAXTHREADNAMESIZE }>(name);
465        let res = libc::pthread_setname_np(name.as_ptr());
466        // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
467        debug_assert_eq!(res, 0);
468    }
469}
470
471#[cfg(target_os = "netbsd")]
472pub fn set_name(name: &CStr) {
473    unsafe {
474        let res = libc::pthread_setname_np(
475            libc::pthread_self(),
476            c"%s".as_ptr(),
477            name.as_ptr() as *mut libc::c_void,
478        );
479        debug_assert_eq!(res, 0);
480    }
481}
482
483#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto"))]
484pub fn set_name(name: &CStr) {
485    weak!(
486        fn pthread_setname_np(thread: libc::pthread_t, name: *const libc::c_char) -> libc::c_int;
487    );
488
489    if let Some(f) = pthread_setname_np.get() {
490        #[cfg(target_os = "nto")]
491        const THREAD_NAME_MAX: usize = libc::_NTO_THREAD_NAME_MAX as usize;
492        #[cfg(any(target_os = "solaris", target_os = "illumos"))]
493        const THREAD_NAME_MAX: usize = 32;
494
495        let name = truncate_cstr::<{ THREAD_NAME_MAX }>(name);
496        let res = unsafe { f(libc::pthread_self(), name.as_ptr()) };
497        debug_assert_eq!(res, 0);
498    }
499}
500
501#[cfg(target_os = "fuchsia")]
502pub fn set_name(name: &CStr) {
503    use crate::sys::pal::fuchsia::*;
504    unsafe {
505        zx_object_set_property(
506            zx_thread_self(),
507            ZX_PROP_NAME,
508            name.as_ptr() as *const libc::c_void,
509            name.to_bytes().len(),
510        );
511    }
512}
513
514#[cfg(target_os = "haiku")]
515pub fn set_name(name: &CStr) {
516    unsafe {
517        let thread_self = libc::find_thread(ptr::null_mut());
518        let res = libc::rename_thread(thread_self, name.as_ptr());
519        // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
520        debug_assert_eq!(res, libc::B_OK);
521    }
522}
523
524#[cfg(target_os = "vxworks")]
525pub fn set_name(name: &CStr) {
526    let mut name = truncate_cstr::<{ (libc::VX_TASK_RENAME_LENGTH - 1) as usize }>(name);
527    let res = unsafe { libc::taskNameSet(libc::taskIdSelf(), name.as_mut_ptr()) };
528    debug_assert_eq!(res, libc::OK);
529}
530
531#[cfg(not(target_os = "espidf"))]
532pub fn sleep(dur: Duration) {
533    cfg_select! {
534        // Any unix that has clock_nanosleep
535        // If this list changes update the MIRI chock_nanosleep shim
536        any(
537            target_os = "freebsd",
538            target_os = "netbsd",
539            target_os = "linux",
540            target_os = "android",
541            target_os = "solaris",
542            target_os = "illumos",
543            target_os = "dragonfly",
544            target_os = "hurd",
545            target_os = "vxworks",
546            target_os = "wasi",
547        ) => {
548            // POSIX specifies that `nanosleep` uses CLOCK_REALTIME, but is not
549            // affected by clock adjustments. The timing of `sleep` however should
550            // be tied to `Instant` where possible. Thus, we use `clock_nanosleep`
551            // with a relative time interval instead, which allows explicitly
552            // specifying the clock.
553            //
554            // In practice, most systems (like e.g. Linux) actually use
555            // CLOCK_MONOTONIC for `nanosleep` anyway, but others like FreeBSD don't
556            // so it's better to be safe.
557            //
558            // wasi-libc prior to WebAssembly/wasi-libc#696 has a broken implementation
559            // of `nanosleep` which used `CLOCK_REALTIME` even though it is unsupported
560            // on WASIp2. Using `clock_nanosleep` directly bypasses the issue.
561            unsafe fn nanosleep(rqtp: *const libc::timespec, rmtp: *mut libc::timespec) -> libc::c_int {
562                unsafe { libc::clock_nanosleep(crate::sys::time::Instant::CLOCK_ID, 0, rqtp, rmtp) }
563            }
564        }
565        _ => {
566            unsafe fn nanosleep(rqtp: *const libc::timespec, rmtp: *mut libc::timespec) -> libc::c_int {
567                let r = unsafe { libc::nanosleep(rqtp, rmtp) };
568                // `clock_nanosleep` returns the error number directly, so mimic
569                // that behaviour to make the shared code below simpler.
570                if r == 0 { 0 } else { sys::io::errno() }
571            }
572        }
573    }
574
575    let mut secs = dur.as_secs();
576    let mut nsecs = dur.subsec_nanos() as _;
577
578    // If we're awoken with a signal then the return value will be -1 and
579    // nanosleep will fill in `ts` with the remaining time.
580    unsafe {
581        while secs > 0 || nsecs > 0 {
582            let mut ts = libc::timespec {
583                tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
584                tv_nsec: nsecs,
585            };
586            secs -= ts.tv_sec as u64;
587            let ts_ptr = &raw mut ts;
588            let r = nanosleep(ts_ptr, ts_ptr);
589            if r != 0 {
590                assert_eq!(r, libc::EINTR);
591                secs += ts.tv_sec as u64;
592                nsecs = ts.tv_nsec;
593            } else {
594                nsecs = 0;
595            }
596        }
597    }
598}
599
600#[cfg(target_os = "espidf")]
601pub fn sleep(dur: Duration) {
602    // ESP-IDF does not have `nanosleep`, so we use `usleep` instead.
603    // As per the documentation of `usleep`, it is expected to support
604    // sleep times as big as at least up to 1 second.
605    //
606    // ESP-IDF does support almost up to `u32::MAX`, but due to a potential integer overflow in its
607    // `usleep` implementation
608    // (https://github.com/espressif/esp-idf/blob/d7ca8b94c852052e3bc33292287ef4dd62c9eeb1/components/newlib/time.c#L210),
609    // we limit the sleep time to the maximum one that would not cause the underlying `usleep` implementation to overflow
610    // (`portTICK_PERIOD_MS` can be anything between 1 to 1000, and is 10 by default).
611    const MAX_MICROS: u32 = u32::MAX - 1_000_000 - 1;
612
613    // Add any nanoseconds smaller than a microsecond as an extra microsecond
614    // so as to comply with the `std::thread::sleep` contract which mandates
615    // implementations to sleep for _at least_ the provided `dur`.
616    // We can't overflow `micros` as it is a `u128`, while `Duration` is a pair of
617    // (`u64` secs, `u32` nanos), where the nanos are strictly smaller than 1 second
618    // (i.e. < 1_000_000_000)
619    let mut micros = dur.as_micros() + if dur.subsec_nanos() % 1_000 > 0 { 1 } else { 0 };
620
621    while micros > 0 {
622        let st = if micros > MAX_MICROS as u128 { MAX_MICROS } else { micros as u32 };
623        unsafe {
624            libc::usleep(st);
625        }
626
627        micros -= st as u128;
628    }
629}
630
631// Any unix that has clock_nanosleep
632// If this list changes update the MIRI chock_nanosleep shim
633#[cfg(any(
634    target_os = "freebsd",
635    target_os = "netbsd",
636    target_os = "linux",
637    target_os = "android",
638    target_os = "solaris",
639    target_os = "illumos",
640    target_os = "dragonfly",
641    target_os = "hurd",
642    target_os = "vxworks",
643    target_os = "wasi",
644))]
645pub fn sleep_until(deadline: crate::time::Instant) {
646    use crate::time::Instant;
647
648    #[cfg(all(
649        target_os = "linux",
650        target_env = "gnu",
651        target_pointer_width = "32",
652        not(target_arch = "riscv32")
653    ))]
654    {
655        use crate::sys::pal::time::__timespec64;
656        use crate::sys::pal::weak::weak;
657
658        // This got added in glibc 2.31, along with a 64-bit `clock_gettime`
659        // function.
660        weak! {
661            fn __clock_nanosleep_time64(
662                clock_id: libc::clockid_t,
663                flags: libc::c_int,
664                req: *const __timespec64,
665                rem: *mut __timespec64,
666            ) -> libc::c_int;
667        }
668
669        if let Some(clock_nanosleep) = __clock_nanosleep_time64.get() {
670            let ts = deadline.into_inner().into_timespec().to_timespec64();
671            loop {
672                let r = unsafe {
673                    clock_nanosleep(
674                        crate::sys::time::Instant::CLOCK_ID,
675                        libc::TIMER_ABSTIME,
676                        &ts,
677                        core::ptr::null_mut(),
678                    )
679                };
680
681                match r {
682                    0 => return,
683                    libc::EINTR => continue,
684                    // If the underlying kernel doesn't support the 64-bit
685                    // syscall, `__clock_nanosleep_time64` will fail. The
686                    // error code nowadays is EOVERFLOW, but it used to be
687                    // ENOSYS – so just don't rely on any particular value.
688                    // The parameters are all valid, so the only reasons
689                    // why the call might fail are EINTR and the call not
690                    // being supported. Fall through to the clamping version
691                    // in that case.
692                    _ => break,
693                }
694            }
695        }
696    }
697
698    let Some(ts) = deadline.into_inner().into_timespec().to_timespec() else {
699        // The deadline is further in the future then can be passed to
700        // clock_nanosleep. We have to use Self::sleep instead. This might
701        // happen on 32 bit platforms, especially closer to 2038.
702        let now = Instant::now();
703        if let Some(delay) = deadline.checked_duration_since(now) {
704            sleep(delay);
705        }
706        return;
707    };
708
709    unsafe {
710        // When we get interrupted (res = EINTR) call clock_nanosleep again
711        loop {
712            let res = libc::clock_nanosleep(
713                crate::sys::time::Instant::CLOCK_ID,
714                libc::TIMER_ABSTIME,
715                &ts,
716                core::ptr::null_mut(), // not required with TIMER_ABSTIME
717            );
718
719            if res == 0 {
720                break;
721            } else {
722                assert_eq!(
723                    res,
724                    libc::EINTR,
725                    "timespec is in range,
726                         clockid is valid and kernel should support it"
727                );
728            }
729        }
730    }
731}
732
733#[cfg(target_vendor = "apple")]
734pub fn sleep_until(deadline: crate::time::Instant) {
735    unsafe extern "C" {
736        // This is defined in the public header mach/mach_time.h alongside
737        // `mach_absolute_time`, and like it has been available since the very
738        // beginning.
739        //
740        // There isn't really any documentation on this function, except for a
741        // short reference in technical note 2169:
742        // https://developer.apple.com/library/archive/technotes/tn2169/_index.html
743        safe fn mach_wait_until(deadline: u64) -> libc::kern_return_t;
744    }
745
746    // Make sure to round up to ensure that we definitely sleep until after
747    // the deadline has elapsed.
748    let Some(deadline) = deadline.into_inner().into_mach_absolute_time_ceil() else {
749        // Since the deadline is before the system boot time, it has already
750        // passed, so we can return immediately.
751        return;
752    };
753
754    // If the deadline is not representable, then sleep for the maximum duration
755    // possible and worry about the potential clock issues later (in ca. 600 years).
756    let deadline = deadline.try_into().unwrap_or(u64::MAX);
757    loop {
758        match mach_wait_until(deadline) {
759            // Success! The deadline has passed.
760            libc::KERN_SUCCESS => break,
761            // If the sleep gets interrupted by a signal, `mach_wait_until`
762            // returns KERN_ABORTED, so we need to restart the syscall.
763            // Also see Apple's implementation of the POSIX `nanosleep`, which
764            // converts this error to the POSIX equivalent EINTR:
765            // https://github.com/apple-oss-distributions/Libc/blob/55b54c0a0c37b3b24393b42b90a4c561d6c606b1/gen/nanosleep.c#L281-L306
766            libc::KERN_ABORTED => continue,
767            // All other errors indicate that something has gone wrong...
768            error => {
769                let description = unsafe { CStr::from_ptr(libc::mach_error_string(error)) };
770                panic!("mach_wait_until failed: {} (code {error})", description.display())
771            }
772        }
773    }
774}
775
776pub fn yield_now() {
777    let ret = unsafe { libc::sched_yield() };
778    debug_assert_eq!(ret, 0);
779}
780
781#[cfg(any(target_os = "android", target_os = "linux"))]
782mod cgroups {
783    //! Currently not covered
784    //! * cgroup v2 in non-standard mountpoints
785    //! * paths containing control characters or spaces, since those would be escaped in procfs
786    //!   output and we don't unescape
787
788    use crate::borrow::Cow;
789    use crate::ffi::OsString;
790    use crate::fs::{File, exists};
791    use crate::io::{BufRead, Read};
792    use crate::os::unix::ffi::OsStringExt;
793    use crate::path::{Path, PathBuf};
794    use crate::str::from_utf8;
795
796    #[derive(PartialEq)]
797    enum Cgroup {
798        V1,
799        V2,
800    }
801
802    /// Returns cgroup CPU quota in core-equivalents, rounded down or usize::MAX if the quota cannot
803    /// be determined or is not set.
804    pub(super) fn quota() -> usize {
805        let mut quota = usize::MAX;
806        if cfg!(miri) {
807            // Attempting to open a file fails under default flags due to isolation.
808            // And Miri does not have parallelism anyway.
809            return quota;
810        }
811
812        let _: Option<()> = try {
813            let mut buf = Vec::with_capacity(128);
814            // find our place in the cgroup hierarchy
815            File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?;
816            let (cgroup_path, version) =
817                buf.split(|&c| c == b'\n').fold(None, |previous, line| {
818                    let mut fields = line.splitn(3, |&c| c == b':');
819                    // 2nd field is a list of controllers for v1 or empty for v2
820                    let version = match fields.nth(1) {
821                        Some(b"") => Cgroup::V2,
822                        Some(controllers)
823                            if from_utf8(controllers)
824                                .is_ok_and(|c| c.split(',').any(|c| c == "cpu")) =>
825                        {
826                            Cgroup::V1
827                        }
828                        _ => return previous,
829                    };
830
831                    // already-found v1 trumps v2 since it explicitly specifies its controllers
832                    if previous.is_some() && version == Cgroup::V2 {
833                        return previous;
834                    }
835
836                    let path = fields.last()?;
837                    // skip leading slash
838                    Some((path[1..].to_owned(), version))
839                })?;
840            let cgroup_path = PathBuf::from(OsString::from_vec(cgroup_path));
841
842            quota = match version {
843                Cgroup::V1 => quota_v1(cgroup_path),
844                Cgroup::V2 => quota_v2(cgroup_path),
845            };
846        };
847
848        quota
849    }
850
851    fn quota_v2(group_path: PathBuf) -> usize {
852        let mut quota = usize::MAX;
853
854        let mut path = PathBuf::with_capacity(128);
855        let mut read_buf = String::with_capacity(20);
856
857        // standard mount location defined in file-hierarchy(7) manpage
858        let cgroup_mount = "/sys/fs/cgroup";
859
860        path.push(cgroup_mount);
861        path.push(&group_path);
862
863        path.push("cgroup.controllers");
864
865        // skip if we're not looking at cgroup2
866        if matches!(exists(&path), Err(_) | Ok(false)) {
867            return usize::MAX;
868        };
869
870        path.pop();
871
872        let _: Option<()> = try {
873            while path.starts_with(cgroup_mount) {
874                path.push("cpu.max");
875
876                read_buf.clear();
877
878                if File::open(&path).and_then(|mut f| f.read_to_string(&mut read_buf)).is_ok() {
879                    let raw_quota = read_buf.lines().next()?;
880                    let mut raw_quota = raw_quota.split(' ');
881                    let limit = raw_quota.next()?;
882                    let period = raw_quota.next()?;
883                    match (limit.parse::<usize>(), period.parse::<usize>()) {
884                        (Ok(limit), Ok(period)) if period > 0 => {
885                            quota = quota.min(limit / period);
886                        }
887                        _ => {}
888                    }
889                }
890
891                path.pop(); // pop filename
892                path.pop(); // pop dir
893            }
894        };
895
896        quota
897    }
898
899    fn quota_v1(group_path: PathBuf) -> usize {
900        let mut quota = usize::MAX;
901        let mut path = PathBuf::with_capacity(128);
902        let mut read_buf = String::with_capacity(20);
903
904        // Hardcode commonly used locations mentioned in the cgroups(7) manpage
905        // if that doesn't work scan mountinfo and adjust `group_path` for bind-mounts
906        let mounts: &[fn(&Path) -> Option<(_, &Path)>] = &[
907            |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu"), p)),
908            |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu,cpuacct"), p)),
909            // this can be expensive on systems with tons of mountpoints
910            // but we only get to this point when /proc/self/cgroups explicitly indicated
911            // this process belongs to a cpu-controller cgroup v1 and the defaults didn't work
912            find_mountpoint,
913        ];
914
915        for mount in mounts {
916            let Some((mount, group_path)) = mount(&group_path) else { continue };
917
918            path.clear();
919            path.push(mount.as_ref());
920            path.push(&group_path);
921
922            // skip if we guessed the mount incorrectly
923            if matches!(exists(&path), Err(_) | Ok(false)) {
924                continue;
925            }
926
927            while path.starts_with(mount.as_ref()) {
928                let mut parse_file = |name| {
929                    path.push(name);
930                    read_buf.clear();
931
932                    let f = File::open(&path);
933                    path.pop(); // restore buffer before any early returns
934                    f.ok()?.read_to_string(&mut read_buf).ok()?;
935                    let parsed = read_buf.trim().parse::<usize>().ok()?;
936
937                    Some(parsed)
938                };
939
940                let limit = parse_file("cpu.cfs_quota_us");
941                let period = parse_file("cpu.cfs_period_us");
942
943                match (limit, period) {
944                    (Some(limit), Some(period)) if period > 0 => quota = quota.min(limit / period),
945                    _ => {}
946                }
947
948                path.pop();
949            }
950
951            // we passed the try_exists above so we should have traversed the correct hierarchy
952            // when reaching this line
953            break;
954        }
955
956        quota
957    }
958
959    /// Scan mountinfo for cgroup v1 mountpoint with a cpu controller
960    ///
961    /// If the cgroupfs is a bind mount then `group_path` is adjusted to skip
962    /// over the already-included prefix
963    fn find_mountpoint(group_path: &Path) -> Option<(Cow<'static, str>, &Path)> {
964        let mut reader = File::open_buffered("/proc/self/mountinfo").ok()?;
965        let mut line = String::with_capacity(256);
966        loop {
967            line.clear();
968            if reader.read_line(&mut line).ok()? == 0 {
969                break;
970            }
971
972            let line = line.trim();
973            let mut items = line.split(' ');
974
975            let sub_path = items.nth(3)?;
976            let mount_point = items.next()?;
977            let mount_opts = items.next_back()?;
978            let filesystem_type = items.nth_back(1)?;
979
980            if filesystem_type != "cgroup" || !mount_opts.split(',').any(|opt| opt == "cpu") {
981                // not a cgroup / not a cpu-controller
982                continue;
983            }
984
985            let sub_path = Path::new(sub_path).strip_prefix("/").ok()?;
986
987            if !group_path.starts_with(sub_path) {
988                // this is a bind-mount and the bound subdirectory
989                // does not contain the cgroup this process belongs to
990                continue;
991            }
992
993            let trimmed_group_path = group_path.strip_prefix(sub_path).ok()?;
994
995            return Some((Cow::Owned(mount_point.to_owned()), trimmed_group_path));
996        }
997
998        None
999    }
1000}
1001
1002// glibc >= 2.15 has a __pthread_get_minstack() function that returns
1003// PTHREAD_STACK_MIN plus bytes needed for thread-local storage.
1004// We need that information to avoid blowing up when a small stack
1005// is created in an application with big thread-local storage requirements.
1006// See #6233 for rationale and details.
1007#[cfg(all(target_os = "linux", target_env = "gnu"))]
1008unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
1009    // We use dlsym to avoid an ELF version dependency on GLIBC_PRIVATE. (#23628)
1010    // We shouldn't really be using such an internal symbol, but there's currently
1011    // no other way to account for the TLS size.
1012    dlsym!(
1013        fn __pthread_get_minstack(attr: *const libc::pthread_attr_t) -> libc::size_t;
1014    );
1015
1016    match __pthread_get_minstack.get() {
1017        None => libc::PTHREAD_STACK_MIN,
1018        Some(f) => unsafe { f(attr) },
1019    }
1020}
1021
1022// No point in looking up __pthread_get_minstack() on non-glibc platforms.
1023#[cfg(all(
1024    not(all(target_os = "linux", target_env = "gnu")),
1025    not(any(target_os = "netbsd", target_os = "nuttx"))
1026))]
1027unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
1028    libc::PTHREAD_STACK_MIN
1029}
1030
1031#[cfg(any(target_os = "netbsd", target_os = "nuttx"))]
1032unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
1033    static STACK: crate::sync::OnceLock<usize> = crate::sync::OnceLock::new();
1034
1035    *STACK.get_or_init(|| {
1036        let mut stack = unsafe { libc::sysconf(libc::_SC_THREAD_STACK_MIN) };
1037        if stack < 0 {
1038            stack = 2048; // just a guess
1039        }
1040
1041        stack as usize
1042    })
1043}