Skip to content

Commit a40d49b

Browse files
bors[bot]Jonas Schievink
andauthored
Merge #1825
1825: Add a `sched_getcpu` wrapper r=rtzoeller a=jonas-schievink Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
2 parents 5db70b6 + 945f743 commit a40d49b

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88

9+
- Added `sched_getcpu` on platforms that support it.
10+
([#1825](https://github.com/nix-rust/nix/pull/1825))
911
- Added `sched_getaffinity` and `sched_setaffinity` on FreeBSD.
1012
([#1804](https://github.com/nix-rust/nix/pull/1804))
1113
- Added `line_discipline` field to `Termios` on Linux, Android and Haiku

src/sched.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ mod sched_affinity {
290290

291291
Errno::result(res).and(Ok(cpuset))
292292
}
293+
294+
/// Determines the CPU on which the calling thread is running.
295+
pub fn sched_getcpu() -> Result<usize> {
296+
let res = unsafe { libc::sched_getcpu() };
297+
298+
Errno::result(res).map(|int| int as usize)
299+
}
293300
}
294301

295302
/// Explicitly yield the processor to other threads.

test/test_sched.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet};
1+
use nix::sched::{sched_getaffinity, sched_getcpu, sched_setaffinity, CpuSet};
22
use nix::unistd::Pid;
33

44
#[test]
@@ -30,6 +30,10 @@ fn test_sched_affinity() {
3030
)
3131
}
3232

33+
// Now check that we're also currently running on the CPU in question.
34+
let cur_cpu = sched_getcpu().unwrap();
35+
assert_eq!(cur_cpu, last_valid_cpu);
36+
3337
// Finally, reset the initial CPU set
3438
sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap();
3539
}

0 commit comments

Comments
 (0)