@@ -320,19 +320,22 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
320
320
target_os = "solaris" ,
321
321
target_os = "illumos" ,
322
322
target_os = "aix" ,
323
+ target_os = "freebsd" ,
323
324
) ) ] {
324
- #[ allow( unused_assignments) ]
325
- #[ allow( unused_mut) ]
326
- let mut quota = usize :: MAX ;
327
-
328
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
325
+ #[ cfg( any( target_os = "android" , target_os = "linux" , target_os = "freebsd" ) ) ]
329
326
{
330
- quota = cgroups:: quota( ) . max( 1 ) ;
331
- let mut set: libc:: cpu_set_t = unsafe { mem:: zeroed( ) } ;
327
+ #[ cfg( not( target_os = "freebsd" ) ) ]
328
+ type cpuset = libc:: cpu_set_t;
329
+ #[ cfg( target_os = "freebsd" ) ]
330
+ type cpuset = libc:: cpuset_t;
331
+ let mut set: cpuset = unsafe { mem:: zeroed( ) } ;
332
332
unsafe {
333
- if libc:: sched_getaffinity( 0 , mem:: size_of:: <libc :: cpu_set_t >( ) , & mut set) == 0 {
333
+ if libc:: sched_getaffinity( 0 , mem:: size_of:: <cpuset >( ) , & mut set) == 0 {
334
334
let count = libc:: CPU_COUNT ( & set) as usize ;
335
- let count = count. min( quota) ;
335
+ #[ cfg( not( target_os = "freebsd" ) ) ]
336
+ let count = count. min( cgroups:: quota( ) . max( 1 ) ) ;
337
+ #[ cfg( target_os = "freebsd" ) ]
338
+ let count = count. min( usize :: MAX ) ;
336
339
337
340
// According to sched_getaffinity's API it should always be non-zero, but
338
341
// some old MIPS kernels were buggy and zero-initialized the mask if
@@ -350,37 +353,20 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
350
353
cpus => {
351
354
let count = cpus as usize ;
352
355
// Cover the unusual situation where we were able to get the quota but not the affinity mask
353
- let count = count. min( quota) ;
356
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
357
+ let count = count. min( cgroups:: quota( ) . max( 1 ) ) ;
358
+ #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
359
+ let count = count. min( usize :: MAX ) ;
354
360
Ok ( unsafe { NonZeroUsize :: new_unchecked( count) } )
355
361
}
356
362
}
357
363
} else if #[ cfg( any(
358
- target_os = "freebsd" ,
359
364
target_os = "dragonfly" ,
360
365
target_os = "openbsd" ,
361
366
target_os = "netbsd" ,
362
367
) ) ] {
363
368
use crate :: ptr;
364
369
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
370
#[ cfg( target_os = "netbsd" ) ]
385
371
{
386
372
unsafe {
0 commit comments