xref: /aosp_15_r20/external/crosvm/base/src/sys/linux/priority.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2018 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::mem::MaybeUninit;
6 
7 use super::errno_result;
8 use super::Result;
9 
10 /// Enables real time thread priorities in the current thread up to `limit`.
set_rt_prio_limit(limit: u64) -> Result<()>11 pub fn set_rt_prio_limit(limit: u64) -> Result<()> {
12     let rt_limit_arg = libc::rlimit64 {
13         rlim_cur: limit,
14         rlim_max: limit,
15     };
16     // SAFETY:
17     // Safe because the kernel doesn't modify memory that is accessible to the process here.
18     let res = unsafe { libc::setrlimit64(libc::RLIMIT_RTPRIO, &rt_limit_arg) };
19 
20     if res != 0 {
21         errno_result()
22     } else {
23         Ok(())
24     }
25 }
26 
27 /// Sets the current thread to be scheduled using the round robin real time class with `priority`.
set_rt_round_robin(priority: i32) -> Result<()>28 pub fn set_rt_round_robin(priority: i32) -> Result<()> {
29     // SAFETY:
30     // Safe because sched_param only contains primitive types for which zero
31     // initialization is valid.
32     let mut sched_param: libc::sched_param = unsafe { MaybeUninit::zeroed().assume_init() };
33     sched_param.sched_priority = priority;
34 
35     let res =
36         // SAFETY:
37         // Safe because the kernel doesn't modify memory that is accessible to the process here.
38         unsafe { libc::pthread_setschedparam(libc::pthread_self(), libc::SCHED_RR, &sched_param) };
39 
40     if res != 0 {
41         errno_result()
42     } else {
43         Ok(())
44     }
45 }
46