sys_util: Add ability to set real time thread priority

Add the minimal amount of functionality needed for audio threads that
need to run with real time priority.

Change-Id: I7052e0f2ba6b9179229fc4568b332952ee32f076
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1366542
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
This commit is contained in:
Dylan Reid 2018-12-06 22:53:39 +00:00 committed by chrome-bot
parent d4d9c26f04
commit 42c409c4d7
2 changed files with 40 additions and 0 deletions

View file

@ -28,6 +28,7 @@ mod guest_memory;
mod mmap;
mod passwd;
mod poll;
mod priority;
mod seek_hole;
mod shm;
pub mod signal;
@ -51,6 +52,7 @@ pub use mmap::*;
pub use passwd::*;
pub use poll::*;
pub use poll_token_derive::*;
pub use priority::*;
pub use shm::*;
pub use signal::*;
pub use signalfd::*;

38
sys_util/src/priority.rs Normal file
View file

@ -0,0 +1,38 @@
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use {errno_result, Result};
/// Enables real time thread priorities in the current thread up to `limit`.
pub fn set_rt_prio_limit(limit: u64) -> Result<()> {
let rt_limit_arg = libc::rlimit {
rlim_cur: limit as libc::rlim_t,
rlim_max: limit as libc::rlim_t,
};
// Safe because the kernel doesn't modify memory that is accessible to the process here.
let res = unsafe { libc::setrlimit(libc::RLIMIT_RTPRIO, &rt_limit_arg) };
if res != 0 {
errno_result()
} else {
Ok(())
}
}
/// Sets the current thread to be scheduled using the round robin real time class with `priority`.
pub fn set_rt_round_robin(priority: i32) -> Result<()> {
let sched_param = libc::sched_param {
sched_priority: priority,
};
// Safe because the kernel doesn't modify memory that is accessible to the process here.
let res =
unsafe { libc::pthread_setschedparam(libc::pthread_self(), libc::SCHED_RR, &sched_param) };
if res != 0 {
errno_result()
} else {
Ok(())
}
}