diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/audio_thread_priority | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/audio_thread_priority')
12 files changed, 1520 insertions, 0 deletions
diff --git a/third_party/rust/audio_thread_priority/.cargo-checksum.json b/third_party/rust/audio_thread_priority/.cargo-checksum.json new file mode 100644 index 0000000000..b865bcc986 --- /dev/null +++ b/third_party/rust/audio_thread_priority/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"cecac360f214709925c6b06c09687804f936da425ac19ed799bbda73aa216434","Makefile":"0f9a771cfb30c7c4b9961d82fdca4e9e229a955bb2e636474a4101389e18e938","README.md":"bcfa4948edf52fdacd485200a0c1c886a92232cc1931eeb4e1044050f46ec253","atp_test.cpp":"8075a040941a65fb9e3f7cbf0535853ca6661c3ac442ec35569b42b24bbec797","audio_thread_priority.h":"f0ecaf1b674f794cde0dc834028e074d4e4675d22ae96acf08b2ae1dceb3474e","generate_osx_bindings.sh":"06e4e03450f788ced18d31fff5660919e6f6ec1119ddace363ffeb82f0518a71","src/lib.rs":"1399de41dad784370f00e41cdf7185c123eb3d08ef38ee326de7d68c821bf23a","src/mach_sys.rs":"352560fcb9b41d877cff92e5b3b04d6dc68b1f30508ce4b9aed78940120a883e","src/rt_linux.rs":"b038c1d8d6be17756b0acf25bca7b722affd262322241dcf5dd7e93097df7775","src/rt_mach.rs":"a7b4deef4bebcdaa6ca6156fe5de5456bc8a61143d6764d1e3f54e639c9653da","src/rt_win.rs":"347e3ae753cefa38cf913f55f84a63a36d97f6be5f6e3c41e9da0c3ffa71ac17"},"package":"cec7141c59547709f640219f15512026d416c0ca8ce7a5ac1524923ee65be2cb"}
\ No newline at end of file diff --git a/third_party/rust/audio_thread_priority/Cargo.toml b/third_party/rust/audio_thread_priority/Cargo.toml new file mode 100644 index 0000000000..146ba1e007 --- /dev/null +++ b/third_party/rust/audio_thread_priority/Cargo.toml @@ -0,0 +1,53 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "audio_thread_priority" +version = "0.23.4" +authors = ["Paul Adenot <paul@paul.cx>"] +description = "Bump a thread to real-time priority, for audio work, on Linux, Windows and macOS" +license = "MPL-2.0" +repository = "https://github.com/padenot/audio_thread_priority" + +[lib] +name = "audio_thread_priority" +crate-type = ["staticlib", "rlib"] +[dependencies.cfg-if] +version = "0.1" + +[dependencies.log] +version = "0.4" + +[dependencies.simple_logger] +version = "0.4" +optional = true +[dev-dependencies.nix] +version = "0.15.0" + +[features] +terminal-logging = ["simple_logger"] +with_dbus = ["dbus"] +[target."cfg(target_os = \"linux\")".dependencies.dbus] +version = "0.6.4" +optional = true + +[target."cfg(target_os = \"linux\")".dependencies.libc] +version = "0.2" +[target."cfg(target_os = \"macos\")".dependencies.libc] +version = "0.2" + +[target."cfg(target_os = \"macos\")".dependencies.mach] +version = "0.3" +[target."cfg(target_os = \"windows\")".dependencies.winapi] +version = "0.3" +features = ["avrt", "errhandlingapi", "ntdef", "minwindef"] diff --git a/third_party/rust/audio_thread_priority/Makefile b/third_party/rust/audio_thread_priority/Makefile new file mode 100644 index 0000000000..9a0e1c7af7 --- /dev/null +++ b/third_party/rust/audio_thread_priority/Makefile @@ -0,0 +1,8 @@ +all: target/debug/libaudio_thread_priority.a + g++ atp_test.cpp target/debug/libaudio_thread_priority.a -I. -lpthread -ldbus-1 -ldl -g -o atp_test + +check: + @./atp_test && echo "test passed" || echo "test failed" + +target/debug/libaudio_thread_priority.a: + cargo build diff --git a/third_party/rust/audio_thread_priority/README.md b/third_party/rust/audio_thread_priority/README.md new file mode 100644 index 0000000000..66c2522991 --- /dev/null +++ b/third_party/rust/audio_thread_priority/README.md @@ -0,0 +1,37 @@ +# audio_thread_priority + +Synopsis: + +```rust + // ... on a thread that will compute audio and has to be real-time: + RtPriorityHandle handle; + + match promote_current_thread_to_real_time(512, 44100) { + Ok(h) => { + handle = h; + println!("this thread is now bumped to real-time priority.") + } + Err(...) => { println!("could not bump to real time.") } + } + + // do some real-time work. + + match demote_current_thread_from_real_time(h) { + Ok(...) => { + println!("this thread is now bumped back to normal.") + } + Err(...) => { + println!("Could not bring the thread back to normal priority.") + } + } + +``` + +This library can also be used from C or C++ using the included header and +compiling the rust code in the application. By default, a `.a` is compiled to +ease linking. + +# License + +MPL-2 + diff --git a/third_party/rust/audio_thread_priority/atp_test.cpp b/third_party/rust/audio_thread_priority/atp_test.cpp new file mode 100644 index 0000000000..acdd1d22c1 --- /dev/null +++ b/third_party/rust/audio_thread_priority/atp_test.cpp @@ -0,0 +1,30 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <assert.h> +#include "audio_thread_priority.h" + +int main() { +#ifdef __linux__ + atp_thread_info* info = atp_get_current_thread_info(); + atp_thread_info* info2 = nullptr; + + uint8_t buffer[ATP_THREAD_INFO_SIZE]; + atp_serialize_thread_info(info, buffer); + + info2 = atp_deserialize_thread_info(buffer); + + int rv = memcmp(info, info2, ATP_THREAD_INFO_SIZE); + + assert(!rv); + + atp_free_thread_info(info); + atp_free_thread_info(info2); + + rv = atp_set_real_time_limit(0, 44100); + assert(!rv); +#endif + + return 0; +} diff --git a/third_party/rust/audio_thread_priority/audio_thread_priority.h b/third_party/rust/audio_thread_priority/audio_thread_priority.h new file mode 100644 index 0000000000..b883728b6d --- /dev/null +++ b/third_party/rust/audio_thread_priority/audio_thread_priority.h @@ -0,0 +1,153 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef AUDIO_THREAD_PRIORITY_H +#define AUDIO_THREAD_PRIORITY_H + +#include <stdint.h> +#include <stdlib.h> + +/** + * An opaque structure containing information about a thread that was promoted + * to real-time priority. + */ +struct atp_handle; +struct atp_thread_info; +extern size_t ATP_THREAD_INFO_SIZE; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/** + * Promotes the current thread to real-time priority. + * + * audio_buffer_frames: number of frames per audio buffer. If unknown, passing 0 + * will choose an appropriate number, conservatively. If variable, either pass 0 + * or an upper bound. + * audio_samplerate_hz: sample-rate for this audio stream, in Hz + * + * Returns an opaque handle in case of success, NULL otherwise. + */ +atp_handle *atp_promote_current_thread_to_real_time(uint32_t audio_buffer_frames, + uint32_t audio_samplerate_hz); + + +/** + * Demotes the current thread promoted to real-time priority via + * `atp_demote_current_thread_from_real_time` to its previous priority. + * + * Returns 0 in case of success, non-zero otherwise. + */ +int32_t atp_demote_current_thread_from_real_time(atp_handle *handle); + +/** + * Frees an atp_handle. This is useful when it impractical to call + *`atp_demote_current_thread_from_real_time` on the right thread. Access to the + * handle must be synchronized externaly (or the related thread must have + * exited). + * + * Returns 0 in case of success, non-zero otherwise. + */ +int32_t atp_free_handle(atp_handle *handle); + +/* + * Linux-only API. + * + * The Linux backend uses DBUS to promote a thread to real-time priority. In + * environment where this is not possible (due to sandboxing), this set of + * functions allow remoting the call to a process that can make DBUS calls. + * + * To do so: + * - Set the real-time limit from within the process where a + * thread will be promoted. This is a `setrlimit` call, that can be done + * before the sandbox lockdown. + * - Then, gather information on the thread that will be promoted. + * - Serialize this info. + * - Send over the serialized data via an IPC mechanism + * - Deserialize the inf + * - Call `atp_promote_thread_to_real_time` + */ + +#ifdef __linux__ +/** + * Promotes a thread, possibly in another process, to real-time priority. + * + * thread_info: info on the thread to promote, gathered with + * `atp_get_current_thread_info()`, called on the thread itself. + * audio_buffer_frames: number of frames per audio buffer. If unknown, passing 0 + * will choose an appropriate number, conservatively. If variable, either pass 0 + * or an upper bound. + * audio_samplerate_hz: sample-rate for this audio stream, in Hz + * + * Returns an opaque handle in case of success, NULL otherwise. + * + * This call is useful on Linux desktop only, when the process is sandboxed and + * cannot promote itself directly. + */ +atp_handle *atp_promote_thread_to_real_time(atp_thread_info *thread_info); + +/** + * Demotes a thread promoted to real-time priority via + * `atp_demote_thread_from_real_time` to its previous priority. + * + * Returns 0 in case of success, non-zero otherwise. + * + * This call is useful on Linux desktop only, when the process is sandboxed and + * cannot promote itself directly. + */ +int32_t atp_demote_thread_from_real_time(atp_thread_info* thread_info); + +/** + * Gather informations from the calling thread, to be able to promote it from + * another thread and/or process. + * + * Returns a non-null pointer to an `atp_thread_info` structure in case of + * sucess, to be freed later with `atp_free_thread_info`, and NULL otherwise. + * + * This call is useful on Linux desktop only, when the process is sandboxed and + * cannot promote itself directly. + */ +atp_thread_info *atp_get_current_thread_info(); + +/** + * Free an `atp_thread_info` structure. + * + * Returns 0 in case of success, non-zero in case of error (because thread_info + * was NULL). + */ +int32_t atp_free_thread_info(atp_thread_info *thread_info); + +/** + * Serialize an `atp_thread_info` to a byte buffer that is + * sizeof(atp_thread_info) long. + */ +void atp_serialize_thread_info(atp_thread_info *thread_info, uint8_t *bytes); + +/** + * Deserialize a byte buffer of sizeof(atp_thread_info) to an `atp_thread_info` + * pointer. It can be then freed using atp_free_thread_info. + * */ +atp_thread_info* atp_deserialize_thread_info(uint8_t *bytes); + +/** + * Set real-time limit for the calling process. + * + * This is useful only on Linux desktop, and allows remoting the rtkit DBUS call + * to a process that has access to DBUS. This function has to be called before + * attempting to promote threads from another process. + * + * This sets the real-time computation limit. For actually promoting the thread + * to a real-time scheduling class, see `atp_promote_thread_to_real_time`. + */ +int32_t atp_set_real_time_limit(uint32_t audio_buffer_frames, + uint32_t audio_samplerate_hz); + +#endif // __linux__ + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif // AUDIO_THREAD_PRIORITY_H diff --git a/third_party/rust/audio_thread_priority/generate_osx_bindings.sh b/third_party/rust/audio_thread_priority/generate_osx_bindings.sh new file mode 100644 index 0000000000..5d55209045 --- /dev/null +++ b/third_party/rust/audio_thread_priority/generate_osx_bindings.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +bindgen /usr/include/mach/thread_policy.h --no-layout-tests --whitelist-type "(thread_policy_flavor_t|thread_policy_t|thread_extended_policy_data_t|thread_time_constraint_policy_data_t|thread_precedence_policy_data_t|thread_t)" --whitelist-var "(THREAD_TIME_CONSTRAINT_POLICY|THREAD_EXTENDED_POLICY|THREAD_PRECEDENCE_POLICY)" > src/mach_sys.rs diff --git a/third_party/rust/audio_thread_priority/src/lib.rs b/third_party/rust/audio_thread_priority/src/lib.rs new file mode 100644 index 0000000000..2bdc8e7e5f --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/lib.rs @@ -0,0 +1,650 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#[warn(missing_docs)] +use cfg_if::cfg_if; +use std::error::Error; +use std::fmt; + +#[derive(Debug)] +pub struct AudioThreadPriorityError { + message: String, + inner: Option<Box<dyn Error + 'static>>, +} + +impl AudioThreadPriorityError { + cfg_if! { + if #[cfg(all(target_os = "linux", feature = "dbus"))] { + fn new_with_inner(message: &str, inner: Box<dyn Error>) -> AudioThreadPriorityError { + AudioThreadPriorityError { + message: message.into(), + inner: Some(inner), + } + } + } + } + fn new(message: &str) -> AudioThreadPriorityError { + AudioThreadPriorityError { + message: message.into(), + inner: None, + } + } +} + +impl fmt::Display for AudioThreadPriorityError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut rv = write!(f, "AudioThreadPriorityError: {}", &self.message); + if let Some(inner) = &self.inner { + rv = write!(f, " ({})", inner); + } + rv + } +} + +impl Error for AudioThreadPriorityError { + fn description(&self) -> &str { + &self.message + } + + fn source(&self) -> Option<&(dyn Error + 'static)> { + self.inner.as_ref().map(|e| e.as_ref()) + } +} + +cfg_if! { + if #[cfg(target_os = "macos")] { + mod rt_mach; +#[allow(unused, non_camel_case_types, non_snake_case, non_upper_case_globals)] + mod mach_sys; + extern crate mach; + extern crate libc; + use rt_mach::promote_current_thread_to_real_time_internal; + use rt_mach::demote_current_thread_from_real_time_internal; + use rt_mach::RtPriorityHandleInternal; + } else if #[cfg(target_os = "windows")] { + extern crate winapi; + mod rt_win; + use rt_win::promote_current_thread_to_real_time_internal; + use rt_win::demote_current_thread_from_real_time_internal; + use rt_win::RtPriorityHandleInternal; + } else if #[cfg(all(target_os = "linux", feature = "dbus"))] { + mod rt_linux; + extern crate dbus; + extern crate libc; + use rt_linux::promote_current_thread_to_real_time_internal; + use rt_linux::demote_current_thread_from_real_time_internal; + use rt_linux::set_real_time_hard_limit_internal as set_real_time_hard_limit; + use rt_linux::get_current_thread_info_internal; + use rt_linux::promote_thread_to_real_time_internal; + use rt_linux::demote_thread_from_real_time_internal; + use rt_linux::RtPriorityThreadInfoInternal; + use rt_linux::RtPriorityHandleInternal; + #[no_mangle] + /// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI. + pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>(); + } else { + // blanket implementations for Android, Linux Desktop without dbus and others + pub struct RtPriorityHandleInternal {} + #[derive(Clone, Copy)] + pub struct RtPriorityThreadInfoInternal { + _dummy: u8 + } + + cfg_if! { + if #[cfg(not(target_os = "linux"))] { + pub type RtPriorityThreadInfo = RtPriorityThreadInfoInternal; + } + } + + impl RtPriorityThreadInfo { + pub fn serialize(&self) -> [u8; 1] { + [0] + } + pub fn deserialize(_: [u8; 1]) -> Self { + RtPriorityThreadInfo{_dummy: 0} + } + } + pub fn promote_current_thread_to_real_time_internal(_: u32, audio_samplerate_hz: u32) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError{message: "sample rate is zero".to_string(), inner: None}); + } + // no-op + Ok(RtPriorityHandle{}) + } + pub fn demote_current_thread_from_real_time_internal(_: RtPriorityHandle) -> Result<(), AudioThreadPriorityError> { + // no-op + Ok(()) + } + pub fn set_real_time_hard_limit( + _: u32, + _: u32, + ) -> Result<(), AudioThreadPriorityError> { + Ok(()) + } + pub fn get_current_thread_info_internal() -> Result<RtPriorityThreadInfo, AudioThreadPriorityError> { + Ok(RtPriorityThreadInfo{_dummy: 0}) + } + pub fn promote_thread_to_real_time_internal( + _: RtPriorityThreadInfo, + _: u32, + audio_samplerate_hz: u32, + ) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError::new("sample rate is zero")); + } + return Ok(RtPriorityHandle{}); + } + + pub fn demote_thread_from_real_time_internal(_: RtPriorityThreadInfo) -> Result<(), AudioThreadPriorityError> { + return Ok(()); + } + #[no_mangle] + /// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI. + pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>(); + } +} + +/// Opaque handle to a thread handle structure. +pub type RtPriorityHandle = RtPriorityHandleInternal; + +cfg_if! { + if #[cfg(target_os = "linux")] { +/// Opaque handle to a thread info. +/// +/// This can be serialized to raw bytes to be sent via IPC. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +pub type RtPriorityThreadInfo = RtPriorityThreadInfoInternal; + + +/// Get the calling thread's information, to be able to promote it to real-time from somewhere +/// else, later. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Return value +/// +/// Ok in case of success, with an opaque structure containing relevant info for the platform, Err +/// otherwise. +pub fn get_current_thread_info() -> Result<RtPriorityThreadInfo, AudioThreadPriorityError> { + return get_current_thread_info_internal(); +} + +/// Return a byte buffer containing serialized information about a thread, to promote it to +/// real-time from elsewhere. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +pub fn thread_info_serialize( + thread_info: RtPriorityThreadInfo, +) -> [u8; std::mem::size_of::<RtPriorityThreadInfo>()] { + return thread_info.serialize(); +} + +/// From a byte buffer, return a `RtPriorityThreadInfo`. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Arguments +/// +/// A byte buffer containing a serializezd `RtPriorityThreadInfo`. +pub fn thread_info_deserialize( + bytes: [u8; std::mem::size_of::<RtPriorityThreadInfo>()], +) -> RtPriorityThreadInfo { + return RtPriorityThreadInfoInternal::deserialize(bytes); +} + +/// Get the calling threads' information, to promote it from another process or thread, with a C +/// API. +/// +/// This is intended to call on the thread that will end up being promoted to real time priority, +/// but that cannot do it itself (probably because of sandboxing reasons). +/// +/// After use, it MUST be freed by calling `atp_free_thread_info`. +/// +/// # Return value +/// +/// A pointer to a struct that can be serialized and deserialized, and that can be passed to +/// `atp_promote_thread_to_real_time`, even from another process. +#[no_mangle] +pub extern "C" fn atp_get_current_thread_info() -> *mut atp_thread_info { + match get_current_thread_info() { + Ok(thread_info) => Box::into_raw(Box::new(atp_thread_info(thread_info))), + _ => std::ptr::null_mut(), + } +} + +/// Frees a thread info, with a c api. +/// +/// # Arguments +/// +/// thread_info: the `atp_thread_info` structure to free. +/// +/// # Return value +/// +/// 0 in case of success, 1 otherwise (if `thread_info` is NULL). +#[no_mangle] +pub extern "C" fn atp_free_thread_info(thread_info: *mut atp_thread_info) -> i32 { + if thread_info.is_null() { + return 1; + } + unsafe { Box::from_raw(thread_info) }; + 0 +} + +/// Return a byte buffer containing serialized information about a thread, to promote it to +/// real-time from elsewhere, with a C API. +/// +/// `bytes` MUST be `std::mem::size_of<RtPriorityThreadInfo>()` bytes long. +/// +/// This is exposed in the C API as `ATP_THREAD_INFO_SIZE`. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed, cannot promote itself +/// directly, and the `atp_thread_info` struct must be passed via IPC. +#[no_mangle] +pub extern "C" fn atp_serialize_thread_info( + thread_info: *mut atp_thread_info, + bytes: *mut libc::c_void, +) { + let thread_info = unsafe { &mut *thread_info }; + let source = thread_info.0.serialize(); + unsafe { + std::ptr::copy(source.as_ptr(), bytes as *mut u8, source.len()); + } +} + +/// From a byte buffer, return a `RtPriorityThreadInfo`, with a C API. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Arguments +/// +/// A byte buffer containing a serializezd `RtPriorityThreadInfo`. +#[no_mangle] +pub extern "C" fn atp_deserialize_thread_info( + in_bytes: *mut u8, +) -> *mut atp_thread_info { + let bytes = unsafe { *(in_bytes as *mut [u8; std::mem::size_of::<RtPriorityThreadInfoInternal>()]) }; + let thread_info = RtPriorityThreadInfoInternal::deserialize(bytes); + return Box::into_raw(Box::new(atp_thread_info(thread_info))); +} + +/// Promote a particular thread thread to real-time priority. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Arguments +/// +/// * `thread_info` - informations about the thread to promote, gathered using +/// `get_current_thread_info`. +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// This function returns a `Result<RtPriorityHandle>`, which is an opaque struct to be passed to +/// `demote_current_thread_from_real_time` to revert to the previous thread priority. +pub fn promote_thread_to_real_time( + thread_info: RtPriorityThreadInfo, + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError::new("sample rate is zero")); + } + return promote_thread_to_real_time_internal( + thread_info, + audio_buffer_frames, + audio_samplerate_hz, + ); +} + +/// Demotes a thread from real-time priority. +/// +/// # Arguments +/// +/// * `thread_info` - An opaque struct returned from a successful call to +/// `get_current_thread_info`. +/// +/// # Return value +/// +/// `Ok` in case of success, `Err` otherwise. +pub fn demote_thread_from_real_time(thread_info: RtPriorityThreadInfo) -> Result<(), AudioThreadPriorityError> { + return demote_thread_from_real_time_internal(thread_info); +} + +/// Opaque info to a particular thread. +#[allow(non_camel_case_types)] +pub struct atp_thread_info(RtPriorityThreadInfo); + +/// Promote a specific thread to real-time, with a C API. +/// +/// This is useful when the thread to promote cannot make some system calls necessary to promote +/// it. +/// +/// # Arguments +/// +/// `thread_info` - the information of the thread to promote to real-time, gather from calling +/// `atp_get_current_thread_info` on the thread to promote. +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// A pointer to an `atp_handle` in case of success, NULL otherwise. +#[no_mangle] +pub extern "C" fn atp_promote_thread_to_real_time( + thread_info: *mut atp_thread_info, + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> *mut atp_handle { + let thread_info = unsafe { &mut *thread_info }; + match promote_thread_to_real_time(thread_info.0, audio_buffer_frames, audio_samplerate_hz) { + Ok(handle) => Box::into_raw(Box::new(atp_handle(handle))), + _ => std::ptr::null_mut(), + } +} + +/// Demote a thread promoted to from real-time, with a C API. +/// +/// # Arguments +/// +/// `handle` - an opaque struct received from a promoting function. +/// +/// # Return value +/// +/// 0 in case of success, non-zero otherwise. +#[no_mangle] +pub extern "C" fn atp_demote_thread_from_real_time(thread_info: *mut atp_thread_info) -> i32 { + if thread_info.is_null() { + return 1; + } + let thread_info = unsafe { (*thread_info).0 }; + + match demote_thread_from_real_time(thread_info) { + Ok(_) => 0, + _ => 1, + } +} + +/// Set a real-time limit for the calling thread. +/// +/// # Arguments +/// +/// `audio_buffer_frames` - the number of frames the audio callback has to render each quantum. 0 +/// picks a rather high default value. +/// `audio_samplerate_hz` - the sample-rate of the audio stream. +/// +/// # Return value +/// +/// 0 in case of success, 1 otherwise. +#[no_mangle] +pub extern "C" fn atp_set_real_time_limit(audio_buffer_frames: u32, + audio_samplerate_hz: u32) -> i32 { + let r = set_real_time_hard_limit(audio_buffer_frames, audio_samplerate_hz); + if r.is_err() { + return 1; + } + 0 +} + +} +} + +/// Promote the calling thread thread to real-time priority. +/// +/// # Arguments +/// +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// This function returns a `Result<RtPriorityHandle>`, which is an opaque struct to be passed to +/// `demote_current_thread_from_real_time` to revert to the previous thread priority. +pub fn promote_current_thread_to_real_time( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError::new("sample rate is zero")); + } + return promote_current_thread_to_real_time_internal(audio_buffer_frames, audio_samplerate_hz); +} + +/// Demotes the calling thread from real-time priority. +/// +/// # Arguments +/// +/// * `handle` - An opaque struct returned from a successful call to +/// `promote_current_thread_to_real_time`. +/// +/// # Return value +/// +/// `Ok` in scase of success, `Err` otherwise. +pub fn demote_current_thread_from_real_time( + handle: RtPriorityHandle, +) -> Result<(), AudioThreadPriorityError> { + return demote_current_thread_from_real_time_internal(handle); +} + +/// Opaque handle for the C API +#[allow(non_camel_case_types)] +pub struct atp_handle(RtPriorityHandle); + +/// Promote the calling thread thread to real-time priority, with a C API. +/// +/// # Arguments +/// +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// This function returns `NULL` in case of error: if it couldn't bump the thread, or if the +/// `audio_samplerate_hz` is zero. It returns an opaque handle, to be passed to +/// `atp_demote_current_thread_from_real_time` to demote the thread. +/// +/// Additionaly, NULL can be returned in sandboxed processes on Linux, when DBUS cannot be used in +/// the process (for example because the socket to DBUS cannot be created). If this is the case, +/// it's necessary to get the information from the thread to promote and ask another process to +/// promote it (maybe via another privileged process). +#[no_mangle] +pub extern "C" fn atp_promote_current_thread_to_real_time( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> *mut atp_handle { + match promote_current_thread_to_real_time(audio_buffer_frames, audio_samplerate_hz) { + Ok(handle) => Box::into_raw(Box::new(atp_handle(handle))), + _ => std::ptr::null_mut(), + } +} +/// Demotes the calling thread from real-time priority, with a C API. +/// +/// # Arguments +/// +/// * `atp_handle` - An opaque struct returned from a successful call to +/// `atp_promote_current_thread_to_real_time`. +/// +/// # Return value +/// +/// 0 in case of success, non-zero in case of error. +#[no_mangle] +pub extern "C" fn atp_demote_current_thread_from_real_time(handle: *mut atp_handle) -> i32 { + assert!(!handle.is_null()); + let handle = unsafe { Box::from_raw(handle) }; + + match demote_current_thread_from_real_time(handle.0) { + Ok(_) => 0, + _ => 1, + } +} + +/// Frees a handle, with a C API. +/// +/// This is useful when it impractical to call `atp_demote_current_thread_from_real_time` on the +/// right thread. Access to the handle must be synchronized externaly, or the thread that was +/// promoted to real-time priority must have exited. +/// +/// # Arguments +/// +/// * `atp_handle` - An opaque struct returned from a successful call to +/// `atp_promote_current_thread_to_real_time`. +/// +/// # Return value +/// +/// 0 in case of success, non-zero in case of error. +#[no_mangle] +pub extern "C" fn atp_free_handle(handle: *mut atp_handle) -> i32 { + if handle.is_null() { + return 1; + } + let _handle = unsafe { Box::from_raw(handle) }; + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + #[cfg(feature = "terminal-logging")] + use simple_logger; + #[test] + fn it_works() { + #[cfg(feature = "terminal-logging")] + simple_logger::init().unwrap(); + { + assert!(promote_current_thread_to_real_time(0, 0).is_err()); + } + { + match promote_current_thread_to_real_time(0, 44100) { + Ok(rt_prio_handle) => { + demote_current_thread_from_real_time(rt_prio_handle).unwrap(); + assert!(true); + } + Err(e) => { + eprintln!("{}", e.description()); + assert!(false); + } + } + } + { + match promote_current_thread_to_real_time(512, 44100) { + Ok(rt_prio_handle) => { + demote_current_thread_from_real_time(rt_prio_handle).unwrap(); + assert!(true); + } + Err(e) => { + eprintln!("{}", e.description()); + assert!(false); + } + } + } + { + match promote_current_thread_to_real_time(512, 44100) { + Ok(_) => { + assert!(true); + } + Err(e) => { + eprintln!("{}", e.description()); + assert!(false); + } + } + // automatically deallocated, but not demoted until the thread exits. + } + } + cfg_if! { + if #[cfg(target_os = "linux")] { + use nix::unistd::*; + use nix::sys::signal::*; + + #[test] + fn test_linux_api() { + { + let info = get_current_thread_info().unwrap(); + match promote_thread_to_real_time(info, 512, 44100) { + Ok(_) => { + assert!(true); + } + Err(e) => { + eprintln!("{}", e); + assert!(false); + } + } + } + { + let info = get_current_thread_info().unwrap(); + let bytes = info.serialize(); + let info2 = RtPriorityThreadInfo::deserialize(bytes); + assert!(info == info2); + } + { + let info = get_current_thread_info().unwrap(); + let bytes = thread_info_serialize(info); + let info2 = thread_info_deserialize(bytes); + assert!(info == info2); + } + } + #[test] + fn test_remote_promotion() { + let (rd, wr) = pipe().unwrap(); + + match fork().expect("fork failed") { + ForkResult::Parent{ child } => { + eprintln!("Parent PID: {}", getpid()); + let mut bytes = [0 as u8; std::mem::size_of::<RtPriorityThreadInfo>()]; + match read(rd, &mut bytes) { + Ok(_) => { + let info = RtPriorityThreadInfo::deserialize(bytes); + match promote_thread_to_real_time(info, 0, 44100) { + Ok(_) => { + eprintln!("thread promotion in the child from the parent succeeded"); + assert!(true); + } + Err(_) => { + eprintln!("promotion Err"); + kill(child, SIGKILL).expect("Could not kill the child?"); + assert!(false); + } + } + } + Err(e) => { + eprintln!("could not read from the pipe: {}", e); + } + } + kill(child, SIGKILL).expect("Could not kill the child?"); + } + ForkResult::Child => { + let r = set_real_time_hard_limit(0, 44100); + if r.is_err() { + eprintln!("Could not set RT limit, the test will fail."); + } + eprintln!("Child pid: {}", getpid()); + let info = get_current_thread_info().unwrap(); + let bytes = info.serialize(); + match write(wr, &bytes) { + Ok(_) => { + loop { + std::thread::sleep(std::time::Duration::from_millis(1000)); + eprintln!("child sleeping, waiting to be promoted..."); + } + } + Err(_) => { + eprintln!("write error on the pipe."); + } + } + } + } + } + } + } +} diff --git a/third_party/rust/audio_thread_priority/src/mach_sys.rs b/third_party/rust/audio_thread_priority/src/mach_sys.rs new file mode 100644 index 0000000000..f5798adf43 --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/mach_sys.rs @@ -0,0 +1,36 @@ +/* automatically generated by rust-bindgen */ + +pub const THREAD_EXTENDED_POLICY: u32 = 1; +pub const THREAD_TIME_CONSTRAINT_POLICY: u32 = 2; +pub const THREAD_PRECEDENCE_POLICY: u32 = 3; +pub type __darwin_natural_t = ::std::os::raw::c_uint; +pub type __darwin_mach_port_name_t = __darwin_natural_t; +pub type __darwin_mach_port_t = __darwin_mach_port_name_t; +pub type boolean_t = ::std::os::raw::c_uint; +pub type natural_t = __darwin_natural_t; +pub type integer_t = ::std::os::raw::c_int; +pub type mach_port_t = __darwin_mach_port_t; +pub type thread_t = mach_port_t; +pub type thread_policy_flavor_t = natural_t; +pub type thread_policy_t = *mut integer_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct thread_extended_policy { + pub timeshare: boolean_t, +} +pub type thread_extended_policy_data_t = thread_extended_policy; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct thread_time_constraint_policy { + pub period: u32, + pub computation: u32, + pub constraint: u32, + pub preemptible: boolean_t, +} +pub type thread_time_constraint_policy_data_t = thread_time_constraint_policy; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct thread_precedence_policy { + pub importance: integer_t, +} +pub type thread_precedence_policy_data_t = thread_precedence_policy; diff --git a/third_party/rust/audio_thread_priority/src/rt_linux.rs b/third_party/rust/audio_thread_priority/src/rt_linux.rs new file mode 100644 index 0000000000..691992647d --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/rt_linux.rs @@ -0,0 +1,313 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* Widely copied from dbus-rs/dbus/examples/rtkit.rs */ + +extern crate dbus; +extern crate libc; + +use std::cmp; +use std::error::Error; +use std::io::Error as OSError; + +use dbus::{BusType, Connection, Message, MessageItem, Props}; + +use crate::AudioThreadPriorityError; + +const DBUS_SOCKET_TIMEOUT: i32 = 10_000; +const RT_PRIO_DEFAULT: u32 = 10; +// This is different from libc::pid_t, which is 32 bits, and is defined in sys/types.h. +#[allow(non_camel_case_types)] +type kernel_pid_t = libc::c_long; + +impl From<dbus::Error> for AudioThreadPriorityError { + fn from(error: dbus::Error) -> Self { + AudioThreadPriorityError::new(&format!( + "{}:{}", + error.name().unwrap_or("?"), + error.message().unwrap_or("?") + )) + } +} + +impl From<Box<dyn Error>> for AudioThreadPriorityError { + fn from(error: Box<dyn Error>) -> Self { + AudioThreadPriorityError::new(&format!("{}", error.to_string())) + } +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct RtPriorityThreadInfoInternal { + /// System-wise thread id, use to promote the thread via dbus. + thread_id: kernel_pid_t, + /// Process-local thread id, used to restore scheduler characteristics. This information is not + /// useful in another process, but is useful tied to the `thread_id`, when back into the first + /// process. + pthread_id: libc::pthread_t, + /// The PID of the process containing `thread_id` below. + pid: libc::pid_t, + /// ... + policy: libc::c_int, +} + +impl RtPriorityThreadInfoInternal { + /// Serialize a RtPriorityThreadInfoInternal to a byte buffer. + pub fn serialize(&self) -> [u8; std::mem::size_of::<Self>()] { + unsafe { std::mem::transmute::<Self, [u8; std::mem::size_of::<Self>()]>(*self) } + } + /// Get an RtPriorityThreadInfoInternal from a byte buffer. + pub fn deserialize(bytes: [u8; std::mem::size_of::<Self>()]) -> Self { + unsafe { std::mem::transmute::<[u8; std::mem::size_of::<Self>()], Self>(bytes) } + } +} + +impl PartialEq for RtPriorityThreadInfoInternal { + fn eq(&self, other: &Self) -> bool { + self.thread_id == other.thread_id && self.pthread_id == other.pthread_id + } +} + +/*#[derive(Debug)]*/ +pub struct RtPriorityHandleInternal { + thread_info: RtPriorityThreadInfoInternal, +} + +fn item_as_i64(i: MessageItem) -> Result<i64, AudioThreadPriorityError> { + match i { + MessageItem::Int32(i) => Ok(i as i64), + MessageItem::Int64(i) => Ok(i), + _ => Err(AudioThreadPriorityError::new(&format!( + "Property is not integer ({:?})", + i + ))), + } +} + +fn rtkit_set_realtime(thread: u64, pid: u64, prio: u32) -> Result<(), Box<dyn Error>> { + let m = if unsafe { libc::getpid() as u64 } == pid { + let mut m = Message::new_method_call( + "org.freedesktop.RealtimeKit1", + "/org/freedesktop/RealtimeKit1", + "org.freedesktop.RealtimeKit1", + "MakeThreadRealtime", + )?; + m.append_items(&[thread.into(), prio.into()]); + m + } else { + let mut m = Message::new_method_call( + "org.freedesktop.RealtimeKit1", + "/org/freedesktop/RealtimeKit1", + "org.freedesktop.RealtimeKit1", + "MakeThreadRealtimeWithPID", + )?; + m.append_items(&[pid.into(), thread.into(), prio.into()]); + m + }; + let c = Connection::get_private(BusType::System)?; + c.send_with_reply_and_block(m, DBUS_SOCKET_TIMEOUT)?; + return Ok(()); +} + +/// Returns the maximum priority, maximum real-time time slice, and the current real-time time +/// slice for this process. +fn get_limits() -> Result<(i64, u64, libc::rlimit64), AudioThreadPriorityError> { + let c = Connection::get_private(BusType::System)?; + + let p = Props::new( + &c, + "org.freedesktop.RealtimeKit1", + "/org/freedesktop/RealtimeKit1", + "org.freedesktop.RealtimeKit1", + DBUS_SOCKET_TIMEOUT, + ); + let mut current_limit = libc::rlimit64 { + rlim_cur: 0, + rlim_max: 0, + }; + + let max_prio = item_as_i64(p.get("MaxRealtimePriority")?)?; + if max_prio < 0 { + return Err(AudioThreadPriorityError::new( + "invalid negative MaxRealtimePriority", + )); + } + + let max_rttime = item_as_i64(p.get("RTTimeUSecMax")?)?; + if max_rttime < 0 { + return Err(AudioThreadPriorityError::new( + "invalid negative RTTimeUSecMax", + )); + } + + if unsafe { libc::getrlimit64(libc::RLIMIT_RTTIME, &mut current_limit) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + &"getrlimit64", + Box::new(OSError::last_os_error()), + )); + } + + Ok((max_prio, (max_rttime as u64), current_limit)) +} + +fn set_limits(request: u64, max: u64) -> Result<(), AudioThreadPriorityError> { + // Set a soft limit to the limit requested, to be able to handle going over the limit using + // SIGXCPU. Set the hard limit to the maxium slice to prevent getting SIGKILL. + let new_limit = libc::rlimit64 { + rlim_cur: request, + rlim_max: max, + }; + if unsafe { libc::setrlimit64(libc::RLIMIT_RTTIME, &new_limit) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + "setrlimit64", + Box::new(OSError::last_os_error()), + )); + } + + Ok(()) +} + +pub fn promote_current_thread_to_real_time_internal( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let thread_info = get_current_thread_info_internal()?; + promote_thread_to_real_time_internal(thread_info, audio_buffer_frames, audio_samplerate_hz) +} + +pub fn demote_current_thread_from_real_time_internal( + rt_priority_handle: RtPriorityHandleInternal, +) -> Result<(), AudioThreadPriorityError> { + assert!(unsafe { libc::pthread_self() } == rt_priority_handle.thread_info.pthread_id); + + let param = unsafe { std::mem::zeroed::<libc::sched_param>() }; + + if unsafe { + libc::pthread_setschedparam( + rt_priority_handle.thread_info.pthread_id, + rt_priority_handle.thread_info.policy, + ¶m, + ) + } < 0 + { + return Err(AudioThreadPriorityError::new_with_inner( + &"could not demote thread", + Box::new(OSError::last_os_error()), + )); + } + return Ok(()); +} + +/// This can be called by sandboxed code, it only restores priority to what they were. +pub fn demote_thread_from_real_time_internal( + thread_info: RtPriorityThreadInfoInternal, +) -> Result<(), AudioThreadPriorityError> { + let param = unsafe { std::mem::zeroed::<libc::sched_param>() }; + + // https://github.com/rust-lang/libc/issues/1511 + const SCHED_RESET_ON_FORK: libc::c_int = 0x40000000; + + if unsafe { + libc::pthread_setschedparam( + thread_info.pthread_id, + libc::SCHED_OTHER | SCHED_RESET_ON_FORK, + ¶m, + ) + } < 0 + { + return Err(AudioThreadPriorityError::new_with_inner( + &"could not demote thread", + Box::new(OSError::last_os_error()), + )); + } + return Ok(()); +} + +/// Get the current thread information, as an opaque struct, that can be serialized and sent +/// accross processes. This is enough to capture the current state of the scheduling policy, and +/// an identifier to have another thread promoted to real-time. +pub fn get_current_thread_info_internal( +) -> Result<RtPriorityThreadInfoInternal, AudioThreadPriorityError> { + let thread_id = unsafe { libc::syscall(libc::SYS_gettid) }; + let pthread_id = unsafe { libc::pthread_self() }; + let mut param = unsafe { std::mem::zeroed::<libc::sched_param>() }; + let mut policy = 0; + + if unsafe { libc::pthread_getschedparam(pthread_id, &mut policy, &mut param) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + &"pthread_getschedparam", + Box::new(OSError::last_os_error()), + )); + } + + let pid = unsafe { libc::getpid() }; + + Ok(RtPriorityThreadInfoInternal { + pid, + thread_id, + pthread_id, + policy, + }) +} + +/// This set the RLIMIT_RTTIME resource to something other than "unlimited". It's necessary for the +/// rtkit request to succeed, and needs to hapen in the child. We can't get the real limit here, +/// because we don't have access to DBUS, so it is hardcoded to 200ms, which is the default in the +/// rtkit package. +pub fn set_real_time_hard_limit_internal( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<(), AudioThreadPriorityError> { + let buffer_frames = if audio_buffer_frames > 0 { + audio_buffer_frames + } else { + // 50ms slice. This "ought to be enough for anybody". + audio_samplerate_hz / 20 + }; + let budget_us = (buffer_frames * 1_000_000 / audio_samplerate_hz) as u64; + + // It's only necessary to set RLIMIT_RTTIME to something when in the child, skip it if it's a + // remoting call. + let (_, max_rttime, _) = get_limits()?; + + // Only take what we need, or cap at the system limit, no further. + let rttime_request = cmp::min(budget_us, max_rttime as u64); + set_limits(rttime_request, max_rttime)?; + + Ok(()) +} + +/// Promote a thread (possibly in another process) identified by its tid, to real-time. +pub fn promote_thread_to_real_time_internal( + thread_info: RtPriorityThreadInfoInternal, + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let RtPriorityThreadInfoInternal { pid, thread_id, .. } = thread_info; + + let handle = RtPriorityHandleInternal { thread_info }; + + let (_, _, limits) = get_limits()?; + set_real_time_hard_limit_internal(audio_buffer_frames, audio_samplerate_hz)?; + + let r = rtkit_set_realtime(thread_id as u64, pid as u64, RT_PRIO_DEFAULT); + + match r { + Ok(_) => { + return Ok(handle); + } + Err(e) => { + if unsafe { libc::setrlimit64(libc::RLIMIT_RTTIME, &limits) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + &"setrlimit64", + Box::new(OSError::last_os_error()), + )); + } + return Err(AudioThreadPriorityError::new_with_inner( + &"Thread promotion error", + e, + )); + } + } +} diff --git a/third_party/rust/audio_thread_priority/src/rt_mach.rs b/third_party/rust/audio_thread_priority/src/rt_mach.rs new file mode 100644 index 0000000000..40ede26340 --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/rt_mach.rs @@ -0,0 +1,162 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use crate::mach_sys::*; +use crate::AudioThreadPriorityError; +use libc::{pthread_self, pthread_t}; +use log::info; +use mach::kern_return::{kern_return_t, KERN_SUCCESS}; +use mach::mach_time::{mach_timebase_info, mach_timebase_info_data_t}; +use mach::message::mach_msg_type_number_t; +use mach::port::mach_port_t; +use std::mem::size_of; + +extern "C" { + fn pthread_mach_thread_np(tid: pthread_t) -> mach_port_t; + // Those functions are commented out in thread_policy.h but somehow it works just fine !? + fn thread_policy_set( + thread: thread_t, + flavor: thread_policy_flavor_t, + policy_info: thread_policy_t, + count: mach_msg_type_number_t, + ) -> kern_return_t; + fn thread_policy_get( + thread: thread_t, + flavor: thread_policy_flavor_t, + policy_info: thread_policy_t, + count: &mut mach_msg_type_number_t, + get_default: &mut boolean_t, + ) -> kern_return_t; +} + +// can't use size_of in const fn just now in stable, use a macro for now. +macro_rules! THREAD_TIME_CONSTRAINT_POLICY_COUNT { + () => { + (size_of::<thread_time_constraint_policy_data_t>() / size_of::<integer_t>()) as u32; + }; +} + +#[derive(Debug)] +pub struct RtPriorityHandleInternal { + tid: mach_port_t, + previous_time_constraint_policy: thread_time_constraint_policy_data_t, +} + +impl RtPriorityHandleInternal { + pub fn new() -> RtPriorityHandleInternal { + return RtPriorityHandleInternal { + tid: 0, + previous_time_constraint_policy: thread_time_constraint_policy_data_t { + period: 0, + computation: 0, + constraint: 0, + preemptible: 0, + }, + }; + } +} + +pub fn demote_current_thread_from_real_time_internal( + rt_priority_handle: RtPriorityHandleInternal, +) -> Result<(), AudioThreadPriorityError> { + unsafe { + let rv: kern_return_t; + let mut h = rt_priority_handle; + rv = thread_policy_set( + h.tid, + THREAD_TIME_CONSTRAINT_POLICY, + (&mut h.previous_time_constraint_policy) as *mut _ as thread_policy_t, + THREAD_TIME_CONSTRAINT_POLICY_COUNT!(), + ); + if rv != KERN_SUCCESS as i32 { + return Err(AudioThreadPriorityError::new( + "thread demotion error: thread_policy_get: RT", + )); + } + + info!("thread {} priority restored.", h.tid); + } + + return Ok(()); +} + +pub fn promote_current_thread_to_real_time_internal( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let mut rt_priority_handle = RtPriorityHandleInternal::new(); + + let buffer_frames = if audio_buffer_frames > 0 { + audio_buffer_frames + } else { + audio_samplerate_hz / 20 + }; + + unsafe { + let tid: mach_port_t = pthread_mach_thread_np(pthread_self()); + let mut rv: kern_return_t; + let mut time_constraints = thread_time_constraint_policy_data_t { + period: 0, + computation: 0, + constraint: 0, + preemptible: 0, + }; + let mut count: mach_msg_type_number_t; + + // Get current thread attributes, to revert back to the correct setting later if needed. + rt_priority_handle.tid = tid; + + // false: we want to get the current value, not the default value. If this is `false` after + // returning, it means there are no current settings because of other factor, and the + // default was returned instead. + let mut get_default: boolean_t = 0; + count = THREAD_TIME_CONSTRAINT_POLICY_COUNT!(); + rv = thread_policy_get( + tid, + THREAD_TIME_CONSTRAINT_POLICY, + (&mut time_constraints) as *mut _ as thread_policy_t, + &mut count, + &mut get_default, + ); + + if rv != KERN_SUCCESS as i32 { + return Err(AudioThreadPriorityError::new( + "thread promotion error: thread_policy_get: time_constraint", + )); + } + + rt_priority_handle.previous_time_constraint_policy = time_constraints; + + let cb_duration = buffer_frames as f32 / (audio_samplerate_hz as f32) * 1000.; + // The multiplicators are somwhat arbitrary for now. + + let mut timebase_info = mach_timebase_info_data_t { denom: 0, numer: 0 }; + mach_timebase_info(&mut timebase_info); + + let ms2abs: f32 = ((timebase_info.denom as f32) / timebase_info.numer as f32) * 1000000.; + + time_constraints = thread_time_constraint_policy_data_t { + period: (cb_duration * ms2abs) as u32, + computation: (0.3 * ms2abs) as u32, // fixed 300us computation time + constraint: (cb_duration * ms2abs) as u32, + preemptible: 1, // true + }; + + rv = thread_policy_set( + tid, + THREAD_TIME_CONSTRAINT_POLICY, + (&mut time_constraints) as *mut _ as thread_policy_t, + THREAD_TIME_CONSTRAINT_POLICY_COUNT!(), + ); + if rv != KERN_SUCCESS as i32 { + return Err(AudioThreadPriorityError::new( + "thread promotion error: thread_policy_set: time_constraint", + )); + } + + info!("thread {} bumped to real time priority.", tid); + } + + Ok(rt_priority_handle) +} diff --git a/third_party/rust/audio_thread_priority/src/rt_win.rs b/third_party/rust/audio_thread_priority/src/rt_win.rs new file mode 100644 index 0000000000..14e76e9165 --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/rt_win.rs @@ -0,0 +1,74 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use winapi::shared::minwindef::DWORD; +use winapi::shared::ntdef::HANDLE; +use winapi::um::avrt::*; +use winapi::um::errhandlingapi::GetLastError; + +use crate::AudioThreadPriorityError; + +use log::info; + +#[derive(Debug)] +pub struct RtPriorityHandleInternal { + mmcss_task_index: DWORD, + task_handle: HANDLE, +} + +impl RtPriorityHandleInternal { + pub fn new() -> RtPriorityHandleInternal { + return RtPriorityHandleInternal { + mmcss_task_index: 0 as DWORD, + task_handle: 0 as HANDLE, + }; + } +} + +pub fn demote_current_thread_from_real_time_internal( + rt_priority_handle: RtPriorityHandleInternal, +) -> Result<(), AudioThreadPriorityError> { + unsafe { + let rv = AvRevertMmThreadCharacteristics(rt_priority_handle.task_handle); + if rv == 0 { + return Err(AudioThreadPriorityError::new(&format!( + "Unable to restore the thread priority ({})", + GetLastError() + ))); + } + } + + info!( + "task {} priority restored.", + rt_priority_handle.mmcss_task_index + ); + + return Ok(()); +} + +pub fn promote_current_thread_to_real_time_internal( + _audio_buffer_frames: u32, + _audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let mut handle = RtPriorityHandleInternal::new(); + + unsafe { + handle.task_handle = + AvSetMmThreadCharacteristicsA("Audio\0".as_ptr() as _, &mut handle.mmcss_task_index); + + if handle.task_handle.is_null() { + return Err(AudioThreadPriorityError::new(&format!( + "Unable to restore the thread priority ({})", + GetLastError() + ))); + } + } + + info!( + "task {} bumped to real time priority.", + handle.mmcss_task_index + ); + + Ok(handle) +} |