summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-09-27 13:18:10 +0000
committerGitHub <noreply@github.com>2022-09-27 13:18:10 +0000
commita40d49b82b7c44eafaef02488c1a89f9a7644001 (patch)
tree622135b10f490953c7da30136ab52c55c12e1b80
parent5db70b6ac8ad0803ce2abcd7ff4ebabd22c58371 (diff)
parent945f743aa7003b6cb6c646d9208713052cbc2701 (diff)
downloadnix-a40d49b82b7c44eafaef02488c1a89f9a7644001.zip
Merge #1825
1825: Add a `sched_getcpu` wrapper r=rtzoeller a=jonas-schievink Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/sched.rs7
-rw-r--r--test/test_sched.rs6
3 files changed, 14 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7356b005..45e191a6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased] - ReleaseDate
### Added
+- Added `sched_getcpu` on platforms that support it.
+ ([#1825](https://github.com/nix-rust/nix/pull/1825))
- Added `sched_getaffinity` and `sched_setaffinity` on FreeBSD.
([#1804](https://github.com/nix-rust/nix/pull/1804))
- Added `line_discipline` field to `Termios` on Linux, Android and Haiku
diff --git a/src/sched.rs b/src/sched.rs
index 943ed6ec..aa2b90b4 100644
--- a/src/sched.rs
+++ b/src/sched.rs
@@ -290,6 +290,13 @@ mod sched_affinity {
Errno::result(res).and(Ok(cpuset))
}
+
+ /// Determines the CPU on which the calling thread is running.
+ pub fn sched_getcpu() -> Result<usize> {
+ let res = unsafe { libc::sched_getcpu() };
+
+ Errno::result(res).map(|int| int as usize)
+ }
}
/// Explicitly yield the processor to other threads.
diff --git a/test/test_sched.rs b/test/test_sched.rs
index ebf346db..c52616b8 100644
--- a/test/test_sched.rs
+++ b/test/test_sched.rs
@@ -1,4 +1,4 @@
-use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet};
+use nix::sched::{sched_getaffinity, sched_getcpu, sched_setaffinity, CpuSet};
use nix::unistd::Pid;
#[test]
@@ -30,6 +30,10 @@ fn test_sched_affinity() {
)
}
+ // Now check that we're also currently running on the CPU in question.
+ let cur_cpu = sched_getcpu().unwrap();
+ assert_eq!(cur_cpu, last_valid_cpu);
+
// Finally, reset the initial CPU set
sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap();
}