Skip to content

Commit

Permalink
Merge #1825
Browse files Browse the repository at this point in the history
1825: Add a `sched_getcpu` wrapper r=rtzoeller a=jonas-schievink



Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
  • Loading branch information
bors[bot] and Jonas Schievink authored Sep 27, 2022
2 parents 5db70b6 + 945f743 commit a40d49b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion test/test_sched.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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();
}

0 comments on commit a40d49b

Please sign in to comment.