• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium OS Authors. All rights reserved.
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 crate::{errno_result, Result};
6 
7 const AUDIO_THREAD_RTPRIO: u16 = 10; // Matches other cros audio clients.
8 
set_audio_thread_priorities() -> Result<()>9 pub fn set_audio_thread_priorities() -> Result<()> {
10     set_rt_prio_limit(u64::from(AUDIO_THREAD_RTPRIO))?;
11     set_rt_round_robin(i32::from(AUDIO_THREAD_RTPRIO))?;
12     Ok(())
13 }
14 /// Enables real time thread priorities in the current thread up to `limit`.
set_rt_prio_limit(limit: u64) -> Result<()>15 pub fn set_rt_prio_limit(limit: u64) -> Result<()> {
16     let rt_limit_arg = libc::rlimit {
17         rlim_cur: limit as libc::rlim_t,
18         rlim_max: limit as libc::rlim_t,
19     };
20     // Safe because the kernel doesn't modify memory that is accessible to the process here.
21     let res = unsafe { libc::setrlimit(libc::RLIMIT_RTPRIO, &rt_limit_arg) };
22 
23     if res != 0 {
24         errno_result()
25     } else {
26         Ok(())
27     }
28 }
29 
30 /// Sets the current thread to be scheduled using the round robin real time class with `priority`.
set_rt_round_robin(priority: i32) -> Result<()>31 pub fn set_rt_round_robin(priority: i32) -> Result<()> {
32     let sched_param = libc::sched_param {
33         sched_priority: priority,
34     };
35 
36     // Safe because the kernel doesn't modify memory that is accessible to the process here.
37     let res =
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