Block the emulator thread when idle
This commit is contained in:
		
							parent
							
								
									e8b706df20
								
							
						
					
					
						commit
						99d6970323
					
				| 
						 | 
				
			
			@ -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),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue