From 94cb5a542d6bea5f7b4d435d3f345b4788897435 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Sun, 10 Nov 2024 16:08:49 -0500 Subject: [PATCH] Block the emulator thread when idle --- src/emulator.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/emulator.rs b/src/emulator.rs index 6e113a8..38fdab6 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -3,7 +3,7 @@ use std::{ path::{Path, PathBuf}, sync::{ atomic::{AtomicBool, Ordering}, - mpsc::{self, TryRecvError}, + mpsc::{self, RecvError, TryRecvError}, Arc, }, }; @@ -94,19 +94,33 @@ impl Emulator { let mut eye_contents = vec![0u8; 384 * 224 * 2]; let mut audio_samples = vec![]; loop { + let mut idle = true; if self.running.load(Ordering::Acquire) { + idle = false; self.sim.emulate_frame(); } if let Some(renderer) = &mut self.renderer { if self.sim.read_pixels(&mut eye_contents) { + idle = false; renderer.render(&eye_contents); } } self.sim.read_samples(&mut audio_samples); if !audio_samples.is_empty() { + idle = false; self.audio.update(&audio_samples); audio_samples.clear(); } + if idle { + // The game is paused, and we have output all the video/audio we have. + // Block the thread until a new command comes in. + match self.commands.recv() { + Ok(command) => self.handle_command(command), + Err(RecvError) => { + return; + } + } + } loop { match self.commands.try_recv() { Ok(command) => self.handle_command(command),