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