Skip to content

Commit 8d2b6bc

Browse files
authored
Merge pull request #41 from kubo39/return-1-if-sysconf-failed
Return 1 if sysconf(_SC_NPROCESSORS_ONLN) failed
2 parents a79b706 + 588bb4e commit 8d2b6bc

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/lib.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,11 @@ fn get_num_cpus() -> usize {
172172
}
173173
count as usize
174174
} else {
175-
unsafe {
176-
libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize
175+
let cpus = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) };
176+
if cpus < 1 {
177+
1
178+
} else {
179+
cpus as usize
177180
}
178181
}
179182
}
@@ -187,10 +190,14 @@ fn get_num_cpus() -> usize {
187190
target_os = "fuchsia")
188191
)]
189192
fn get_num_cpus() -> usize {
190-
unsafe {
191-
libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize
193+
let cpus = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) };
194+
if cpus < 1 {
195+
1
196+
} else {
197+
cpus as usize
192198
}
193199
}
200+
194201
#[cfg(any(target_os = "emscripten", target_os = "redox", target_os = "haiku"))]
195202
fn get_num_cpus() -> usize {
196203
1

0 commit comments

Comments
 (0)