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)))]
9use crate::ffi::CStr;
10use crate::mem::{self, DropGuard, ManuallyDrop};
11use crate::num::NonZero;
12#[cfg(all(target_os = "linux", target_env = "gnu"))]
13use crate::sys::weak::dlsym;
14#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto",))]
15use crate::sys::weak::weak;
16use crate::sys::{os, stack_overflow};
17use crate::time::Duration;
18use crate::{cmp, io, ptr};
19#[cfg(not(any(
20 target_os = "l4re",
21 target_os = "vxworks",
22 target_os = "espidf",
23 target_os = "nuttx"
24)))]
25pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
26#[cfg(target_os = "l4re")]
27pub const DEFAULT_MIN_STACK_SIZE: usize = 1024 * 1024;
28#[cfg(target_os = "vxworks")]
29pub const DEFAULT_MIN_STACK_SIZE: usize = 256 * 1024;
30#[cfg(any(target_os = "espidf", target_os = "nuttx"))]
31pub const DEFAULT_MIN_STACK_SIZE: usize = 0; struct ThreadData {
34 name: Option<Box<str>>,
35 f: Box<dyn FnOnce()>,
36}
37
38pub struct Thread {
39 id: libc::pthread_t,
40}
41
42unsafe impl Send for Thread {}
45unsafe impl Sync for Thread {}
46
47impl Thread {
48 #[cfg_attr(miri, track_caller)] pub unsafe fn new(
51 stack: usize,
52 name: Option<&str>,
53 f: Box<dyn FnOnce()>,
54 ) -> io::Result<Thread> {
55 let data = Box::new(ThreadData { name: name.map(Box::from), f });
56
57 let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
58 assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
59 let mut attr = DropGuard::new(&mut attr, |attr| {
60 assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0)
61 });
62
63 #[cfg(any(target_os = "espidf", target_os = "nuttx"))]
64 if stack > 0 {
65 assert_eq!(
68 libc::pthread_attr_setstacksize(
69 attr.as_mut_ptr(),
70 cmp::max(stack, min_stack_size(attr.as_ptr()))
71 ),
72 0
73 );
74 }
75
76 #[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
77 {
78 let stack_size = cmp::max(stack, min_stack_size(attr.as_ptr()));
79
80 match libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) {
81 0 => {}
82 n => {
83 assert_eq!(n, libc::EINVAL);
84 let page_size = os::page_size();
89 let stack_size =
90 (stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
91
92 if libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) != 0 {
96 return Err(io::const_error!(
97 io::ErrorKind::InvalidInput,
98 "invalid stack size"
99 ));
100 }
101 }
102 };
103 }
104
105 let data = Box::into_raw(data);
106 let mut native: libc::pthread_t = mem::zeroed();
107 let ret = libc::pthread_create(&mut native, attr.as_ptr(), thread_start, data as *mut _);
108 return if ret == 0 {
109 Ok(Thread { id: native })
110 } else {
111 drop(Box::from_raw(data));
114 Err(io::Error::from_raw_os_error(ret))
115 };
116
117 extern "C" fn thread_start(data: *mut libc::c_void) -> *mut libc::c_void {
118 unsafe {
119 let data = Box::from_raw(data as *mut ThreadData);
120 let _handler = stack_overflow::Handler::new(data.name);
123 (data.f)();
125 }
126 ptr::null_mut()
127 }
128 }
129
130 pub fn join(self) {
131 let id = self.into_id();
132 let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };
133 assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret));
134 }
135
136 pub fn id(&self) -> libc::pthread_t {
137 self.id
138 }
139
140 pub fn into_id(self) -> libc::pthread_t {
141 ManuallyDrop::new(self).id
142 }
143}
144
145impl Drop for Thread {
146 fn drop(&mut self) {
147 let ret = unsafe { libc::pthread_detach(self.id) };
148 debug_assert_eq!(ret, 0);
149 }
150}
151
152pub fn available_parallelism() -> io::Result<NonZero<usize>> {
153 cfg_select! {
154 any(
155 target_os = "android",
156 target_os = "emscripten",
157 target_os = "fuchsia",
158 target_os = "hurd",
159 target_os = "linux",
160 target_os = "aix",
161 target_vendor = "apple",
162 target_os = "cygwin",
163 ) => {
164 #[allow(unused_assignments)]
165 #[allow(unused_mut)]
166 let mut quota = usize::MAX;
167
168 #[cfg(any(target_os = "android", target_os = "linux"))]
169 {
170 quota = cgroups::quota().max(1);
171 let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
172 unsafe {
173 if libc::sched_getaffinity(0, size_of::<libc::cpu_set_t>(), &mut set) == 0 {
174 let count = libc::CPU_COUNT(&set) as usize;
175 let count = count.min(quota);
176
177 if let Some(count) = NonZero::new(count) {
182 return Ok(count)
183 }
184 }
185 }
186 }
187 match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
188 -1 => Err(io::Error::last_os_error()),
189 0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
190 cpus => {
191 let count = cpus as usize;
192 let count = count.min(quota);
194 Ok(unsafe { NonZero::new_unchecked(count) })
195 }
196 }
197 }
198 any(
199 target_os = "freebsd",
200 target_os = "dragonfly",
201 target_os = "openbsd",
202 target_os = "netbsd",
203 ) => {
204 use crate::ptr;
205
206 #[cfg(target_os = "freebsd")]
207 {
208 let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
209 unsafe {
210 if libc::cpuset_getaffinity(
211 libc::CPU_LEVEL_WHICH,
212 libc::CPU_WHICH_PID,
213 -1,
214 size_of::<libc::cpuset_t>(),
215 &mut set,
216 ) == 0 {
217 let count = libc::CPU_COUNT(&set) as usize;
218 if count > 0 {
219 return Ok(NonZero::new_unchecked(count));
220 }
221 }
222 }
223 }
224
225 #[cfg(target_os = "netbsd")]
226 {
227 unsafe {
228 let set = libc::_cpuset_create();
229 if !set.is_null() {
230 let mut count: usize = 0;
231 if libc::pthread_getaffinity_np(libc::pthread_self(), libc::_cpuset_size(set), set) == 0 {
232 for i in 0..libc::cpuid_t::MAX {
233 match libc::_cpuset_isset(i, set) {
234 -1 => break,
235 0 => continue,
236 _ => count = count + 1,
237 }
238 }
239 }
240 libc::_cpuset_destroy(set);
241 if let Some(count) = NonZero::new(count) {
242 return Ok(count);
243 }
244 }
245 }
246 }
247
248 let mut cpus: libc::c_uint = 0;
249 let mut cpus_size = size_of_val(&cpus);
250
251 unsafe {
252 cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
253 }
254
255 if cpus < 1 {
257 let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
258 let res = unsafe {
259 libc::sysctl(
260 mib.as_mut_ptr(),
261 2,
262 (&raw mut cpus) as *mut _,
263 (&raw mut cpus_size) as *mut _,
264 ptr::null_mut(),
265 0,
266 )
267 };
268
269 if res == -1 {
271 return Err(io::Error::last_os_error());
272 } else if cpus == 0 {
273 return Err(io::Error::UNKNOWN_THREAD_COUNT);
274 }
275 }
276
277 Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
278 }
279 target_os = "nto" => {
280 unsafe {
281 use libc::_syspage_ptr;
282 if _syspage_ptr.is_null() {
283 Err(io::const_error!(io::ErrorKind::NotFound, "no syspage available"))
284 } else {
285 let cpus = (*_syspage_ptr).num_cpu;
286 NonZero::new(cpus as usize)
287 .ok_or(io::Error::UNKNOWN_THREAD_COUNT)
288 }
289 }
290 }
291 any(target_os = "solaris", target_os = "illumos") => {
292 let mut cpus = 0u32;
293 if unsafe { libc::pset_info(libc::PS_MYID, core::ptr::null_mut(), &mut cpus, core::ptr::null_mut()) } != 0 {
294 return Err(io::Error::UNKNOWN_THREAD_COUNT);
295 }
296 Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
297 }
298 target_os = "haiku" => {
299 unsafe {
302 let mut sinfo: libc::system_info = crate::mem::zeroed();
303 let res = libc::get_system_info(&mut sinfo);
304
305 if res != libc::B_OK {
306 return Err(io::Error::UNKNOWN_THREAD_COUNT);
307 }
308
309 Ok(NonZero::new_unchecked(sinfo.cpu_count as usize))
310 }
311 }
312 target_os = "vxworks" => {
313 unsafe{
318 let set = libc::vxCpuEnabledGet();
319 Ok(NonZero::new_unchecked(set.count_ones() as usize))
320 }
321 }
322 _ => {
323 Err(io::const_error!(io::ErrorKind::Unsupported, "getting the number of hardware threads is not supported on the target platform"))
325 }
326 }
327}
328
329pub fn current_os_id() -> Option<u64> {
330 cfg_select! {
336 any(target_os = "android", target_os = "linux") => {
338 use crate::sys::pal::weak::syscall;
339
340 syscall!(fn gettid() -> libc::pid_t;);
343
344 let id: libc::pid_t = unsafe { gettid() };
346 Some(id as u64)
347 }
348 target_os = "nto" => {
349 let id: libc::pid_t = unsafe { libc::gettid() };
351 Some(id as u64)
352 }
353 target_os = "openbsd" => {
354 let id: libc::pid_t = unsafe { libc::getthrid() };
356 Some(id as u64)
357 }
358 target_os = "freebsd" => {
359 let id: libc::c_int = unsafe { libc::pthread_getthreadid_np() };
361 Some(id as u64)
362 }
363 target_os = "netbsd" => {
364 let id: libc::lwpid_t = unsafe { libc::_lwp_self() };
366 Some(id as u64)
367 }
368 any(target_os = "illumos", target_os = "solaris") => {
369 let id: libc::pthread_t = unsafe { libc::pthread_self() };
372 Some(id as u64)
373 }
374 target_vendor = "apple" => {
375 let mut id = 0u64;
377 let status: libc::c_int = unsafe { libc::pthread_threadid_np(0, &mut id) };
379 if status == 0 {
380 Some(id)
381 } else {
382 None
383 }
384 }
385 _ => None,
387 }
388}
389
390#[cfg(any(
391 target_os = "linux",
392 target_os = "nto",
393 target_os = "solaris",
394 target_os = "illumos",
395 target_os = "vxworks",
396 target_os = "cygwin",
397 target_vendor = "apple",
398))]
399fn truncate_cstr<const MAX_WITH_NUL: usize>(cstr: &CStr) -> [libc::c_char; MAX_WITH_NUL] {
400 let mut result = [0; MAX_WITH_NUL];
401 for (src, dst) in cstr.to_bytes().iter().zip(&mut result[..MAX_WITH_NUL - 1]) {
402 *dst = *src as libc::c_char;
403 }
404 result
405}
406
407#[cfg(target_os = "android")]
408pub fn set_name(name: &CStr) {
409 const PR_SET_NAME: libc::c_int = 15;
410 unsafe {
411 let res = libc::prctl(
412 PR_SET_NAME,
413 name.as_ptr(),
414 0 as libc::c_ulong,
415 0 as libc::c_ulong,
416 0 as libc::c_ulong,
417 );
418 debug_assert_eq!(res, 0);
420 }
421}
422
423#[cfg(any(
424 target_os = "linux",
425 target_os = "freebsd",
426 target_os = "dragonfly",
427 target_os = "nuttx",
428 target_os = "cygwin"
429))]
430pub fn set_name(name: &CStr) {
431 unsafe {
432 cfg_select! {
433 any(target_os = "linux", target_os = "cygwin") => {
434 const TASK_COMM_LEN: usize = 16;
436 let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
437 }
438 _ => {
439 }
441 };
442 let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
445 debug_assert_eq!(res, 0);
447 }
448}
449
450#[cfg(target_os = "openbsd")]
451pub fn set_name(name: &CStr) {
452 unsafe {
453 libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr());
454 }
455}
456
457#[cfg(target_vendor = "apple")]
458pub fn set_name(name: &CStr) {
459 unsafe {
460 let name = truncate_cstr::<{ libc::MAXTHREADNAMESIZE }>(name);
461 let res = libc::pthread_setname_np(name.as_ptr());
462 debug_assert_eq!(res, 0);
464 }
465}
466
467#[cfg(target_os = "netbsd")]
468pub fn set_name(name: &CStr) {
469 unsafe {
470 let res = libc::pthread_setname_np(
471 libc::pthread_self(),
472 c"%s".as_ptr(),
473 name.as_ptr() as *mut libc::c_void,
474 );
475 debug_assert_eq!(res, 0);
476 }
477}
478
479#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto"))]
480pub fn set_name(name: &CStr) {
481 weak!(
482 fn pthread_setname_np(thread: libc::pthread_t, name: *const libc::c_char) -> libc::c_int;
483 );
484
485 if let Some(f) = pthread_setname_np.get() {
486 #[cfg(target_os = "nto")]
487 const THREAD_NAME_MAX: usize = libc::_NTO_THREAD_NAME_MAX as usize;
488 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
489 const THREAD_NAME_MAX: usize = 32;
490
491 let name = truncate_cstr::<{ THREAD_NAME_MAX }>(name);
492 let res = unsafe { f(libc::pthread_self(), name.as_ptr()) };
493 debug_assert_eq!(res, 0);
494 }
495}
496
497#[cfg(target_os = "fuchsia")]
498pub fn set_name(name: &CStr) {
499 use crate::sys::pal::fuchsia::*;
500 unsafe {
501 zx_object_set_property(
502 zx_thread_self(),
503 ZX_PROP_NAME,
504 name.as_ptr() as *const libc::c_void,
505 name.to_bytes().len(),
506 );
507 }
508}
509
510#[cfg(target_os = "haiku")]
511pub fn set_name(name: &CStr) {
512 unsafe {
513 let thread_self = libc::find_thread(ptr::null_mut());
514 let res = libc::rename_thread(thread_self, name.as_ptr());
515 debug_assert_eq!(res, libc::B_OK);
517 }
518}
519
520#[cfg(target_os = "vxworks")]
521pub fn set_name(name: &CStr) {
522 let mut name = truncate_cstr::<{ (libc::VX_TASK_RENAME_LENGTH - 1) as usize }>(name);
523 let res = unsafe { libc::taskNameSet(libc::taskIdSelf(), name.as_mut_ptr()) };
524 debug_assert_eq!(res, libc::OK);
525}
526
527#[cfg(not(target_os = "espidf"))]
528pub fn sleep(dur: Duration) {
529 let mut secs = dur.as_secs();
530 let mut nsecs = dur.subsec_nanos() as _;
531
532 unsafe {
535 while secs > 0 || nsecs > 0 {
536 let mut ts = libc::timespec {
537 tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
538 tv_nsec: nsecs,
539 };
540 secs -= ts.tv_sec as u64;
541 let ts_ptr = &raw mut ts;
542 if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
543 assert_eq!(os::errno(), libc::EINTR);
544 secs += ts.tv_sec as u64;
545 nsecs = ts.tv_nsec;
546 } else {
547 nsecs = 0;
548 }
549 }
550 }
551}
552
553#[cfg(target_os = "espidf")]
554pub fn sleep(dur: Duration) {
555 const MAX_MICROS: u32 = u32::MAX - 1_000_000 - 1;
565
566 let mut micros = dur.as_micros() + if dur.subsec_nanos() % 1_000 > 0 { 1 } else { 0 };
573
574 while micros > 0 {
575 let st = if micros > MAX_MICROS as u128 { MAX_MICROS } else { micros as u32 };
576 unsafe {
577 libc::usleep(st);
578 }
579
580 micros -= st as u128;
581 }
582}
583
584#[cfg(any(
587 target_os = "freebsd",
588 target_os = "netbsd",
589 target_os = "linux",
590 target_os = "android",
591 target_os = "solaris",
592 target_os = "illumos",
593 target_os = "dragonfly",
594 target_os = "hurd",
595 target_os = "fuchsia",
596 target_os = "vxworks",
597))]
598pub fn sleep_until(deadline: crate::time::Instant) {
599 use crate::time::Instant;
600
601 let Some(ts) = deadline.into_inner().into_timespec().to_timespec() else {
602 let now = Instant::now();
606 if let Some(delay) = deadline.checked_duration_since(now) {
607 sleep(delay);
608 }
609 return;
610 };
611
612 unsafe {
613 loop {
615 let res = libc::clock_nanosleep(
616 crate::sys::time::Instant::CLOCK_ID,
617 libc::TIMER_ABSTIME,
618 &ts,
619 core::ptr::null_mut(), );
621
622 if res == 0 {
623 break;
624 } else {
625 assert_eq!(
626 res,
627 libc::EINTR,
628 "timespec is in range,
629 clockid is valid and kernel should support it"
630 );
631 }
632 }
633 }
634}
635
636pub fn yield_now() {
637 let ret = unsafe { libc::sched_yield() };
638 debug_assert_eq!(ret, 0);
639}
640
641#[cfg(any(target_os = "android", target_os = "linux"))]
642mod cgroups {
643 use crate::borrow::Cow;
649 use crate::ffi::OsString;
650 use crate::fs::{File, exists};
651 use crate::io::{BufRead, Read};
652 use crate::os::unix::ffi::OsStringExt;
653 use crate::path::{Path, PathBuf};
654 use crate::str::from_utf8;
655
656 #[derive(PartialEq)]
657 enum Cgroup {
658 V1,
659 V2,
660 }
661
662 pub(super) fn quota() -> usize {
665 let mut quota = usize::MAX;
666 if cfg!(miri) {
667 return quota;
670 }
671
672 let _: Option<()> = try {
673 let mut buf = Vec::with_capacity(128);
674 File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?;
676 let (cgroup_path, version) =
677 buf.split(|&c| c == b'\n').fold(None, |previous, line| {
678 let mut fields = line.splitn(3, |&c| c == b':');
679 let version = match fields.nth(1) {
681 Some(b"") => Cgroup::V2,
682 Some(controllers)
683 if from_utf8(controllers)
684 .is_ok_and(|c| c.split(',').any(|c| c == "cpu")) =>
685 {
686 Cgroup::V1
687 }
688 _ => return previous,
689 };
690
691 if previous.is_some() && version == Cgroup::V2 {
693 return previous;
694 }
695
696 let path = fields.last()?;
697 Some((path[1..].to_owned(), version))
699 })?;
700 let cgroup_path = PathBuf::from(OsString::from_vec(cgroup_path));
701
702 quota = match version {
703 Cgroup::V1 => quota_v1(cgroup_path),
704 Cgroup::V2 => quota_v2(cgroup_path),
705 };
706 };
707
708 quota
709 }
710
711 fn quota_v2(group_path: PathBuf) -> usize {
712 let mut quota = usize::MAX;
713
714 let mut path = PathBuf::with_capacity(128);
715 let mut read_buf = String::with_capacity(20);
716
717 let cgroup_mount = "/sys/fs/cgroup";
719
720 path.push(cgroup_mount);
721 path.push(&group_path);
722
723 path.push("cgroup.controllers");
724
725 if matches!(exists(&path), Err(_) | Ok(false)) {
727 return usize::MAX;
728 };
729
730 path.pop();
731
732 let _: Option<()> = try {
733 while path.starts_with(cgroup_mount) {
734 path.push("cpu.max");
735
736 read_buf.clear();
737
738 if File::open(&path).and_then(|mut f| f.read_to_string(&mut read_buf)).is_ok() {
739 let raw_quota = read_buf.lines().next()?;
740 let mut raw_quota = raw_quota.split(' ');
741 let limit = raw_quota.next()?;
742 let period = raw_quota.next()?;
743 match (limit.parse::<usize>(), period.parse::<usize>()) {
744 (Ok(limit), Ok(period)) if period > 0 => {
745 quota = quota.min(limit / period);
746 }
747 _ => {}
748 }
749 }
750
751 path.pop(); path.pop(); }
754 };
755
756 quota
757 }
758
759 fn quota_v1(group_path: PathBuf) -> usize {
760 let mut quota = usize::MAX;
761 let mut path = PathBuf::with_capacity(128);
762 let mut read_buf = String::with_capacity(20);
763
764 let mounts: &[fn(&Path) -> Option<(_, &Path)>] = &[
767 |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu"), p)),
768 |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu,cpuacct"), p)),
769 find_mountpoint,
773 ];
774
775 for mount in mounts {
776 let Some((mount, group_path)) = mount(&group_path) else { continue };
777
778 path.clear();
779 path.push(mount.as_ref());
780 path.push(&group_path);
781
782 if matches!(exists(&path), Err(_) | Ok(false)) {
784 continue;
785 }
786
787 while path.starts_with(mount.as_ref()) {
788 let mut parse_file = |name| {
789 path.push(name);
790 read_buf.clear();
791
792 let f = File::open(&path);
793 path.pop(); f.ok()?.read_to_string(&mut read_buf).ok()?;
795 let parsed = read_buf.trim().parse::<usize>().ok()?;
796
797 Some(parsed)
798 };
799
800 let limit = parse_file("cpu.cfs_quota_us");
801 let period = parse_file("cpu.cfs_period_us");
802
803 match (limit, period) {
804 (Some(limit), Some(period)) if period > 0 => quota = quota.min(limit / period),
805 _ => {}
806 }
807
808 path.pop();
809 }
810
811 break;
814 }
815
816 quota
817 }
818
819 fn find_mountpoint(group_path: &Path) -> Option<(Cow<'static, str>, &Path)> {
824 let mut reader = File::open_buffered("/proc/self/mountinfo").ok()?;
825 let mut line = String::with_capacity(256);
826 loop {
827 line.clear();
828 if reader.read_line(&mut line).ok()? == 0 {
829 break;
830 }
831
832 let line = line.trim();
833 let mut items = line.split(' ');
834
835 let sub_path = items.nth(3)?;
836 let mount_point = items.next()?;
837 let mount_opts = items.next_back()?;
838 let filesystem_type = items.nth_back(1)?;
839
840 if filesystem_type != "cgroup" || !mount_opts.split(',').any(|opt| opt == "cpu") {
841 continue;
843 }
844
845 let sub_path = Path::new(sub_path).strip_prefix("/").ok()?;
846
847 if !group_path.starts_with(sub_path) {
848 continue;
851 }
852
853 let trimmed_group_path = group_path.strip_prefix(sub_path).ok()?;
854
855 return Some((Cow::Owned(mount_point.to_owned()), trimmed_group_path));
856 }
857
858 None
859 }
860}
861
862#[cfg(all(target_os = "linux", target_env = "gnu"))]
868unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
869 dlsym!(
873 fn __pthread_get_minstack(attr: *const libc::pthread_attr_t) -> libc::size_t;
874 );
875
876 match __pthread_get_minstack.get() {
877 None => libc::PTHREAD_STACK_MIN,
878 Some(f) => unsafe { f(attr) },
879 }
880}
881
882#[cfg(all(
884 not(all(target_os = "linux", target_env = "gnu")),
885 not(any(target_os = "netbsd", target_os = "nuttx"))
886))]
887unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
888 libc::PTHREAD_STACK_MIN
889}
890
891#[cfg(any(target_os = "netbsd", target_os = "nuttx"))]
892unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
893 static STACK: crate::sync::OnceLock<usize> = crate::sync::OnceLock::new();
894
895 *STACK.get_or_init(|| {
896 let mut stack = unsafe { libc::sysconf(libc::_SC_THREAD_STACK_MIN) };
897 if stack < 0 {
898 stack = 2048; }
900
901 stack as usize
902 })
903}