diff --git a/playback/src/player.rs b/playback/src/player.rs index 6b1ba10cd..1e07b8d58 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -81,7 +81,7 @@ struct PlayerInternal { player_id: usize, } -static PLAYER_COUNTER: AtomicUsize = AtomicUsize::new(0); +pub static PLAYER_COUNTER: AtomicUsize = AtomicUsize::new(0); enum PlayerCommand { Load { @@ -507,11 +507,11 @@ impl Player { impl Drop for Player { fn drop(&mut self) { - debug!("Shutting down player thread ..."); + debug!("Shutting down thread ..."); self.commands = None; if let Some(handle) = self.thread_handle.take() { if let Err(e) = handle.join() { - error!("Player thread Error: {:?}", e); + error!(" thread Error: {:?}", e); } } } @@ -1953,7 +1953,12 @@ impl PlayerInternal { let load_handles_clone = self.load_handles.clone(); let handle = tokio::runtime::Handle::current(); - let thread_name = format!("loader:{}", spotify_id.to_uri().unwrap_or_default()); + // The player increments the player id when it gets it... + let thread_name = format!( + "loader:{player_id}:{}", + PLAYER_COUNTER.load(Ordering::Relaxed).saturating_sub(1), + spotify_id.to_uri().unwrap_or_default() + ); let builder = thread::Builder::new().name(thread_name.clone()); diff --git a/playback/src/resampler.rs b/playback/src/resampler.rs index f9da63b0f..a087d2fc7 100644 --- a/playback/src/resampler.rs +++ b/playback/src/resampler.rs @@ -2,12 +2,14 @@ use std::{ collections::{vec_deque, VecDeque}, marker::Send, process::exit, + sync::atomic::Ordering, sync::mpsc, thread, }; use crate::{ config::{InterpolationQuality, SampleRate}, + player::PLAYER_COUNTER, RESAMPLER_INPUT_SIZE, SAMPLE_RATE as SOURCE_SAMPLE_RATE, }; @@ -287,8 +289,8 @@ impl ResampleWorker { } ResampleTask::Terminate => { match thread::current().name() { - Some(name) => debug!("drop [{name}] thread"), - None => debug!("drop thread"), + Some(name) => debug!(" [{name}] thread finished"), + None => debug!(" thread finished"), } break; @@ -353,6 +355,7 @@ impl ResampleWorker { impl Drop for ResampleWorker { fn drop(&mut self) { + debug!("Shutting down thread ..."); self.task_sender .take() .and_then(|sender| sender.send(ResampleTask::Terminate).ok()); @@ -399,8 +402,11 @@ impl StereoInterleavedResampler { _ => { debug!("Interpolation Quality: {interpolation_quality}"); - let left_thread_name = "resampler:left".to_string(); - let right_thread_name = "resampler:right".to_string(); + // The player increments the player id when it gets it... + let player_id = PLAYER_COUNTER.load(Ordering::Relaxed).saturating_sub(1); + + let left_thread_name = format!("resampler:{player_id}:left"); + let right_thread_name = format!("resampler:{player_id}:right"); match interpolation_quality { InterpolationQuality::Low => { diff --git a/src/player_event_handler.rs b/src/player_event_handler.rs index d9d6de21e..9161cd304 100644 --- a/src/player_event_handler.rs +++ b/src/player_event_handler.rs @@ -1,10 +1,10 @@ use log::{debug, error, warn}; -use std::{collections::HashMap, process::Command, thread}; +use std::{collections::HashMap, process::exit, process::Command, sync::atomic::Ordering, thread}; use librespot::{ metadata::audio::UniqueFields, - playback::player::{PlayerEvent, PlayerEventChannel, SinkStatus}, + playback::player::{PlayerEvent, PlayerEventChannel, SinkStatus, PLAYER_COUNTER}, }; pub struct EventHandler { @@ -14,9 +14,21 @@ pub struct EventHandler { impl EventHandler { pub fn new(mut player_events: PlayerEventChannel, onevent: &str) -> Self { let on_event = onevent.to_string(); - let thread_handle = Some(thread::spawn(move || loop { + + let thread_name = format!("event-handler:{}", PLAYER_COUNTER.load(Ordering::Relaxed)); + + let builder = thread::Builder::new().name(thread_name.clone()); + + let thread_handle = match builder.spawn(move || loop { match player_events.blocking_recv() { - None => break, + None => { + match thread::current().name() { + Some(name) => debug!(" [{name}] thread finished"), + None => debug!(" thread finished"), + } + + break; + } Some(event) => { let mut env_vars = HashMap::new(); @@ -245,18 +257,29 @@ impl EventHandler { } } } - })); + }) { + Ok(handle) => { + debug!("Created [{thread_name}] thread"); + handle + } + Err(e) => { + error!("Error creating [{thread_name}] thread: {e}"); + exit(1); + } + }; - Self { thread_handle } + Self { + thread_handle: Some(thread_handle), + } } } impl Drop for EventHandler { fn drop(&mut self) { - debug!("Shutting down EventHandler thread ..."); + debug!("Shutting down thread ..."); if let Some(handle) = self.thread_handle.take() { if let Err(e) = handle.join() { - error!("EventHandler thread Error: {:?}", e); + error!(" thread Error: {:?}", e); } } }