From ce7c224470b7b390d1c0e277f76c207543341aec Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Tue, 5 Mar 2024 09:33:53 +0530 Subject: [PATCH] [tim] Cleanup on timer aisle --- src/device/timer.rs | 65 +++++++++++++++++++++++++-------------------- src/main.rs | 11 ++++---- src/util.rs | 3 ++- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/device/timer.rs b/src/device/timer.rs index 83c77e7..b51e95c 100644 --- a/src/device/timer.rs +++ b/src/device/timer.rs @@ -1,64 +1,71 @@ use std::sync::{Arc, Mutex}; +use std::sync::mpsc::SendError; use std::thread::{JoinHandle, sleep}; use std::time::Duration; +use crate::util::EmulatorResult; -pub struct Timer{ +pub struct Timer { timer_left: Arc>, - join_handle: Option<(JoinHandle<()>,std::sync::mpsc::Sender<()>)> + join_handle: Option<(JoinHandle<()>, std::sync::mpsc::Sender<()>)>, } -impl Timer{ - pub fn new()->Timer{ - Timer{timer_left:Arc::new(Mutex::default()),join_handle:None} +impl Timer { + pub fn new() -> Timer { + Timer { timer_left: Arc::new(Mutex::default()), join_handle: None } } - pub fn start(&mut self){ + pub fn start(&mut self) { let timer_left_ref = self.timer_left.clone(); - let (sender,receiver) = std::sync::mpsc::channel(); - let res = std::thread::spawn(move ||{ - loop{ + let (sender, receiver) = std::sync::mpsc::channel(); + let res = std::thread::spawn(move || { + loop { let val = receiver.try_recv(); - if let Ok(()) = val{ + if let Ok(()) = val { break; - }else if let Err(std::sync::mpsc::TryRecvError::Disconnected) = val{ + } else if let Err(std::sync::mpsc::TryRecvError::Disconnected) = val { panic!("Disconnected"); } { let mut timer_lock = timer_left_ref.lock().expect("Failed to lock"); - if *timer_lock >0 { + if *timer_lock > 0 { *timer_lock -= 1; } } - 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` - pub fn set_timer(& self,val:u16){ - let mut timer_val = self.timer_left.lock().expect("Failed to get mutex"); + pub fn try_set_timer(&self, val: u16) -> EmulatorResult<()> { + let mut timer_val = self.timer_left.lock()?; *timer_val = val; + Ok(()) } - pub fn poll_value(&self)->u16{ - let res = self.timer_left.lock().expect("Failed to lock?"); - res.clone() + pub fn poll_value(&self) -> EmulatorResult { + let res = self.timer_left.lock()?; + Ok(res.clone()) } - pub fn stop(self){ - if let Some((u,x)) = self.join_handle{ - x.send(()).expect("Failed to send signal to close thread"); + pub fn stop(self) { + if let Some((u, x)) = self.join_handle { u.join().expect("Failed to close thread"); - }else{ + } else { log::warn!("Nothing present!"); } } - pub fn send_stop_signal(&mut self){ - if let Some((_,x)) = &self.join_handle{ - x.send(()).expect("Failed to send signal to close thread"); - }else{ + pub fn send_stop_signal(&mut self) { + if let Some((_, x)) = &self.join_handle { + match x.send(()) { + Ok(_) => { + log::trace!("Sent stop Signal") + } + Err(SendError(_)) => { + log::info!("Thread already stopped!"); + } + }; + } else { log::warn!("Nothing present!"); } } - } diff --git a/src/main.rs b/src/main.rs index e8089b3..7e101a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::io::Read; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::sync::{Arc, Mutex}; use std::sync::mpsc::Receiver; use std::thread; use std::time::Duration; @@ -9,8 +9,10 @@ use sdl2::audio::{AudioQueue, AudioSpecDesired}; use sdl2::event::Event; use sdl2::EventPump; use sdl2::keyboard::Keycode; -use sdl2::pixels::{Color, PixelFormatEnum}; -use sdl2::render::{BlendMode, TextureAccess, WindowCanvas}; +use sdl2::pixels::Color; +use sdl2::render::BlendMode; + +use sdl2::render::WindowCanvas; use simple_logger::SimpleLogger; use device::timer::Timer; use crate::device::Device; @@ -81,8 +83,7 @@ fn main() -> EmulatorResult<()> { thread::sleep(Duration::new(0, 1_000_000_000u32 / 60 )); } - - compute_handle.join().unwrap(); + compute_handle.join().expect("Failed to close compute thread"); Ok(()) } diff --git a/src/util.rs b/src/util.rs index 6b5ca33..227aa8e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,6 +4,7 @@ use sdl2::video::WindowBuildError; pub type EmulatorResult = Result; + #[derive(Clone, Debug)] pub enum EmulatorError { SdlError(String), @@ -41,4 +42,4 @@ impl From> for EmulatorError{ fn from(value: PoisonError) -> Self { Self::MutexInvalidState(value.to_string()) } -} \ No newline at end of file +}