@@ -318,25 +318,38 @@ pub fn available_parallelism() -> io::Result<NonZeroUsize> {
318
318
target_os = "solaris" ,
319
319
target_os = "illumos" ,
320
320
) ) ] {
321
+ #[ allow( unused_assignments) ]
322
+ #[ allow( unused_mut) ]
323
+ let mut quota = usize :: MAX ;
324
+
321
325
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
322
326
{
323
- let quota = cgroups:: quota( ) . max( 1 ) ;
327
+ quota = cgroups:: quota( ) . max( 1 ) ;
324
328
let mut set: libc:: cpu_set_t = unsafe { mem:: zeroed( ) } ;
325
329
unsafe {
326
330
if libc:: sched_getaffinity( 0 , mem:: size_of:: <libc:: cpu_set_t>( ) , & mut set) == 0 {
327
331
let count = libc:: CPU_COUNT ( & set) as usize ;
328
332
let count = count. min( quota) ;
329
- // reported to occur on MIPS kernels older than our minimum supported kernel version for those targets
330
- let count = NonZeroUsize :: new( count)
331
- . expect( "CPU count must be > 0. This may be a bug in sched_getaffinity(); try upgrading the kernel." ) ;
332
- return Ok ( count) ;
333
+
334
+ // According to sched_getaffinity's API it should always be non-zero, but
335
+ // some old MIPS kernels were buggy and zero-initialized the mask if
336
+ // none was explicitly set.
337
+ // In that case we use the sysconf fallback.
338
+ if let Some ( count) = NonZeroUsize :: new( count) {
339
+ return Ok ( count)
340
+ }
333
341
}
334
342
}
335
343
}
336
344
match unsafe { libc:: sysconf( libc:: _SC_NPROCESSORS_ONLN) } {
337
345
-1 => Err ( io:: Error :: last_os_error( ) ) ,
338
346
0 => Err ( io:: const_io_error!( io:: ErrorKind :: NotFound , "The number of hardware threads is not known for the target platform" ) ) ,
339
- cpus => Ok ( unsafe { NonZeroUsize :: new_unchecked( cpus as usize ) } ) ,
347
+ cpus => {
348
+ let count = cpus as usize ;
349
+ // Cover the unusual situation where we were able to get the quota but not the affinity mask
350
+ let count = count. min( quota) ;
351
+ Ok ( unsafe { NonZeroUsize :: new_unchecked( count) } )
352
+ }
340
353
}
341
354
} else if #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" , target_os = "netbsd" ) ) ] {
342
355
use crate :: ptr;
0 commit comments