Best-effort number of OS threads in this process. Returns <= 0 when unavailable.
()
| 875 | /// Best-effort number of OS threads in this process. |
| 876 | /// Returns <= 0 when unavailable. |
| 877 | fn get_number_of_os_threads() -> isize { |
| 878 | #[cfg(target_os = "macos")] |
| 879 | { |
| 880 | type MachPortT = libc::c_uint; |
| 881 | type KernReturnT = libc::c_int; |
| 882 | type MachMsgTypeNumberT = libc::c_uint; |
| 883 | type ThreadActArrayT = *mut MachPortT; |
| 884 | const KERN_SUCCESS: KernReturnT = 0; |
| 885 | unsafe extern "C" { |
| 886 | fn mach_task_self() -> MachPortT; |
| 887 | fn task_for_pid( |
| 888 | task: MachPortT, |
| 889 | pid: libc::c_int, |
| 890 | target_task: *mut MachPortT, |
| 891 | ) -> KernReturnT; |
| 892 | fn task_threads( |
| 893 | target_task: MachPortT, |
| 894 | act_list: *mut ThreadActArrayT, |
| 895 | act_list_cnt: *mut MachMsgTypeNumberT, |
| 896 | ) -> KernReturnT; |
| 897 | fn vm_deallocate( |
| 898 | target_task: MachPortT, |
| 899 | address: libc::uintptr_t, |
| 900 | size: libc::uintptr_t, |
| 901 | ) -> KernReturnT; |
| 902 | } |
| 903 | |
| 904 | let self_task = unsafe { mach_task_self() }; |
| 905 | let mut proc_task: MachPortT = 0; |
| 906 | if unsafe { task_for_pid(self_task, libc::getpid(), &mut proc_task) } == KERN_SUCCESS { |
| 907 | let mut threads: ThreadActArrayT = core::ptr::null_mut(); |
| 908 | let mut n_threads: MachMsgTypeNumberT = 0; |
| 909 | if unsafe { task_threads(proc_task, &mut threads, &mut n_threads) } == KERN_SUCCESS |
| 910 | { |
| 911 | if !threads.is_null() { |
| 912 | let _ = unsafe { |
| 913 | vm_deallocate( |
| 914 | self_task, |
| 915 | threads as libc::uintptr_t, |
| 916 | (n_threads as usize * core::mem::size_of::<MachPortT>()) |
| 917 | as libc::uintptr_t, |
| 918 | ) |
| 919 | }; |
| 920 | } |
| 921 | return n_threads as isize; |
| 922 | } |
| 923 | } |
| 924 | 0 |
| 925 | } |
| 926 | #[cfg(target_os = "linux")] |
| 927 | { |
| 928 | use std::io::Read as _; |
| 929 | let mut file = match std::fs::File::open("/proc/self/stat") { |
| 930 | Ok(f) => f, |
| 931 | Err(_) => return 0, |
| 932 | }; |
| 933 | let mut buf = [0u8; 160]; |
| 934 | let n = match file.read(&mut buf) { |