]>
git.proxmox.com Git - rustc.git/blob - vendor/rustix/src/process/sched.rs
1 use crate::process
::Pid
;
2 use crate::{backend, io}
;
4 /// `CpuSet` represents a bit-mask of CPUs.
6 /// `CpuSet`s are used by [`sched_setaffinity`] and [`sched_getaffinity`], for
12 /// [Linux]: https://man7.org/linux/man-pages/man3/CPU_SET.3.html
13 /// [`sched_setaffinity`]: crate::process::sched_setaffinity
14 /// [`sched_getaffinity`]: crate::process::sched_getaffinity
16 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
18 cpu_set
: backend
::process
::types
::RawCpuSet
,
22 /// The maximum number of CPU in `CpuSet`.
23 pub const MAX_CPU
: usize = backend
::process
::types
::CPU_SETSIZE
;
25 /// Create a new and empty `CpuSet`.
27 pub fn new() -> Self {
29 cpu_set
: backend
::process
::types
::raw_cpu_set_new(),
33 /// Test to see if a CPU is in the `CpuSet`.
35 /// `field` is the CPU id to test.
37 pub fn is_set(&self, field
: usize) -> bool
{
38 backend
::process
::cpu_set
::CPU_ISSET(field
, &self.cpu_set
)
41 /// Add a CPU to `CpuSet`.
43 /// `field` is the CPU id to add.
45 pub fn set(&mut self, field
: usize) {
46 backend
::process
::cpu_set
::CPU_SET(field
, &mut self.cpu_set
)
49 /// Remove a CPU from `CpuSet`.
51 /// `field` is the CPU id to remove.
53 pub fn unset(&mut self, field
: usize) {
54 backend
::process
::cpu_set
::CPU_CLR(field
, &mut self.cpu_set
)
57 /// Count the number of CPUs set in the `CpuSet`.
60 pub fn count(&self) -> u32 {
61 backend
::process
::cpu_set
::CPU_COUNT(&self.cpu_set
)
64 /// Zeroes the `CpuSet`.
66 pub fn clear(&mut self) {
67 backend
::process
::cpu_set
::CPU_ZERO(&mut self.cpu_set
)
71 impl Default
for CpuSet
{
73 fn default() -> Self {
78 /// `sched_setaffinity(pid, cpuset)`—Set a thread's CPU affinity mask.
80 /// `pid` is the thread ID to update. If pid is `None`, then the current thread
83 /// The `CpuSet` argument specifies the set of CPUs on which the thread will be
89 /// [Linux]: https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html
91 pub fn sched_setaffinity(pid
: Option
<Pid
>, cpuset
: &CpuSet
) -> io
::Result
<()> {
92 backend
::process
::syscalls
::sched_setaffinity(pid
, &cpuset
.cpu_set
)
95 /// `sched_getaffinity(pid)`—Get a thread's CPU affinity mask.
97 /// `pid` is the thread ID to check. If pid is `None`, then the current thread
100 /// Returns the set of CPUs on which the thread is eligible to run.
105 /// [Linux]: https://man7.org/linux/man-pages/man2/sched_getaffinity.2.html
107 pub fn sched_getaffinity(pid
: Option
<Pid
>) -> io
::Result
<CpuSet
> {
108 let mut cpuset
= CpuSet
::new();
109 backend
::process
::syscalls
::sched_getaffinity(pid
, &mut cpuset
.cpu_set
).and(Ok(cpuset
))