Skip to content

Commit

Permalink
Harmonize thread names and debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLG1979 committed Jun 23, 2023
1 parent 8d35b4b commit 0e3ffe5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
13 changes: 9 additions & 4 deletions playback/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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());

Expand Down
14 changes: 10 additions & 4 deletions playback/src/resampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 => {
Expand Down
43 changes: 35 additions & 8 deletions src/player_event_handler.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 0e3ffe5

Please sign in to comment.