Harmonize thread names and debug messages

This commit is contained in:
JasonLG1979 2023-06-23 02:26:57 -05:00
parent 8d35b4b860
commit 0e3ffe5394
3 changed files with 54 additions and 16 deletions

View file

@ -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 <Player> thread ...");
self.commands = None;
if let Some(handle) = self.thread_handle.take() {
if let Err(e) = handle.join() {
error!("Player thread Error: {:?}", e);
error!("<Player> 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_COUNTER.load(Ordering::Relaxed).saturating_sub(1),
spotify_id.to_uri().unwrap_or_default()
);
let builder = thread::Builder::new().name(thread_name.clone());

View file

@ -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 <ResampleWorker> [{name}] thread"),
None => debug!("drop <ResampleWorker> thread"),
Some(name) => debug!("<ResampleWorker> [{name}] thread finished"),
None => debug!("<ResampleWorker> thread finished"),
}
break;
@ -353,6 +355,7 @@ impl ResampleWorker {
impl Drop for ResampleWorker {
fn drop(&mut self) {
debug!("Shutting down <ResampleWorker> 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 => {

View file

@ -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,25 @@ 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 {
// The player increments the player id when it gets it...
let thread_name = format!(
"event-handler:{}",
PLAYER_COUNTER.load(Ordering::Relaxed).saturating_sub(1)
);
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!("<EventHandler> [{name}] thread finished"),
None => debug!("<EventHandler> thread finished"),
}
break;
}
Some(event) => {
let mut env_vars = HashMap::new();
@ -245,18 +261,29 @@ impl EventHandler {
}
}
}
}));
}) {
Ok(handle) => {
debug!("Created <EventHandler> [{thread_name}] thread");
handle
}
Err(e) => {
error!("Error creating <EventHandler> [{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 <EventHandler> thread ...");
if let Some(handle) = self.thread_handle.take() {
if let Err(e) = handle.join() {
error!("EventHandler thread Error: {:?}", e);
error!("<EventHandler> thread Error: {:?}", e);
}
}
}