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; #[cfg(target_os = "fuchsia")]
26mod zircon {
27 type zx_handle_t = u32;
28 type zx_status_t = i32;
29 pub const ZX_PROP_NAME: u32 = 3;
30
31 unsafe extern "C" {
32 pub fn zx_object_set_property(
33 handle: zx_handle_t,
34 property: u32,
35 value: *const libc::c_void,
36 value_size: libc::size_t,
37 ) -> zx_status_t;
38 pub fn zx_thread_self() -> zx_handle_t;
39 }
40}
41
42pub struct Thread {
43 id: libc::pthread_t,
44}
45
46unsafe impl Send for Thread {}
49unsafe impl Sync for Thread {}
50
51impl Thread {
52 #[cfg_attr(miri, track_caller)] pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
55 let p = Box::into_raw(Box::new(p));
56 let mut native: libc::pthread_t = mem::zeroed();
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
60 #[cfg(any(target_os = "espidf", target_os = "nuttx"))]
61 if stack > 0 {
62 assert_eq!(
65 libc::pthread_attr_setstacksize(
66 attr.as_mut_ptr(),
67 cmp::max(stack, min_stack_size(attr.as_ptr()))
68 ),
69 0
70 );
71 }
72
73 #[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
74 {
75 let stack_size = cmp::max(stack, min_stack_size(attr.as_ptr()));
76
77 match libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) {
78 0 => {}
79 n => {
80 assert_eq!(n, libc::EINVAL);
81 let page_size = os::page_size();
86 let stack_size =
87 (stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
88 assert_eq!(libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size), 0);
89 }
90 };
91 }
92
93 let ret = libc::pthread_create(&mut native, attr.as_ptr(), thread_start, p as *mut _);
94 assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
98
99 return if ret != 0 {
100 drop(Box::from_raw(p));
103 Err(io::Error::from_raw_os_error(ret))
104 } else {
105 Ok(Thread { id: native })
106 };
107
108 extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
109 unsafe {
110 let _handler = stack_overflow::Handler::new();
113 Box::from_raw(main as *mut Box<dyn FnOnce()>)();
115 }
116 ptr::null_mut()
117 }
118 }
119
120 pub fn yield_now() {
121 let ret = unsafe { libc::sched_yield() };
122 debug_assert_eq!(ret, 0);
123 }
124
125 #[cfg(target_os = "android")]
126 pub fn set_name(name: &CStr) {
127 const PR_SET_NAME: libc::c_int = 15;
128 unsafe {
129 let res = libc::prctl(
130 PR_SET_NAME,
131 name.as_ptr(),
132 0 as libc::c_ulong,
133 0 as libc::c_ulong,
134 0 as libc::c_ulong,
135 );
136 debug_assert_eq!(res, 0);
138 }
139 }
140
141 #[cfg(any(
142 target_os = "linux",
143 target_os = "freebsd",
144 target_os = "dragonfly",
145 target_os = "nuttx",
146 target_os = "cygwin"
147 ))]
148 pub fn set_name(name: &CStr) {
149 unsafe {
150 cfg_if::cfg_if! {
151 if #[cfg(any(target_os = "linux", target_os = "cygwin"))] {
152 const TASK_COMM_LEN: usize = 16;
154 let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
155 } else {
156 }
158 };
159 let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
162 debug_assert_eq!(res, 0);
164 }
165 }
166
167 #[cfg(target_os = "openbsd")]
168 pub fn set_name(name: &CStr) {
169 unsafe {
170 libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr());
171 }
172 }
173
174 #[cfg(target_vendor = "apple")]
175 pub fn set_name(name: &CStr) {
176 unsafe {
177 let name = truncate_cstr::<{ libc::MAXTHREADNAMESIZE }>(name);
178 let res = libc::pthread_setname_np(name.as_ptr());
179 debug_assert_eq!(res, 0);
181 }
182 }
183
184 #[cfg(target_os = "netbsd")]
185 pub fn set_name(name: &CStr) {
186 unsafe {
187 let res = libc::pthread_setname_np(
188 libc::pthread_self(),
189 c"%s".as_ptr(),
190 name.as_ptr() as *mut libc::c_void,
191 );
192 debug_assert_eq!(res, 0);
193 }
194 }
195
196 #[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto"))]
197 pub fn set_name(name: &CStr) {
198 weak!(
199 fn pthread_setname_np(
200 thread: libc::pthread_t,
201 name: *const libc::c_char,
202 ) -> libc::c_int;
203 );
204
205 if let Some(f) = pthread_setname_np.get() {
206 #[cfg(target_os = "nto")]
207 const THREAD_NAME_MAX: usize = libc::_NTO_THREAD_NAME_MAX as usize;
208 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
209 const THREAD_NAME_MAX: usize = 32;
210
211 let name = truncate_cstr::<{ THREAD_NAME_MAX }>(name);
212 let res = unsafe { f(libc::pthread_self(), name.as_ptr()) };
213 debug_assert_eq!(res, 0);
214 }
215 }
216
217 #[cfg(target_os = "fuchsia")]
218 pub fn set_name(name: &CStr) {
219 use self::zircon::*;
220 unsafe {
221 zx_object_set_property(
222 zx_thread_self(),
223 ZX_PROP_NAME,
224 name.as_ptr() as *const libc::c_void,
225 name.to_bytes().len(),
226 );
227 }
228 }
229
230 #[cfg(target_os = "haiku")]
231 pub fn set_name(name: &CStr) {
232 unsafe {
233 let thread_self = libc::find_thread(ptr::null_mut());
234 let res = libc::rename_thread(thread_self, name.as_ptr());
235 debug_assert_eq!(res, libc::B_OK);
237 }
238 }
239
240 #[cfg(target_os = "vxworks")]
241 pub fn set_name(name: &CStr) {
242 unsafe extern "C" {
244 fn taskNameSet(task_id: libc::TASK_ID, task_name: *mut libc::c_char) -> libc::c_int;
245 }
246
247 const VX_TASK_NAME_LEN: usize = 31;
249
250 let mut name = truncate_cstr::<{ VX_TASK_NAME_LEN }>(name);
251 let res = unsafe { taskNameSet(libc::taskIdSelf(), name.as_mut_ptr()) };
252 debug_assert_eq!(res, libc::OK);
253 }
254
255 #[cfg(any(
256 target_env = "newlib",
257 target_os = "l4re",
258 target_os = "emscripten",
259 target_os = "redox",
260 target_os = "hurd",
261 target_os = "aix",
262 ))]
263 pub fn set_name(_name: &CStr) {
264 }
266
267 #[cfg(not(target_os = "espidf"))]
268 pub fn sleep(dur: Duration) {
269 let mut secs = dur.as_secs();
270 let mut nsecs = dur.subsec_nanos() as _;
271
272 unsafe {
275 while secs > 0 || nsecs > 0 {
276 let mut ts = libc::timespec {
277 tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
278 tv_nsec: nsecs,
279 };
280 secs -= ts.tv_sec as u64;
281 let ts_ptr = &raw mut ts;
282 if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
283 assert_eq!(os::errno(), libc::EINTR);
284 secs += ts.tv_sec as u64;
285 nsecs = ts.tv_nsec;
286 } else {
287 nsecs = 0;
288 }
289 }
290 }
291 }
292
293 #[cfg(target_os = "espidf")]
294 pub fn sleep(dur: Duration) {
295 const MAX_MICROS: u32 = u32::MAX - 1_000_000 - 1;
305
306 let mut micros = dur.as_micros() + if dur.subsec_nanos() % 1_000 > 0 { 1 } else { 0 };
313
314 while micros > 0 {
315 let st = if micros > MAX_MICROS as u128 { MAX_MICROS } else { micros as u32 };
316 unsafe {
317 libc::usleep(st);
318 }
319
320 micros -= st as u128;
321 }
322 }
323
324 pub fn join(self) {
325 let id = self.into_id();
326 let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };
327 assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret));
328 }
329
330 pub fn id(&self) -> libc::pthread_t {
331 self.id
332 }
333
334 pub fn into_id(self) -> libc::pthread_t {
335 ManuallyDrop::new(self).id
336 }
337}
338
339impl Drop for Thread {
340 fn drop(&mut self) {
341 let ret = unsafe { libc::pthread_detach(self.id) };
342 debug_assert_eq!(ret, 0);
343 }
344}
345
346#[cfg(any(
347 target_os = "linux",
348 target_os = "nto",
349 target_os = "solaris",
350 target_os = "illumos",
351 target_os = "vxworks",
352 target_os = "cygwin",
353 target_vendor = "apple",
354))]
355fn truncate_cstr<const MAX_WITH_NUL: usize>(cstr: &CStr) -> [libc::c_char; MAX_WITH_NUL] {
356 let mut result = [0; MAX_WITH_NUL];
357 for (src, dst) in cstr.to_bytes().iter().zip(&mut result[..MAX_WITH_NUL - 1]) {
358 *dst = *src as libc::c_char;
359 }
360 result
361}
362
363pub fn available_parallelism() -> io::Result<NonZero<usize>> {
364 cfg_if::cfg_if! {
365 if #[cfg(any(
366 target_os = "android",
367 target_os = "emscripten",
368 target_os = "fuchsia",
369 target_os = "hurd",
370 target_os = "linux",
371 target_os = "aix",
372 target_vendor = "apple",
373 target_os = "cygwin",
374 ))] {
375 #[allow(unused_assignments)]
376 #[allow(unused_mut)]
377 let mut quota = usize::MAX;
378
379 #[cfg(any(target_os = "android", target_os = "linux"))]
380 {
381 quota = cgroups::quota().max(1);
382 let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
383 unsafe {
384 if libc::sched_getaffinity(0, size_of::<libc::cpu_set_t>(), &mut set) == 0 {
385 let count = libc::CPU_COUNT(&set) as usize;
386 let count = count.min(quota);
387
388 if let Some(count) = NonZero::new(count) {
393 return Ok(count)
394 }
395 }
396 }
397 }
398 match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
399 -1 => Err(io::Error::last_os_error()),
400 0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
401 cpus => {
402 let count = cpus as usize;
403 let count = count.min(quota);
405 Ok(unsafe { NonZero::new_unchecked(count) })
406 }
407 }
408 } else if #[cfg(any(
409 target_os = "freebsd",
410 target_os = "dragonfly",
411 target_os = "openbsd",
412 target_os = "netbsd",
413 ))] {
414 use crate::ptr;
415
416 #[cfg(target_os = "freebsd")]
417 {
418 let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
419 unsafe {
420 if libc::cpuset_getaffinity(
421 libc::CPU_LEVEL_WHICH,
422 libc::CPU_WHICH_PID,
423 -1,
424 size_of::<libc::cpuset_t>(),
425 &mut set,
426 ) == 0 {
427 let count = libc::CPU_COUNT(&set) as usize;
428 if count > 0 {
429 return Ok(NonZero::new_unchecked(count));
430 }
431 }
432 }
433 }
434
435 #[cfg(target_os = "netbsd")]
436 {
437 unsafe {
438 let set = libc::_cpuset_create();
439 if !set.is_null() {
440 let mut count: usize = 0;
441 if libc::pthread_getaffinity_np(libc::pthread_self(), libc::_cpuset_size(set), set) == 0 {
442 for i in 0..libc::cpuid_t::MAX {
443 match libc::_cpuset_isset(i, set) {
444 -1 => break,
445 0 => continue,
446 _ => count = count + 1,
447 }
448 }
449 }
450 libc::_cpuset_destroy(set);
451 if let Some(count) = NonZero::new(count) {
452 return Ok(count);
453 }
454 }
455 }
456 }
457
458 let mut cpus: libc::c_uint = 0;
459 let mut cpus_size = size_of_val(&cpus);
460
461 unsafe {
462 cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
463 }
464
465 if cpus < 1 {
467 let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
468 let res = unsafe {
469 libc::sysctl(
470 mib.as_mut_ptr(),
471 2,
472 (&raw mut cpus) as *mut _,
473 (&raw mut cpus_size) as *mut _,
474 ptr::null_mut(),
475 0,
476 )
477 };
478
479 if res == -1 {
481 return Err(io::Error::last_os_error());
482 } else if cpus == 0 {
483 return Err(io::Error::UNKNOWN_THREAD_COUNT);
484 }
485 }
486
487 Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
488 } else if #[cfg(target_os = "nto")] {
489 unsafe {
490 use libc::_syspage_ptr;
491 if _syspage_ptr.is_null() {
492 Err(io::const_error!(io::ErrorKind::NotFound, "no syspage available"))
493 } else {
494 let cpus = (*_syspage_ptr).num_cpu;
495 NonZero::new(cpus as usize)
496 .ok_or(io::Error::UNKNOWN_THREAD_COUNT)
497 }
498 }
499 } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
500 let mut cpus = 0u32;
501 if unsafe { libc::pset_info(libc::PS_MYID, core::ptr::null_mut(), &mut cpus, core::ptr::null_mut()) } != 0 {
502 return Err(io::Error::UNKNOWN_THREAD_COUNT);
503 }
504 Ok(unsafe { NonZero::new_unchecked(cpus as usize) })
505 } else if #[cfg(target_os = "haiku")] {
506 unsafe {
509 let mut sinfo: libc::system_info = crate::mem::zeroed();
510 let res = libc::get_system_info(&mut sinfo);
511
512 if res != libc::B_OK {
513 return Err(io::Error::UNKNOWN_THREAD_COUNT);
514 }
515
516 Ok(NonZero::new_unchecked(sinfo.cpu_count as usize))
517 }
518 } else if #[cfg(target_os = "vxworks")] {
519 unsafe extern "C" {
522 fn vxCpuEnabledGet() -> libc::cpuset_t;
523 }
524
525 unsafe{
527 let set = vxCpuEnabledGet();
528 Ok(NonZero::new_unchecked(set.count_ones() as usize))
529 }
530 } else {
531 Err(io::const_error!(io::ErrorKind::Unsupported, "getting the number of hardware threads is not supported on the target platform"))
533 }
534 }
535}
536
537#[cfg(any(target_os = "android", target_os = "linux"))]
538mod cgroups {
539 use crate::borrow::Cow;
545 use crate::ffi::OsString;
546 use crate::fs::{File, exists};
547 use crate::io::{BufRead, Read};
548 use crate::os::unix::ffi::OsStringExt;
549 use crate::path::{Path, PathBuf};
550 use crate::str::from_utf8;
551
552 #[derive(PartialEq)]
553 enum Cgroup {
554 V1,
555 V2,
556 }
557
558 pub(super) fn quota() -> usize {
561 let mut quota = usize::MAX;
562 if cfg!(miri) {
563 return quota;
566 }
567
568 let _: Option<()> = try {
569 let mut buf = Vec::with_capacity(128);
570 File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?;
572 let (cgroup_path, version) =
573 buf.split(|&c| c == b'\n').fold(None, |previous, line| {
574 let mut fields = line.splitn(3, |&c| c == b':');
575 let version = match fields.nth(1) {
577 Some(b"") => Cgroup::V2,
578 Some(controllers)
579 if from_utf8(controllers)
580 .is_ok_and(|c| c.split(',').any(|c| c == "cpu")) =>
581 {
582 Cgroup::V1
583 }
584 _ => return previous,
585 };
586
587 if previous.is_some() && version == Cgroup::V2 {
589 return previous;
590 }
591
592 let path = fields.last()?;
593 Some((path[1..].to_owned(), version))
595 })?;
596 let cgroup_path = PathBuf::from(OsString::from_vec(cgroup_path));
597
598 quota = match version {
599 Cgroup::V1 => quota_v1(cgroup_path),
600 Cgroup::V2 => quota_v2(cgroup_path),
601 };
602 };
603
604 quota
605 }
606
607 fn quota_v2(group_path: PathBuf) -> usize {
608 let mut quota = usize::MAX;
609
610 let mut path = PathBuf::with_capacity(128);
611 let mut read_buf = String::with_capacity(20);
612
613 let cgroup_mount = "/sys/fs/cgroup";
615
616 path.push(cgroup_mount);
617 path.push(&group_path);
618
619 path.push("cgroup.controllers");
620
621 if matches!(exists(&path), Err(_) | Ok(false)) {
623 return usize::MAX;
624 };
625
626 path.pop();
627
628 let _: Option<()> = try {
629 while path.starts_with(cgroup_mount) {
630 path.push("cpu.max");
631
632 read_buf.clear();
633
634 if File::open(&path).and_then(|mut f| f.read_to_string(&mut read_buf)).is_ok() {
635 let raw_quota = read_buf.lines().next()?;
636 let mut raw_quota = raw_quota.split(' ');
637 let limit = raw_quota.next()?;
638 let period = raw_quota.next()?;
639 match (limit.parse::<usize>(), period.parse::<usize>()) {
640 (Ok(limit), Ok(period)) if period > 0 => {
641 quota = quota.min(limit / period);
642 }
643 _ => {}
644 }
645 }
646
647 path.pop(); path.pop(); }
650 };
651
652 quota
653 }
654
655 fn quota_v1(group_path: PathBuf) -> usize {
656 let mut quota = usize::MAX;
657 let mut path = PathBuf::with_capacity(128);
658 let mut read_buf = String::with_capacity(20);
659
660 let mounts: &[fn(&Path) -> Option<(_, &Path)>] = &[
663 |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu"), p)),
664 |p| Some((Cow::Borrowed("/sys/fs/cgroup/cpu,cpuacct"), p)),
665 find_mountpoint,
669 ];
670
671 for mount in mounts {
672 let Some((mount, group_path)) = mount(&group_path) else { continue };
673
674 path.clear();
675 path.push(mount.as_ref());
676 path.push(&group_path);
677
678 if matches!(exists(&path), Err(_) | Ok(false)) {
680 continue;
681 }
682
683 while path.starts_with(mount.as_ref()) {
684 let mut parse_file = |name| {
685 path.push(name);
686 read_buf.clear();
687
688 let f = File::open(&path);
689 path.pop(); f.ok()?.read_to_string(&mut read_buf).ok()?;
691 let parsed = read_buf.trim().parse::<usize>().ok()?;
692
693 Some(parsed)
694 };
695
696 let limit = parse_file("cpu.cfs_quota_us");
697 let period = parse_file("cpu.cfs_period_us");
698
699 match (limit, period) {
700 (Some(limit), Some(period)) if period > 0 => quota = quota.min(limit / period),
701 _ => {}
702 }
703
704 path.pop();
705 }
706
707 break;
710 }
711
712 quota
713 }
714
715 fn find_mountpoint(group_path: &Path) -> Option<(Cow<'static, str>, &Path)> {
720 let mut reader = File::open_buffered("/proc/self/mountinfo").ok()?;
721 let mut line = String::with_capacity(256);
722 loop {
723 line.clear();
724 if reader.read_line(&mut line).ok()? == 0 {
725 break;
726 }
727
728 let line = line.trim();
729 let mut items = line.split(' ');
730
731 let sub_path = items.nth(3)?;
732 let mount_point = items.next()?;
733 let mount_opts = items.next_back()?;
734 let filesystem_type = items.nth_back(1)?;
735
736 if filesystem_type != "cgroup" || !mount_opts.split(',').any(|opt| opt == "cpu") {
737 continue;
739 }
740
741 let sub_path = Path::new(sub_path).strip_prefix("/").ok()?;
742
743 if !group_path.starts_with(sub_path) {
744 continue;
747 }
748
749 let trimmed_group_path = group_path.strip_prefix(sub_path).ok()?;
750
751 return Some((Cow::Owned(mount_point.to_owned()), trimmed_group_path));
752 }
753
754 None
755 }
756}
757
758#[cfg(all(target_os = "linux", target_env = "gnu"))]
764unsafe fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
765 dlsym!(
769 fn __pthread_get_minstack(attr: *const libc::pthread_attr_t) -> libc::size_t;
770 );
771
772 match __pthread_get_minstack.get() {
773 None => libc::PTHREAD_STACK_MIN,
774 Some(f) => unsafe { f(attr) },
775 }
776}
777
778#[cfg(all(
780 not(all(target_os = "linux", target_env = "gnu")),
781 not(any(target_os = "netbsd", target_os = "nuttx"))
782))]
783unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
784 libc::PTHREAD_STACK_MIN
785}
786
787#[cfg(any(target_os = "netbsd", target_os = "nuttx"))]
788unsafe fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
789 static STACK: crate::sync::OnceLock<usize> = crate::sync::OnceLock::new();
790
791 *STACK.get_or_init(|| {
792 let mut stack = unsafe { libc::sysconf(libc::_SC_THREAD_STACK_MIN) };
793 if stack < 0 {
794 stack = 2048; }
796
797 stack as usize
798 })
799}