| 1 |
use crate::mach_sys::*; |
| 2 |
use crate::AudioThreadPriorityError; |
| 3 |
use libc::{pthread_self, pthread_t}; |
| 4 |
use log::info; |
| 5 |
use mach::kern_return::{kern_return_t, KERN_SUCCESS}; |
| 6 |
use mach::mach_time::{mach_timebase_info, mach_timebase_info_data_t}; |
| 7 |
use mach::message::mach_msg_type_number_t; |
| 8 |
use mach::port::mach_port_t; |
| 9 |
use std::mem::size_of; |
| 10 |
|
| 11 |
extern "C" { |
| 12 |
fn pthread_mach_thread_np(tid: pthread_t) -> mach_port_t; |
| 13 |
// Those functions are commented out in thread_policy.h but somehow it works just fine !? |
| 14 |
fn thread_policy_set( |
| 15 |
thread: thread_t, |
| 16 |
flavor: thread_policy_flavor_t, |
| 17 |
policy_info: thread_policy_t, |
| 18 |
count: mach_msg_type_number_t, |
| 19 |
) -> kern_return_t; |
| 20 |
fn thread_policy_get( |
| 21 |
thread: thread_t, |
| 22 |
flavor: thread_policy_flavor_t, |
| 23 |
policy_info: thread_policy_t, |
| 24 |
count: &mut mach_msg_type_number_t, |
| 25 |
get_default: &mut boolean_t, |
| 26 |
) -> kern_return_t; |
| 27 |
} |
| 28 |
|
| 29 |
// can't use size_of in const fn just now in stable, use a macro for now. |
| 30 |
macro_rules! THREAD_TIME_CONSTRAINT_POLICY_COUNT { |
| 31 |
() => { |
| 32 |
(size_of::<thread_time_constraint_policy_data_t>() / size_of::<integer_t>()) as u32 |
| 33 |
}; |
| 34 |
} |
| 35 |
|
| 36 |
#[derive(Debug)] |
| 37 |
pub struct RtPriorityHandleInternal { |
| 38 |
tid: mach_port_t, |
| 39 |
previous_time_constraint_policy: thread_time_constraint_policy_data_t, |
| 40 |
} |
| 41 |
|
| 42 |
impl Default for RtPriorityHandleInternal { |
| 43 |
fn default() -> Self { |
| 44 |
Self::new() |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl RtPriorityHandleInternal { |
| 49 |
pub fn new() -> RtPriorityHandleInternal { |
| 50 |
RtPriorityHandleInternal { |
| 51 |
tid: 0, |
| 52 |
previous_time_constraint_policy: thread_time_constraint_policy_data_t { |
| 53 |
period: 0, |
| 54 |
computation: 0, |
| 55 |
constraint: 0, |
| 56 |
preemptible: 0, |
| 57 |
}, |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
pub fn demote_current_thread_from_real_time_internal( |
| 63 |
rt_priority_handle: RtPriorityHandleInternal, |
| 64 |
) -> Result<(), AudioThreadPriorityError> { |
| 65 |
unsafe { |
| 66 |
let mut h = rt_priority_handle; |
| 67 |
let rv: kern_return_t = thread_policy_set( |
| 68 |
h.tid, |
| 69 |
THREAD_TIME_CONSTRAINT_POLICY, |
| 70 |
(&mut h.previous_time_constraint_policy) as *mut _ as thread_policy_t, |
| 71 |
THREAD_TIME_CONSTRAINT_POLICY_COUNT!(), |
| 72 |
); |
| 73 |
if rv != KERN_SUCCESS { |
| 74 |
return Err(AudioThreadPriorityError::new( |
| 75 |
"thread demotion error: thread_policy_get: RT", |
| 76 |
)); |
| 77 |
} |
| 78 |
|
| 79 |
info!("thread {} priority restored.", h.tid); |
| 80 |
} |
| 81 |
|
| 82 |
Ok(()) |
| 83 |
} |
| 84 |
|
| 85 |
pub fn promote_current_thread_to_real_time_internal( |
| 86 |
audio_buffer_frames: u32, |
| 87 |
audio_samplerate_hz: u32, |
| 88 |
) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { |
| 89 |
let mut rt_priority_handle = RtPriorityHandleInternal::new(); |
| 90 |
|
| 91 |
let buffer_frames = if audio_buffer_frames > 0 { |
| 92 |
audio_buffer_frames |
| 93 |
} else { |
| 94 |
audio_samplerate_hz / 20 |
| 95 |
}; |
| 96 |
|
| 97 |
unsafe { |
| 98 |
let tid: mach_port_t = pthread_mach_thread_np(pthread_self()); |
| 99 |
let mut time_constraints = thread_time_constraint_policy_data_t { |
| 100 |
period: 0, |
| 101 |
computation: 0, |
| 102 |
constraint: 0, |
| 103 |
preemptible: 0, |
| 104 |
}; |
| 105 |
|
| 106 |
// Get current thread attributes, to revert back to the correct setting later if needed. |
| 107 |
rt_priority_handle.tid = tid; |
| 108 |
|
| 109 |
// false: we want to get the current value, not the default value. If this is `false` after |
| 110 |
// returning, it means there are no current settings because of other factor, and the |
| 111 |
// default was returned instead. |
| 112 |
let mut get_default: boolean_t = 0; |
| 113 |
let mut count: mach_msg_type_number_t = THREAD_TIME_CONSTRAINT_POLICY_COUNT!(); |
| 114 |
let mut rv: kern_return_t = thread_policy_get( |
| 115 |
tid, |
| 116 |
THREAD_TIME_CONSTRAINT_POLICY, |
| 117 |
(&mut time_constraints) as *mut _ as thread_policy_t, |
| 118 |
&mut count, |
| 119 |
&mut get_default, |
| 120 |
); |
| 121 |
|
| 122 |
if rv != KERN_SUCCESS { |
| 123 |
return Err(AudioThreadPriorityError::new( |
| 124 |
"thread promotion error: thread_policy_get: time_constraint", |
| 125 |
)); |
| 126 |
} |
| 127 |
|
| 128 |
rt_priority_handle.previous_time_constraint_policy = time_constraints; |
| 129 |
|
| 130 |
let cb_duration = buffer_frames as f32 / (audio_samplerate_hz as f32) * 1000.; |
| 131 |
// The multiplicators are somwhat arbitrary for now. |
| 132 |
|
| 133 |
let mut timebase_info = mach_timebase_info_data_t { denom: 0, numer: 0 }; |
| 134 |
mach_timebase_info(&mut timebase_info); |
| 135 |
|
| 136 |
let ms2abs: f32 = ((timebase_info.denom as f32) / timebase_info.numer as f32) * 1000000.; |
| 137 |
|
| 138 |
// Computation time is half of constraint, per macOS 12 behaviour. |
| 139 |
time_constraints = thread_time_constraint_policy_data_t { |
| 140 |
period: (cb_duration * ms2abs) as u32, |
| 141 |
computation: (cb_duration / 2.0 * ms2abs) as u32, |
| 142 |
constraint: (cb_duration * ms2abs) as u32, |
| 143 |
preemptible: 1, // true |
| 144 |
}; |
| 145 |
|
| 146 |
rv = thread_policy_set( |
| 147 |
tid, |
| 148 |
THREAD_TIME_CONSTRAINT_POLICY, |
| 149 |
(&mut time_constraints) as *mut _ as thread_policy_t, |
| 150 |
THREAD_TIME_CONSTRAINT_POLICY_COUNT!(), |
| 151 |
); |
| 152 |
if rv != KERN_SUCCESS { |
| 153 |
return Err(AudioThreadPriorityError::new( |
| 154 |
"thread promotion error: thread_policy_set: time_constraint", |
| 155 |
)); |
| 156 |
} |
| 157 |
|
| 158 |
info!("thread {} bumped to real time priority.", tid); |
| 159 |
} |
| 160 |
|
| 161 |
Ok(rt_priority_handle) |
| 162 |
} |