@@ -320,19 +320,22 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
320320 target_os = "solaris" ,
321321 target_os = "illumos" ,
322322 target_os = "aix" ,
323+ target_os = "freebsd" ,
323324 ) ) ] {
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" ) ) ]
329326 {
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( ) } ;
332332 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 {
334334 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 ) ;
336339
337340 // According to sched_getaffinity's API it should always be non-zero, but
338341 // some old MIPS kernels were buggy and zero-initialized the mask if
@@ -350,37 +353,20 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
350353 cpus => {
351354 let count = cpus as usize ;
352355 // 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( not( target_os = "freebsd" ) ) ]
357+ let count = count. min( cgroups:: quota( ) . max( 1 ) ) ;
358+ #[ cfg( target_os = "freebsd" ) ]
359+ let count = count. min( usize :: MAX ) ;
354360 Ok ( unsafe { NonZeroUsize :: new_unchecked( count) } )
355361 }
356362 }
357363 } else if #[ cfg( any(
358- target_os = "freebsd" ,
359364 target_os = "dragonfly" ,
360365 target_os = "openbsd" ,
361366 target_os = "netbsd" ,
362367 ) ) ] {
363368 use crate :: ptr;
364369
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-
384370 #[ cfg( target_os = "netbsd" ) ]
385371 {
386372 unsafe {
0 commit comments