From b1300527c3b002f33a0818122dde9e77619f02f3 Mon Sep 17 00:00:00 2001 From: Fabian Lippold Date: Thu, 16 Nov 2023 22:51:14 +0100 Subject: [PATCH] use AtomicBool --- src/tui/mod.rs | 8 ++++---- src/tui/tabs.rs | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/tui/mod.rs b/src/tui/mod.rs index b2e9c6b..aa7e70d 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -7,7 +7,7 @@ mod status; mod tabs; use std::{ - sync::{mpsc, Arc, Mutex, RwLock}, + sync::{atomic::AtomicBool, mpsc, Arc, Mutex, RwLock}, time::Duration, }; @@ -62,7 +62,7 @@ pub fn tui<'a>( enable_raw_mode()?; terminal.clear()?; - let running = Mutex::new(true); + let running = Arc::new(AtomicBool::new(true)); let mut tabs = Tabs::new( vec![ ( @@ -79,7 +79,7 @@ pub fn tui<'a>( ), ("Fancy stuff ✨ ", Box::new(Fancy::new(player.clone()))), ], - &running, + running.clone(), ); let usage = Status::new(player.clone()); @@ -99,7 +99,7 @@ pub fn tui<'a>( tabs.input(&event::read()?)?; } - if !*running.lock().unwrap() { + if !running.load(std::sync::atomic::Ordering::Relaxed) { break; } } diff --git a/src/tui/tabs.rs b/src/tui/tabs.rs index a2b6531..2aa0679 100644 --- a/src/tui/tabs.rs +++ b/src/tui/tabs.rs @@ -1,4 +1,4 @@ -use std::sync::Mutex; +use std::sync::{atomic::AtomicBool, Arc}; use crossterm::event::{Event, KeyCode, KeyEvent}; use log::trace; @@ -15,11 +15,11 @@ use super::Tui; pub struct Tabs<'a> { pub selected: usize, pub tabs: Vec<(&'static str, Box)>, - running: &'a Mutex, + running: Arc, } impl<'a> Tabs<'a> { - pub fn new(tabs: Vec<(&'static str, Box)>, running: &'a Mutex) -> Self { + pub fn new(tabs: Vec<(&'static str, Box)>, running: Arc) -> Self { Self { selected: 0, tabs, @@ -86,8 +86,8 @@ impl Tui for Tabs<'_> { self.selected = (self.selected.wrapping_sub(1)) % self.tabs.len(); } KeyCode::Char('q') => { - trace!("locking player"); - *self.running.lock().unwrap() = false; + self.running + .store(false, std::sync::atomic::Ordering::Relaxed); } _ => { let content = self.tabs.get_mut(self.selected).expect("Tab not found");