Skip to content

Commit 7e83dae

Browse files
authored
Enable CpuSet::count on Android (#204)
* Enable `CpuSet::count` on Android.
1 parent 1141fb3 commit 7e83dae

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

src/imp/libc/process/cpu_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn CPU_ISSET(cpu: usize, cpuset: &RawCpuSet) -> bool {
4040
unsafe { libc::CPU_ISSET(cpu, cpuset) }
4141
}
4242

43-
#[cfg(any(target_os = "linux"))]
43+
#[cfg(any(target_os = "android", target_os = "linux"))]
4444
#[inline]
4545
pub(crate) fn CPU_COUNT(cpuset: &RawCpuSet) -> u32 {
4646
use core::convert::TryInto;

src/process/sched.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl CpuSet {
5555
}
5656

5757
/// Count the number of CPUs set in the `CpuSet`.
58-
#[cfg(target_os = "linux")]
58+
#[cfg(any(target_os = "android", target_os = "linux"))]
5959
#[inline]
6060
pub fn count(&self) -> u32 {
6161
imp::process::cpu_set::CPU_COUNT(&self.cpu_set)

tests/process/cpu_set.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[cfg(any(target_os = "android", target_os = "linux"))]
2+
#[test]
3+
fn test_cpu_set() {
4+
let set = rustix::process::sched_getaffinity(None).unwrap();
5+
6+
let mut count = 0;
7+
for i in 0..rustix::process::CpuSet::MAX_CPU {
8+
if set.is_set(i) {
9+
count += 1;
10+
}
11+
}
12+
13+
assert_eq!(count, set.count());
14+
}

tests/process/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
66

77
mod auxv;
8+
mod cpu_set;
89
#[cfg(not(target_os = "wasi"))] // WASI doesn't have get[gpu]id.
910
mod id;
1011
#[cfg(any(target_os = "android", target_os = "linux"))]

0 commit comments

Comments
 (0)