diff --git a/src/host/wasapi/stream.rs b/src/host/wasapi/stream.rs index 72c7836..3541912 100644 --- a/src/host/wasapi/stream.rs +++ b/src/host/wasapi/stream.rs @@ -9,7 +9,6 @@ use std::ptr; use std::sync::mpsc::{channel, Receiver, SendError, Sender}; use std::thread::{self, JoinHandle}; use windows::Win32::Foundation; -use windows::Win32::Foundation::HANDLE; use windows::Win32::Foundation::WAIT_OBJECT_0; use windows::Win32::Media::Audio; use windows::Win32::System::SystemServices; @@ -18,8 +17,6 @@ use windows::Win32::System::Threading; pub struct Stream { /// The high-priority audio processing thread calling callbacks. /// Option used for moving out in destructor. - /// - /// TODO: Actually set the thread priority. thread: Option>, // Commands processed by the `run()` method that is currently running. @@ -269,7 +266,9 @@ fn run_input( data_callback: &mut dyn FnMut(&Data, &InputCallbackInfo), error_callback: &mut dyn FnMut(StreamError), ) { - boost_current_thread_priority(); + if let Err(err) = boost_current_thread_priority() { + error_callback(err); + } loop { match process_commands_and_await_signal(&mut run_ctxt, error_callback) { @@ -298,7 +297,9 @@ fn run_output( data_callback: &mut dyn FnMut(&mut Data, &OutputCallbackInfo), error_callback: &mut dyn FnMut(StreamError), ) { - boost_current_thread_priority(); + if let Err(err) = boost_current_thread_priority() { + error_callback(err); + } loop { match process_commands_and_await_signal(&mut run_ctxt, error_callback) { @@ -322,17 +323,21 @@ fn run_output( } } -fn boost_current_thread_priority() { +// Priority boosting remains best-effort; callers report failure without stopping the stream. +fn boost_current_thread_priority() -> Result<(), StreamError> { unsafe { - let thread_id = Threading::GetCurrentThreadId(); - - let _ = Threading::SetThreadPriority( - HANDLE(thread_id as isize), + Threading::SetThreadPriority( + Threading::GetCurrentThread(), Threading::THREAD_PRIORITY_TIME_CRITICAL, - ); + ) } + .map_err(|err| super::windows_err_to_cpal_err_message(err, "SetThreadPriority failed: ")) } +#[cfg(test)] +#[path = "stream_priority_tests.rs"] +mod priority_tests; + enum ControlFlow { Break, Continue, diff --git a/src/host/wasapi/stream_priority_tests.rs b/src/host/wasapi/stream_priority_tests.rs new file mode 100644 index 0000000..8e0deee --- /dev/null +++ b/src/host/wasapi/stream_priority_tests.rs @@ -0,0 +1,60 @@ +use super::boost_current_thread_priority; +use windows::Win32::System::Threading; + +const THREAD_PRIORITY_ERROR_RETURN: i32 = i32::MAX; + +#[test] +fn boost_changes_the_calling_thread_priority() { + assert_boost_from(Threading::THREAD_PRIORITY_NORMAL); +} + +#[test] +fn boost_works_from_below_normal_priority() { + assert_boost_from(Threading::THREAD_PRIORITY_BELOW_NORMAL); +} + +#[test] +fn boost_works_from_above_normal_priority() { + assert_boost_from(Threading::THREAD_PRIORITY_ABOVE_NORMAL); +} + +#[test] +fn boost_preserves_time_critical_priority() { + assert_boost_from(Threading::THREAD_PRIORITY_TIME_CRITICAL); +} + +fn assert_boost_from(initial: Threading::THREAD_PRIORITY) { + let parent = unsafe { Threading::GetCurrentThread() }; + let parent_priority = unsafe { Threading::GetThreadPriority(parent) }; + assert_ne!(parent_priority, THREAD_PRIORITY_ERROR_RETURN); + std::thread::spawn(move || unsafe { + let thread = Threading::GetCurrentThread(); + let original = Threading::GetThreadPriority(thread); + assert_ne!(original, THREAD_PRIORITY_ERROR_RETURN); + Threading::SetThreadPriority(thread, initial) + .expect("could not establish initial priority"); + + let result = boost_current_thread_priority(); + let observed = Threading::GetThreadPriority(thread); + let repeated = boost_current_thread_priority(); + let observed_again = Threading::GetThreadPriority(thread); + let restored = Threading::SetThreadPriority(thread, Threading::THREAD_PRIORITY(original)); + + restored.expect("could not restore the original priority"); + result.expect("priority boost failed"); + repeated.expect("repeated priority boost failed"); + assert_eq!( + observed, + Threading::THREAD_PRIORITY_TIME_CRITICAL.0, + "the production priority helper did not boost its calling thread" + ); + assert_eq!(observed_again, observed); + assert_eq!(Threading::GetThreadPriority(thread), original); + }) + .join() + .expect("priority regression thread failed"); + assert_eq!( + unsafe { Threading::GetThreadPriority(parent) }, + parent_priority + ); +}