From d156c04a04c7564dde2d75f7c443143f4e2cf344 Mon Sep 17 00:00:00 2001 From: lifning <> Date: Tue, 17 Aug 2021 16:50:26 -0700 Subject: [PATCH] fix SDL2 audio thread hanging on close --- examples/sdl2_emulator.rs | 2 +- src/components/mod.rs | 4 +--- src/components/provided/sdl2.rs | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/sdl2_emulator.rs b/examples/sdl2_emulator.rs index 48f8f29..57aa336 100644 --- a/examples/sdl2_emulator.rs +++ b/examples/sdl2_emulator.rs @@ -392,7 +392,7 @@ impl AudioCallback for MySdlAudio { fn callback(&mut self, out: &mut [Self::Channel]) { if self.audio_spec.format == AudioFormat::S16LSB { - if let Ok(samples) = self.audio_receiver.recv() { + if let Ok(samples) = self.audio_receiver.recv_timeout(Duration::from_millis(500)) { out.copy_from_slice(&samples[..out.len()]); } } diff --git a/src/components/mod.rs b/src/components/mod.rs index d74e63a..0eb51c2 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -10,9 +10,7 @@ use std::io::Read; pub struct RetroComponentBase { retro: LibretroWrapper, libretro_path: PathBuf, - // TODO: control when things get added to this. - // probably shouldn't be after load_game? - // unless we invent a way to play back the latest set_pixel_format etc. metadata required. + // TODO: control when things get added to this with lifetime constraints defined by implementers components: Vec>, // replaying env calls for late-added components diff --git a/src/components/provided/sdl2.rs b/src/components/provided/sdl2.rs index 5b21e05..43ddb85 100644 --- a/src/components/provided/sdl2.rs +++ b/src/components/provided/sdl2.rs @@ -284,7 +284,7 @@ impl AudioCallback for MySdlAudio { fn callback(&mut self, out: &mut [Self::Channel]) { if self.audio_spec.format == AudioFormat::S16LSB { - if let Ok(samples) = self.audio_receiver.recv() { + if let Ok(samples) = self.audio_receiver.recv_timeout(Duration::from_millis(500)) { out.copy_from_slice(&samples[..out.len()]); } }