[tim] Cleanup on timer aisle

This commit is contained in:
2024-03-05 09:33:53 +05:30
parent 3aa51389a3
commit ce7c224470
3 changed files with 44 additions and 35 deletions

View File

@@ -1,10 +1,12 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::sync::mpsc::SendError;
use std::thread::{JoinHandle, sleep}; use std::thread::{JoinHandle, sleep};
use std::time::Duration; use std::time::Duration;
use crate::util::EmulatorResult;
pub struct Timer { pub struct Timer {
timer_left: Arc<Mutex<u16>>, timer_left: Arc<Mutex<u16>>,
join_handle: Option<(JoinHandle<()>,std::sync::mpsc::Sender<()>)> join_handle: Option<(JoinHandle<()>, std::sync::mpsc::Sender<()>)>,
} }
impl Timer { impl Timer {
@@ -30,24 +32,23 @@ impl Timer{
} }
sleep(Duration::from_secs_f32(1f32 / 60f32)); sleep(Duration::from_secs_f32(1f32 / 60f32));
} }
}); });
self.join_handle = Some((res, sender)); self.join_handle = Some((res, sender));
} }
/// Set a timer down tick from `val` /// Set a timer down tick from `val`
pub fn set_timer(& self,val:u16){ pub fn try_set_timer(&self, val: u16) -> EmulatorResult<()> {
let mut timer_val = self.timer_left.lock().expect("Failed to get mutex"); let mut timer_val = self.timer_left.lock()?;
*timer_val = val; *timer_val = val;
Ok(())
} }
pub fn poll_value(&self)->u16{ pub fn poll_value(&self) -> EmulatorResult<u16> {
let res = self.timer_left.lock().expect("Failed to lock?"); let res = self.timer_left.lock()?;
res.clone() Ok(res.clone())
} }
pub fn stop(self) { pub fn stop(self) {
if let Some((u, x)) = self.join_handle { if let Some((u, x)) = self.join_handle {
x.send(()).expect("Failed to send signal to close thread");
u.join().expect("Failed to close thread"); u.join().expect("Failed to close thread");
} else { } else {
log::warn!("Nothing present!"); log::warn!("Nothing present!");
@@ -55,10 +56,16 @@ impl Timer{
} }
pub fn send_stop_signal(&mut self) { pub fn send_stop_signal(&mut self) {
if let Some((_, x)) = &self.join_handle { if let Some((_, x)) = &self.join_handle {
x.send(()).expect("Failed to send signal to close thread"); match x.send(()) {
Ok(_) => {
log::trace!("Sent stop Signal")
}
Err(SendError(_)) => {
log::info!("Thread already stopped!");
}
};
} else { } else {
log::warn!("Nothing present!"); log::warn!("Nothing present!");
} }
} }
} }

View File

@@ -1,6 +1,6 @@
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::sync::{Arc, Mutex, MutexGuard}; use std::sync::{Arc, Mutex};
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@@ -9,8 +9,10 @@ use sdl2::audio::{AudioQueue, AudioSpecDesired};
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::EventPump; use sdl2::EventPump;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::{Color, PixelFormatEnum}; use sdl2::pixels::Color;
use sdl2::render::{BlendMode, TextureAccess, WindowCanvas}; use sdl2::render::BlendMode;
use sdl2::render::WindowCanvas;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use device::timer::Timer; use device::timer::Timer;
use crate::device::Device; use crate::device::Device;
@@ -81,8 +83,7 @@ fn main() -> EmulatorResult<()> {
thread::sleep(Duration::new(0, 1_000_000_000u32 / 60 )); thread::sleep(Duration::new(0, 1_000_000_000u32 / 60 ));
} }
compute_handle.join().expect("Failed to close compute thread");
compute_handle.join().unwrap();
Ok(()) Ok(())
} }

View File

@@ -4,6 +4,7 @@ use sdl2::video::WindowBuildError;
pub type EmulatorResult<T> = Result<T, EmulatorError>; pub type EmulatorResult<T> = Result<T, EmulatorError>;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum EmulatorError { pub enum EmulatorError {
SdlError(String), SdlError(String),