Use AtomicBool instead of Mutex

This commit is contained in:
Dheepak Krishnamurthy 2021-02-11 02:06:15 -07:00
parent 65b960ecc7
commit b7bc5252dc

View file

@ -7,7 +7,8 @@ use crossterm::{
use tui::{backend::CrosstermBackend, Terminal}; use tui::{backend::CrosstermBackend, Terminal};
use std::io::{self, Write}; use std::io::{self, Write};
use std::sync::{Arc, Mutex}; use std::sync::{Arc};
use std::sync::atomic::{AtomicBool, Ordering};
use std::{sync::mpsc, thread, time::Duration}; use std::{sync::mpsc, thread, time::Duration};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -61,8 +62,8 @@ pub fn destruct_terminal() {
pub struct Events { pub struct Events {
pub rx: mpsc::Receiver<Event<Key>>, pub rx: mpsc::Receiver<Event<Key>>,
pub tx: mpsc::Sender<Event<Key>>, pub tx: mpsc::Sender<Event<Key>>,
pub pause_stdin: Arc<Mutex<bool>>, pub pause_stdin: Arc<AtomicBool>,
pub pause_ticker: Arc<Mutex<bool>>, pub pause_ticker: Arc<AtomicBool>,
} }
impl Events { impl Events {
@ -70,14 +71,14 @@ impl Events {
pub fn with_config(config: EventConfig) -> Events { pub fn with_config(config: EventConfig) -> Events {
use crossterm::event::{KeyCode::*, KeyModifiers}; use crossterm::event::{KeyCode::*, KeyModifiers};
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let pause_stdin = Arc::new(Mutex::new(false)); let pause_stdin = Arc::new(AtomicBool::new(false));
let pause_ticker = Arc::new(Mutex::new(false)); let pause_ticker = Arc::new(AtomicBool::new(false));
let _input_handle = { let _input_handle = {
let tx = tx.clone(); let tx = tx.clone();
let pause_stdin = pause_stdin.clone(); let pause_stdin = pause_stdin.clone();
thread::spawn(move || { thread::spawn(move || {
loop { loop {
if *pause_stdin.lock().unwrap() { if pause_stdin.load(Ordering::Relaxed) {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
continue; continue;
} }
@ -119,7 +120,7 @@ impl Events {
thread::spawn(move || loop { thread::spawn(move || loop {
// print!("\r\n"); // print!("\r\n");
// dbg!(*pause_ticker.lock().unwrap()); // dbg!(*pause_ticker.lock().unwrap());
while *pause_ticker.lock().unwrap() { while pause_ticker.load(Ordering::Relaxed) {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
} }
if tx.send(Event::Tick).is_err() { if tx.send(Event::Tick).is_err() {
@ -144,21 +145,19 @@ impl Events {
} }
pub fn pause_ticker(&self) { pub fn pause_ticker(&self) {
*self.pause_ticker.lock().unwrap() = true; self.pause_ticker.swap(true, Ordering::Relaxed);
// print!("\r\n");
// dbg!(*self.pause_ticker.lock().unwrap());
} }
pub fn resume_ticker(&self) { pub fn resume_ticker(&self) {
*self.pause_ticker.lock().unwrap() = false; self.pause_ticker.swap(false, Ordering::Relaxed);
} }
pub fn pause_event_loop(&self) { pub fn pause_event_loop(&self) {
*self.pause_stdin.lock().unwrap() = true; self.pause_stdin.swap(true, Ordering::Relaxed);
} }
pub fn resume_event_loop(&self) { pub fn resume_event_loop(&self) {
*self.pause_stdin.lock().unwrap() = false; self.pause_stdin.swap(false, Ordering::Relaxed);
} }
pub fn pause_key_capture(&self, terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) { pub fn pause_key_capture(&self, terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) {