Skip to content

Commit eee005a

Browse files
oech3cakebaker
authored andcommitted
nproc: reduce a nest by if
1 parent 33e4f0d commit eee005a

1 file changed

Lines changed: 23 additions & 28 deletions

File tree

src/uu/nproc/src/nproc.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,31 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4343

4444
let mut cores = if matches.get_flag(OPT_ALL) {
4545
num_cpus_all()
46-
} else {
46+
} else if let Ok(threads) = env::var("OMP_NUM_THREADS") {
4747
// OMP_NUM_THREADS doesn't have an impact on --all
48-
match env::var("OMP_NUM_THREADS") {
49-
// Uses the OpenMP variable to force the number of threads
50-
// If the parsing fails, returns the number of CPU
51-
Ok(threads) => {
52-
// In some cases, OMP_NUM_THREADS can be "x,y,z"
53-
// In this case, only take the first one (like GNU)
54-
// If OMP_NUM_THREADS=0, rejects the value
55-
match threads.split_terminator(',').next() {
56-
None => available_parallelism(),
57-
Some(s) => match s.trim().parse() {
58-
Ok(0) | Err(_) => available_parallelism(),
59-
Ok(n) => n,
60-
},
61-
}
62-
}
63-
// the variable 'OMP_NUM_THREADS' doesn't exist
64-
// fallback to the regular CPU detection
65-
Err(_) => {
66-
// ignore quota under some schedulers
67-
#[cfg(any(target_os = "linux", target_os = "android"))]
68-
match unsafe { libc::sched_getscheduler(0) } {
69-
libc::SCHED_FIFO | libc::SCHED_RR | libc::SCHED_DEADLINE => num_cpus_all(),
70-
_ => available_parallelism(), // include fallback for error
71-
}
72-
#[cfg(not(any(target_os = "linux", target_os = "android")))]
73-
available_parallelism()
74-
}
48+
// Uses the OpenMP variable to force the number of threads
49+
// If the parsing fails, returns the number of CPU
50+
// In some cases, OMP_NUM_THREADS can be "x,y,z"
51+
// In this case, only take the first one (like GNU)
52+
// If OMP_NUM_THREADS=0, rejects the value
53+
match threads.split_terminator(',').next() {
54+
None => available_parallelism(),
55+
Some(s) => match s.trim().parse() {
56+
Ok(0) | Err(_) => available_parallelism(),
57+
Ok(n) => n,
58+
},
59+
}
60+
} else {
61+
// the variable 'OMP_NUM_THREADS' doesn't exist
62+
// fallback to the regular CPU detection
63+
// ignore quota under some schedulers
64+
#[cfg(any(target_os = "linux", target_os = "android"))]
65+
match unsafe { libc::sched_getscheduler(0) } {
66+
libc::SCHED_FIFO | libc::SCHED_RR | libc::SCHED_DEADLINE => num_cpus_all(),
67+
_ => available_parallelism(), // include fallback for error
7568
}
69+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
70+
available_parallelism()
7671
};
7772

7873
cores = std::cmp::min(limit, cores);

0 commit comments

Comments
 (0)