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; pub struct Thread {
42 id: libc::pthread_t,
43}
44
45unsafe impl Send for Thread {}
48unsafe impl Sync for Thread {}
49
50impl Thread {
51 #[cfg_attr(miri, track_caller)] 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 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 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 let page_size = sys::pal::conf::page_size();
89 let stack_size = stack_size.checked_next_multiple_of(page_size);
90
91 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 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 let init = Box::from_raw(data as *mut ThreadInit);
122 let rust_start = init.init();
123
124 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 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 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 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 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 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 unsafe {
336 let set = libc::vxCpuEnabledGet();
337 Ok(NonZero::new_unchecked(set.count_ones() as usize))
338 }
339 }
340 _ => {
341 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 cfg_select! {
357 any(target_os = "android", target_os = "linux") => {
359 use crate::sys::pal::weak::syscall;
360
361 syscall!(
364 fn gettid() -> libc::pid_t;
365 );
366
367 let id: libc::pid_t = unsafe { gettid() };
369 Some(id as u64)
370 }
371 any(target_os = "nto", target_os = "qnx") => {
372 let id: libc::pid_t = unsafe { libc::gettid() };
374 Some(id as u64)
375 }
376 target_os = "openbsd" => {
377 let id: libc::pid_t = unsafe { libc::getthrid() };
379 Some(id as u64)
380 }
381 target_os = "freebsd" => {
382 let id: libc::c_int = unsafe { libc::pthread_getthreadid_np() };
384 Some(id as u64)
385 }
386 target_os = "netbsd" => {
387 let id: libc::lwpid_t = unsafe { libc::_lwp_self() };
389 Some(id as u64)
390 }
391 any(target_os = "illumos", target_os = "solaris") => {
392 let id: libc::pthread_t = unsafe { libc::pthread_self() };
395 Some(id as u64)
396 }
397 target_vendor = "apple" => {
398 let mut id = 0u64;
400 let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) };
402 if status == 0 { Some(id) } else { None }
403 }
404 _ => 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 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 const TASK_COMM_LEN: usize = 16;
457 let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
458 }
459 _ => {
460 }
462 };
463 let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
466 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 debug_assert_eq!(res, 0);
485 }
486}
487
488#[cfg(target_os = "netbsd")]
489pub fn set_name(name: &CStr) {
490 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 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(
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 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 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 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 const MAX_MICROS: u32 = u32::MAX - 1_000_000 - 1;
640
641 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#[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 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 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 _ => break,
741 }
742 }
743 }
744 }
745
746 let Some(ts) = timespec.to_timespec() else {
747 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 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(), );
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 safe fn mach_wait_until(deadline: u64) -> libc::kern_return_t;
792 }
793
794 let Some(deadline) = deadline.into_inner().into_mach_absolute_time_ceil() else {
797 return;
800 };
801
802 let deadline = deadline.try_into().unwrap_or(u64::MAX);
805 loop {
806 match mach_wait_until(deadline) {
807 libc::KERN_SUCCESS => break,
809 libc::KERN_ABORTED => continue,
815 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 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 pub(super) fn quota() -> usize {
863 let mut quota = usize::MAX;
864 if cfg!(miri) {
865 return quota;
868 }
869
870 let _: Option<()> = try {
871 let mut buf = Vec::with_capacity(128);
872 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 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 if previous.is_some() && version == Cgroup::V2 {
891 return previous;
892 }
893
894 let path = fields.last()?;
895 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 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 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(); path.pop(); }
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 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 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 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(); 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 break;
1012 }
1013
1014 quota
1015 }
1016
1017 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 continue;
1041 }
1042
1043 let sub_path = Path::new(sub_path).strip_prefix("/").ok()?;
1044
1045 if !group_path.starts_with(sub_path) {
1046 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#[cfg(all(target_os = "linux", target_env = "gnu"))]
1066unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
1067 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#[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; }
1098
1099 stack as usize
1100 })
1101}