std/sys/pal/unix/
thread.rs

1use crate::ffi::CStr;
2use crate::mem::{self, ManuallyDrop};
3use crate::num::NonZero;
4#[cfg(all(target_os = "linux", target_env = "gnu"))]
5use crate::sys::weak::dlsym;
6#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto",))]
7use crate::sys::weak::weak;
8use crate::sys::{os, stack_overflow};
9use crate::time::Duration;
10use crate::{cmp, io, ptr};
11#[cfg(not(any(
12    target_os = "l4re",
13    target_os = "vxworks",
14    target_os = "espidf",
15    target_os = "nuttx"
16)))]
17pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
18#[cfg(target_os = "l4re")]
19pub const DEFAULT_MIN_STACK_SIZE: usize = 1024 * 1024;
20#[cfg(target_os = "vxworks")]
21pub const DEFAULT_MIN_STACK_SIZE: usize = 256 * 1024;
22#[cfg(any(target_os = "espidf", target_os = "nuttx"))]
23pub const DEFAULT_MIN_STACK_SIZE: usize = 0; // 0 indicates that the stack size configured in the ESP-IDF/NuttX menuconfig system should be used
24
25pub struct Thread {
26    id: libc::pthread_t,
27}
28
29// Some platforms may have pthread_t as a pointer in which case we still want
30// a thread to be Send/Sync
31unsafe impl Send for Thread {}
32unsafe impl Sync for Thread {}
33
34impl Thread {
35    // unsafe: see thread::Builder::spawn_unchecked for safety requirements
36    #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
37    pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
38        let p = Box::into_raw(Box::new(p));
39        let mut native: libc::pthread_t = mem::zeroed();
40        let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
41        assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
42
43        #[cfg(any(target_os = "espidf", target_os = "nuttx"))]
44        if stack > 0 {
45            // Only set the stack if a non-zero value is passed
46            // 0 is used as an indication that the default stack size configured in the ESP-IDF/NuttX menuconfig system should be used
47            assert_eq!(
48                libc::pthread_attr_setstacksize(
49                    attr.as_mut_ptr(),
50                    cmp::max(stack, min_stack_size(attr.as_ptr()))
51                ),
52                0
53            );
54        }
55
56        #[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
57        {
58            let stack_size = cmp::max(stack, min_stack_size(attr.as_ptr()));
59
60            match libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) {
61                0 => {}
62                n => {
63                    assert_eq!(n, libc::EINVAL);
64                    // EINVAL means |stack_size| is either too small or not a
65                    // multiple of the system page size. Because it's definitely
66                    // >= PTHREAD_STACK_MIN, it must be an alignment issue.
67                    // Round up to the nearest page and try again.
68                    let page_size = os::page_size();
69                    let stack_size =
70                        (stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
71                    assert_eq!(libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size), 0);
72                }
73            };
74        }
75
76        let ret = libc::pthread_create(&mut native, attr.as_ptr(), thread_start, p as *mut _);
77        // Note: if the thread creation fails and this assert fails, then p will
78        // be leaked. However, an alternative design could cause double-free
79        // which is clearly worse.
80        assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
81
82        return if ret != 0 {
83            // The thread failed to start and as a result p was not consumed. Therefore, it is
84            // safe to reconstruct the box so that it gets deallocated.
85            drop(Box::from_raw(p));
86            Err(io::Error::from_raw_os_error(ret))
87        } else {
88            Ok(Thread { id: native })
89        };
90
91        extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
92            unsafe {
93                // Next, set up our stack overflow handler which may get triggered if we run
94                // out of stack.
95                let _handler = stack_overflow::Handler::new();
96                // Finally, let's run some code.
97                Box::from_raw(main as *mut Box<dyn FnOnce()>)();
98            }
99            ptr::null_mut()
100        }
101    }
102
103    pub fn yield_now() {
104        let ret = unsafe { libc::sched_yield() };
105        debug_assert_eq!(ret, 0);
106    }
107
108    #[cfg(target_os = "android")]
109    pub fn set_name(name: &CStr) {
110        const PR_SET_NAME: libc::c_int = 15;
111        unsafe {
112            let res = libc::prctl(
113                PR_SET_NAME,
114                name.as_ptr(),
115                0 as libc::c_ulong,
116                0 as libc::c_ulong,
117                0 as libc::c_ulong,
118            );
119            // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
120            debug_assert_eq!(res, 0);
121        }
122    }
123
124    #[cfg(any(
125        target_os = "linux",
126        target_os = "freebsd",
127        target_os = "dragonfly",
128        target_os = "nuttx",
129        target_os = "cygwin"
130    ))]
131    pub fn set_name(name: &CStr) {
132        unsafe {
133            cfg_if::cfg_if! {
134                if #[cfg(any(target_os = "linux", target_os = "cygwin"))] {
135                    // Linux and Cygwin limits the allowed length of the name.
136                    const TASK_COMM_LEN: usize = 16;
137                    let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
138                } else {
139                    // FreeBSD, DragonFly BSD and NuttX do not enforce length limits.
140                }
141            };
142            // Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20 for Linux,
143            // FreeBSD 12.2 and 13.0, and DragonFly BSD 6.0.
144            let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
145            // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
146            debug_assert_eq!(res, 0);
147        }
148    }
149
150    #[cfg(target_os = "openbsd")]
151    pub fn set_name(name: &CStr) {
152        unsafe {
153            libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr());
154        }
155    }
156
157    #[cfg(target_vendor = "apple")]
158    pub fn set_name(name: &CStr) {
159        unsafe {
160            let name = truncate_cstr::<{ libc::MAXTHREADNAMESIZE }>(name);
161            let res = libc::pthread_setname_np(name.as_ptr());
162            // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
163            debug_assert_eq!(res, 0);
164        }
165    }
166
167    #[cfg(target_os = "netbsd")]
168    pub fn set_name(name: &CStr) {
169        unsafe {
170            let res = libc::pthread_setname_np(
171                libc::pthread_self(),
172                c"%s".as_ptr(),
173                name.as_ptr() as *mut libc::c_void,
174            );
175            debug_assert_eq!(res, 0);
176        }
177    }
178
179    #[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto"))]
180    pub fn set_name(name: &CStr) {
181        weak!(
182            fn pthread_setname_np(
183                thread: libc::pthread_t,
184                name: *const libc::c_char,
185            ) -> libc::c_int;
186        );
187
188        if let Some(f) = pthread_setname_np.get() {
189            #[cfg(target_os = "nto")]
190            const THREAD_NAME_MAX: usize = libc::_NTO_THREAD_NAME_MAX as usize;
191            #[cfg(any(target_os = "solaris", target_os = "illumos"))]
192            const THREAD_NAME_MAX: usize = 32;
193
194            let name = truncate_cstr::<{ THREAD_NAME_MAX }>(name);
195            let res = unsafe { f(libc::pthread_self(), name.as_ptr()) };
196            debug_assert_eq!(res, 0);
197        }
198    }
199
200    #[cfg(target_os = "fuchsia")]
201    pub fn set_name(name: &CStr) {
202        use super::fuchsia::*;
203        unsafe {
204            zx_object_set_property(
205                zx_thread_self(),
206                ZX_PROP_NAME,
207                name.as_ptr() as *const libc::c_void,
208                name.to_bytes().len(),
209            );
210        }
211    }
212
213    #[cfg(target_os = "haiku")]
214    pub fn set_name(name: &CStr) {
215        unsafe {
216            let thread_self = libc::find_thread(ptr::null_mut());
217            let res = libc::rename_thread(thread_self, name.as_ptr());
218            // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
219            debug_assert_eq!(res, libc::B_OK);
220        }
221    }
222
223    #[cfg(target_os = "vxworks")]
224    pub fn set_name(name: &CStr) {
225        let mut name = truncate_cstr::<{ libc::VX_TASK_RENAME_LENGTH - 1 }>(name);
226        let res = unsafe { libc::taskNameSet(libc::taskIdSelf(), name.as_mut_ptr()) };
227        debug_assert_eq!(res, libc::OK);
228    }
229
230    #[cfg(any(
231        target_env = "newlib",
232        target_os = "l4re",
233        target_os = "emscripten",
234        target_os = "redox",
235        target_os = "hurd",
236        target_os = "aix",
237    ))]
238    pub fn set_name(_name: &CStr) {
239        // Newlib and Emscripten have no way to set a thread name.
240    }
241
242    #[cfg(not(target_os = "espidf"))]
243    pub fn sleep(dur: Duration) {
244        let mut secs = dur.as_secs();
245        let mut nsecs = dur.subsec_nanos() as _;
246
247        // If we're awoken with a signal then the return value will be -1 and
248        // nanosleep will fill in `ts` with the remaining time.
249        unsafe {
250            while secs > 0 || nsecs > 0 {
251                let mut ts = libc::timespec {
252                    tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
253                    tv_nsec: nsecs,
254                };
255                secs -= ts.tv_sec as u64;
256                let ts_ptr = &raw mut ts;
257                if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
258                    assert_eq!(os::errno(), libc::EINTR);
259                    secs += ts.tv_sec as u64;
260                    nsecs = ts.tv_nsec;
261                } else {
262                    nsecs = 0;
263                }
264            }
265        }
266    }
267
268    #[cfg(target_os = "espidf")]
269    pub fn sleep(dur: Duration) {
270        // ESP-IDF does not have `nanosleep`, so we use `usleep` instead.
271        // As per the documentation of `usleep`, it is expected to support
272        // sleep times as big as at least up to 1 second.
273        //
274        // ESP-IDF does support almost up to `u32::MAX`, but due to a potential integer overflow in its
275        // `usleep` implementation
276        // (https://github.com/espressif/esp-idf/blob/d7ca8b94c852052e3bc33292287ef4dd62c9eeb1/components/newlib/time.c#L210),
277        // we limit the sleep time to the maximum one that would not cause the underlying `usleep` implementation to overflow
278        // (`portTICK_PERIOD_MS` can be anything between 1 to 1000, and is 10 by default).
279        const MAX_MICROS: u32 = u32::MAX - 1_000_000 - 1;
280
281        // Add any nanoseconds smaller than a microsecond as an extra microsecond
282        // so as to comply with the `std::thread::sleep` contract which mandates
283        // implementations to sleep for _at least_ the provided `dur`.
284        // We can't overflow `micros` as it is a `u128`, while `Duration` is a pair of
285        // (`u64` secs, `u32` nanos), where the nanos are strictly smaller than 1 second
286        // (i.e. < 1_000_000_000)
287        let mut micros = dur.as_micros() + if dur.subsec_nanos() % 1_000 > 0 { 1 } else { 0 };
288
289        while micros > 0 {
290            let st = if micros > MAX_MICROS as u128 { MAX_MICROS } else { micros as u32 };
291            unsafe {
292                libc::usleep(st);
293            }
294
295            micros -= st as u128;
296        }
297    }
298
299    pub fn join(self) {
300        let id = self.into_id();
301        let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };
302        assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret));
303    }
304
305    pub fn id(&self) -> libc::pthread_t {
306        self.id
307    }
308
309    pub fn into_id(self) -> libc::pthread_t {
310        ManuallyDrop::new(self).id
311    }
312}
313
314impl Drop for Thread {
315    fn drop(&mut self) {
316        let ret = unsafe { libc::pthread_detach(self.id) };
317        debug_assert_eq!(ret, 0);
318    }
319}
320
321#[cfg(any(
322    target_os = "linux",
323    target_os = "nto",
324    target_os = "solaris",
325    target_os = "illumos",
326    target_os = "vxworks",
327    target_os = "cygwin",
328    target_vendor = "apple",
329))]
330fn truncate_cstr<const MAX_WITH_NUL: usize>(cstr: &CStr) -> [libc::c_char; MAX_WITH_NUL] {
331    let mut result = [0; MAX_WITH_NUL];
332    for (src, dst) in cstr.to_bytes().iter().zip(&mut result[..MAX_WITH_NUL - 1]) {
333        *dst = *src as libc::c_char;
334    }
335    result
336}
337
338pub fn available_parallelism() -> io::Result<NonZero<usize>> {
339    cfg_if::cfg_if! {
340        if #[cfg(any(
341            target_os = "android",
342            target_os = "emscripten",
343            target_os = "fuchsia",
344            target_os = "hurd",
345            target_os = "linux",
346            target_os = "aix",
347            target_vendor = "apple",
348            target_os = "cygwin",
349        ))] {
350            #[allow(unused_assignments)]
351            #[allow(unused_mut)]
352            let mut quota = usize::MAX;
353
354            #[cfg(any(target_os = "android", target_os = "linux"))]
355            {
356                quota = cgroups::quota().max(1);
357                let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
358                unsafe {
359                    if libc::sched_getaffinity(0, size_of::<libc::cpu_set_t>(), &mut set) == 0 {
360                        let count = libc::CPU_COUNT(&set) as usize;
361                        let count = count.min(quota);
362
363                        // According to sched_getaffinity's API it should always be non-zero, but
364                        // some old MIPS kernels were buggy and zero-initialized the mask if
365                        // none was explicitly set.
366                        // In that case we use the sysconf fallback.
367                        if let Some(count) = NonZero::new(count) {
368                            return Ok(count)
369                        }
370                    }
371                }
372            }
373            match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
374                -1 => Err(io::Error::last_os_error()),
375                0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
376                cpus => {
377                    let count = cpus as usize;
378                    // Cover the unusual situation where we were able to get the quota but not the affinity mask
379                    let count = count.min(quota);
380                    Ok(unsafe { NonZero::new_unchecked(count) })
381                }
382            }
383        } else if #[cfg(any(
384                   target_os = "freebsd",
385                   target_os = "dragonfly",
386                   target_os = "openbsd",
387                   target_os = "netbsd",
388               ))] {
389            use crate::ptr;
390
391            #[cfg(target_os = "freebsd")]
392            {
393                let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
394                unsafe {
395                    if libc::cpuset_getaffinity(
396                        libc::CPU_LEVEL_WHICH,
397                        libc::CPU_WHICH_PID,
398                        -1,
399                        size_of::<libc::cpuset_t>(),
400                        &mut set,
401                    ) == 0 {
402                        let count = libc::CPU_COUNT(&set) as usize;
403                        if count > 0 {
404                            return Ok(NonZero::new_unchecked(count));
405                        }
406                    }
407                }
408            }
409
410            #[cfg(target_os = "netbsd")]
411            {
412                unsafe {
413                    let set = libc::_cpuset_create();
414                    if !set.is_null() {
415                        let mut count: usize = 0;
416                        if libc::pthread_getaffinity_np(libc::pthread_self(), libc::_cpuset_size(set), set) == 0 {
417                            for i in 0..libc::cpuid_t::MAX {
418                                match libc::_cpuset_isset(i, set) {
419                                    -1 => break,
420                                    0 => continue,
421                                    _ => count = count + 1,
422                                }
423                            }
424                        }
425                        libc::_cpuset_destroy(set);
426                        if let Some(count) = NonZero::new(count) {
427                            return Ok(count);
428                        }
429                    }
430                }
431            }
432
433            let mut cpus: libc::c_uint = 0;
434            let mut cpus_size = size_of_val(&cpus);
435
436            unsafe {
437                cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
438            }
439
440            // Fallback approach in case of errors or no hardware threads.
441            if cpus < 1 {
442                let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
443                let res = unsafe {
444                    libc::sysctl(
445                        mib.as_mut_ptr(),
446                        2,
447                        (&raw mut cpus) as *mut _,
448                        (&raw mut cpus_size) as *mut _,
449                        ptr::null_mut(),
450                        0,
451                    )
452                };
453
454                // Handle errors if any.
455                if res == -1 {
456                    return Err(io::Error::last_os_error());
457                } else if cpus == 0 {
458                    return Err(io::Error::UNKNOWN_THREAD_COUNT);
459                }
460            }
461
462            Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
463        } else if #[cfg(target_os = "nto")] {
464            unsafe {
465                use libc::_syspage_ptr;
466                if _syspage_ptr.is_null() {
467                    Err(io::const_error!(io::ErrorKind::NotFound, "no syspage available"))
468                } else {
469                    let cpus = (*_syspage_ptr).num_cpu;
470                    NonZero::new(cpus as usize)
471                        .ok_or(io::Error::UNKNOWN_THREAD_COUNT)
472                }
473            }
474        } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
475            let mut cpus = 0u32;
476            if unsafe { libc::pset_info(libc::PS_MYID, core::ptr::null_mut(), &mut cpus, core::ptr::null_mut()) } != 0 {
477                return Err(io::Error::UNKNOWN_THREAD_COUNT);
478            }
479            Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
480        } else if #[cfg(target_os = "haiku")] {
481            // system_info cpu_count field gets the static data set at boot time with `smp_set_num_cpus`
482            // `get_system_info` calls then `smp_get_num_cpus`
483            unsafe {
484                let mut sinfo: libc::system_info = crate::mem::zeroed();
485                let res = libc::get_system_info(&mut sinfo);
486
487                if res != libc::B_OK {
488                    return Err(io::Error::UNKNOWN_THREAD_COUNT);
489                }
490
491                Ok(NonZero::new_unchecked(sinfo.cpu_count as usize))
492            }
493        } else if #[cfg(target_os = "vxworks")] {
494            // Note: there is also `vxCpuConfiguredGet`, closer to _SC_NPROCESSORS_CONF
495            // expectations than the actual cores availability.
496            unsafe extern "C" {
497                fn vxCpuEnabledGet() -> libc::cpuset_t;
498            }
499
500            // SAFETY: `vxCpuEnabledGet` always fetches a mask with at least one bit set
501            unsafe{
502                let set = vxCpuEnabledGet();
503                Ok(NonZero::new_unchecked(set.count_ones() as usize))
504            }
505        } else {
506            // FIXME: implement on Redox, l4re
507            Err(io::const_error!(io::ErrorKind::Unsupported, "getting the number of hardware threads is not supported on the target platform"))
508        }
509    }
510}
511
512#[cfg(any(target_os = "android", target_os = "linux"))]
513mod cgroups {
514    //! Currently not covered
515    //! * cgroup v2 in non-standard mountpoints
516    //! * paths containing control characters or spaces, since those would be escaped in procfs
517    //!   output and we don't unescape
518
519    use crate::borrow::Cow;
520    use crate::ffi::OsString;
521    use crate::fs::{File, exists};
522    use crate::io::{BufRead, Read};
523    use crate::os::unix::ffi::OsStringExt;
524    use crate::path::{Path, PathBuf};
525    use crate::str::from_utf8;
526
527    #[derive(PartialEq)]
528    enum Cgroup {
529        V1,
530        V2,
531    }
532
533    /// Returns cgroup CPU quota in core-equivalents, rounded down or usize::MAX if the quota cannot
534    /// be determined or is not set.
535    pub(super) fn quota() -> usize {
536        let mut quota = usize::MAX;
537        if cfg!(miri) {
538            // Attempting to open a file fails under default flags due to isolation.
539            // And Miri does not have parallelism anyway.
540            return quota;
541        }
542
543        let _: Option<()> = try {
544            let mut buf = Vec::with_capacity(128);
545            // find our place in the cgroup hierarchy
546            File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?;
547            let (cgroup_path, version) =
548                buf.split(|&c| c == b'\n').fold(None, |previous, line| {
549                    let mut fields = line.splitn(3, |&c| c == b':');
550                    // 2nd field is a list of controllers for v1 or empty for v2
551                    let version = match fields.nth(1) {
552                        Some(b"") => Cgroup::V2,
553                        Some(controllers)
554                            if from_utf8(controllers)
555                                .is_ok_and(|c| c.split(',').any(|c| c == "cpu")) =>
556                        {
557                            Cgroup::V1
558                        }
559                        _ => return previous,
560                    };
561
562                    // already-found v1 trumps v2 since it explicitly specifies its controllers
563                    if previous.is_some() && version == Cgroup::V2 {
564                        return previous;
565                    }
566
567                    let path = fields.last()?;
568                    // skip leading slash
569                    Some((path[1..].to_owned(), version))
570                })?;
571            let cgroup_path = PathBuf::from(OsString::from_vec(cgroup_path));
572
573            quota = match version {
574                Cgroup::V1 => quota_v1(cgroup_path),
575                Cgroup::V2 => quota_v2(cgroup_path),
576            };
577        };
578
579        quota
580    }
581
582    fn quota_v2(group_path: PathBuf) -> usize {
583        let mut quota = usize::MAX;
584
585        let mut path = PathBuf::with_capacity(128);
586        let mut read_buf = String::with_capacity(20);
587
588        // standard mount location defined in file-hierarchy(7) manpage
589        let cgroup_mount = "/sys/fs/cgroup";
590
591        path.push(cgroup_mount);
592        path.push(&group_path);
593
594        path.push("cgroup.controllers");
595
596        // skip if we're not looking at cgroup2
597        if matches!(exists(&path), Err(_) | Ok(false)) {
598            return usize::MAX;
599        };
600
601        path.pop();
602
603        let _: Option<()> = try {
604            while path.starts_with(cgroup_mount) {
605                path.push("cpu.max");
606
607                read_buf.clear();
608
609                if File::open(&path).and_then(|mut f| f.read_to_string(&mut read_buf)).is_ok() {
610                    let raw_quota = read_buf.lines().next()?;
611                    let mut raw_quota = raw_quota.split(' ');
612                    let limit = raw_quota.next()?;
613                    let period = raw_quota.next()?;
614                    match (limit.parse::<usize>(), period.parse::<usize>()) {
615                        (Ok(limit), Ok(period)) if period > 0 => {
616                            quota = quota.min(limit / period);
617                        }
618                        _ => {}
619                    }
620                }
621
622                path.pop(); // pop filename
623                path.pop(); // pop dir
624            }
625        };
626
627        quota
628    }
629
630    fn quota_v1(group_path: PathBuf) -> usize {
631        let mut quota = usize::MAX;
632        let mut path = PathBuf::with_capacity(128);
633        let mut read_buf = String::with_capacity(20);
634
635        // Hardcode commonly used locations mentioned in the cgroups(7) manpage
636        // if that doesn't work scan mountinfo and adjust `group_path` for bind-mounts
637        let mounts: &[fn(&Path) -> Option<(_, &Path)>] = &[
638            |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu"), p)),
639            |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu,cpuacct"), p)),
640            // this can be expensive on systems with tons of mountpoints
641            // but we only get to this point when /proc/self/cgroups explicitly indicated
642            // this process belongs to a cpu-controller cgroup v1 and the defaults didn't work
643            find_mountpoint,
644        ];
645
646        for mount in mounts {
647            let Some((mount, group_path)) = mount(&group_path) else { continue };
648
649            path.clear();
650            path.push(mount.as_ref());
651            path.push(&group_path);
652
653            // skip if we guessed the mount incorrectly
654            if matches!(exists(&path), Err(_) | Ok(false)) {
655                continue;
656            }
657
658            while path.starts_with(mount.as_ref()) {
659                let mut parse_file = |name| {
660                    path.push(name);
661                    read_buf.clear();
662
663                    let f = File::open(&path);
664                    path.pop(); // restore buffer before any early returns
665                    f.ok()?.read_to_string(&mut read_buf).ok()?;
666                    let parsed = read_buf.trim().parse::<usize>().ok()?;
667
668                    Some(parsed)
669                };
670
671                let limit = parse_file("cpu.cfs_quota_us");
672                let period = parse_file("cpu.cfs_period_us");
673
674                match (limit, period) {
675                    (Some(limit), Some(period)) if period > 0 => quota = quota.min(limit / period),
676                    _ => {}
677                }
678
679                path.pop();
680            }
681
682            // we passed the try_exists above so we should have traversed the correct hierarchy
683            // when reaching this line
684            break;
685        }
686
687        quota
688    }
689
690    /// Scan mountinfo for cgroup v1 mountpoint with a cpu controller
691    ///
692    /// If the cgroupfs is a bind mount then `group_path` is adjusted to skip
693    /// over the already-included prefix
694    fn find_mountpoint(group_path: &Path) -> Option<(Cow<'static, str>, &Path)> {
695        let mut reader = File::open_buffered("/proc/self/mountinfo").ok()?;
696        let mut line = String::with_capacity(256);
697        loop {
698            line.clear();
699            if reader.read_line(&mut line).ok()? == 0 {
700                break;
701            }
702
703            let line = line.trim();
704            let mut items = line.split(' ');
705
706            let sub_path = items.nth(3)?;
707            let mount_point = items.next()?;
708            let mount_opts = items.next_back()?;
709            let filesystem_type = items.nth_back(1)?;
710
711            if filesystem_type != "cgroup" || !mount_opts.split(',').any(|opt| opt == "cpu") {
712                // not a cgroup / not a cpu-controller
713                continue;
714            }
715
716            let sub_path = Path::new(sub_path).strip_prefix("/").ok()?;
717
718            if !group_path.starts_with(sub_path) {
719                // this is a bind-mount and the bound subdirectory
720                // does not contain the cgroup this process belongs to
721                continue;
722            }
723
724            let trimmed_group_path = group_path.strip_prefix(sub_path).ok()?;
725
726            return Some((Cow::Owned(mount_point.to_owned()), trimmed_group_path));
727        }
728
729        None
730    }
731}
732
733// glibc >= 2.15 has a __pthread_get_minstack() function that returns
734// PTHREAD_STACK_MIN plus bytes needed for thread-local storage.
735// We need that information to avoid blowing up when a small stack
736// is created in an application with big thread-local storage requirements.
737// See #6233 for rationale and details.
738#[cfg(all(target_os = "linux", target_env = "gnu"))]
739unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
740    // We use dlsym to avoid an ELF version dependency on GLIBC_PRIVATE. (#23628)
741    // We shouldn't really be using such an internal symbol, but there's currently
742    // no other way to account for the TLS size.
743    dlsym!(
744        fn __pthread_get_minstack(attr: *const libc::pthread_attr_t) -> libc::size_t;
745    );
746
747    match __pthread_get_minstack.get() {
748        None => libc::PTHREAD_STACK_MIN,
749        Some(f) => unsafe { f(attr) },
750    }
751}
752
753// No point in looking up __pthread_get_minstack() on non-glibc platforms.
754#[cfg(all(
755    not(all(target_os = "linux", target_env = "gnu")),
756    not(any(target_os = "netbsd", target_os = "nuttx"))
757))]
758unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
759    libc::PTHREAD_STACK_MIN
760}
761
762#[cfg(any(target_os = "netbsd", target_os = "nuttx"))]
763unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
764    static STACK: crate::sync::OnceLock<usize> = crate::sync::OnceLock::new();
765
766    *STACK.get_or_init(|| {
767        let mut stack = unsafe { libc::sysconf(libc::_SC_THREAD_STACK_MIN) };
768        if stack < 0 {
769            stack = 2048; // just a guess
770        }
771
772        stack as usize
773    })
774}