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<()>11pub 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 // Safe because the kernel doesn't modify memory that is accessible to the process here. 17 let res = unsafe { libc::setrlimit64(libc::RLIMIT_RTPRIO, &rt_limit_arg) }; 18 19 if res != 0 { 20 errno_result() 21 } else { 22 Ok(()) 23 } 24 } 25 26 /// Sets the current thread to be scheduled using the round robin real time class with `priority`. set_rt_round_robin(priority: i32) -> Result<()>27pub fn set_rt_round_robin(priority: i32) -> Result<()> { 28 // Safe because sched_param only contains primitive types for which zero 29 // initialization is valid. 30 let mut sched_param: libc::sched_param = unsafe { MaybeUninit::zeroed().assume_init() }; 31 sched_param.sched_priority = priority; 32 33 // Safe because the kernel doesn't modify memory that is accessible to the process here. 34 let res = 35 unsafe { libc::pthread_setschedparam(libc::pthread_self(), libc::SCHED_RR, &sched_param) }; 36 37 if res != 0 { 38 errno_result() 39 } else { 40 Ok(()) 41 } 42 } 43