std::thread::available_parallelism merging linux/android/freebsd version · rust-lang/rust@0bfcafd (original) (raw)

Original file line number Diff line number Diff line change
@@ -320,6 +320,7 @@ pub fn available_parallelism() -> io::Result {
320 320 target_os = "solaris",
321 321 target_os = "illumos",
322 322 target_os = "aix",
323 + target_os = "freebsd",
323 324 ))] {
324 325 #[allow(unused_assignments)]
325 326 #[allow(unused_mut)]
@@ -328,9 +329,17 @@ pub fn available_parallelism() -> io::Result {
328 329 #[cfg(any(target_os = "android", target_os = "linux"))]
329 330 {
330 331 quota = cgroups::quota().max(1);
331 -let mut set: libc::cpu_set_t = unsafe { mem::zeroed() };
332 +}
333 +
334 + #[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))]
335 +{
336 + #[cfg(not(target_os = "freebsd"))]
337 +type cpuset = libc::cpu_set_t;
338 + #[cfg(target_os = "freebsd")]
339 +type cpuset = libc::cpuset_t;
340 +let mut set: cpuset = unsafe { mem::zeroed() };
332 341 unsafe {
333 -if libc::sched_getaffinity(0, mem::size_of::libc::cpu\_set\_t\(), &mut set) == 0 {
342 +if libc::sched_getaffinity(0, mem::size_of::<cpuset>(), &mut set) == 0 {
334 343 let count = libc::CPU_COUNT(&set) as usize;
335 344 let count = count.min(quota);
336 345
@@ -355,32 +364,12 @@ pub fn available_parallelism() -> io::Result {
355 364 }
356 365 }
357 366 } else if #[cfg(any(
358 - target_os = "freebsd",
359 367 target_os = "dragonfly",
360 368 target_os = "openbsd",
361 369 target_os = "netbsd",
362 370 ))] {
363 371 use crate::ptr;
364 372
365 - #[cfg(target_os = "freebsd")]
366 -{
367 -let mut set: libc::cpuset_t = unsafe { mem::zeroed() };
368 -unsafe {
369 -if libc::cpuset_getaffinity(
370 - libc::CPU_LEVEL_WHICH,
371 - libc::CPU_WHICH_PID,
372 - -1,
373 - mem::size_of::libc::cpuset\_t(),
374 -&mut set,
375 -) == 0 {
376 -let count = libc::CPU_COUNT(&set) as usize;
377 -if count > 0 {
378 -return Ok(NonZeroUsize::new_unchecked(count));
379 -}
380 -}
381 -}
382 -}
383 -
384 373 #[cfg(target_os = "netbsd")]
385 374 {
386 375 unsafe {