diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2023-05-25 00:13:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-25 00:13:04 -0400 |
commit | 69318465ae82281c40b68d3478232e0119dba414 (patch) | |
tree | d36795a792674e13c6b697c720397df8fc0e1de2 /src/lib.rs | |
parent | b6dc17cfb38cd51877ea86002da48700124deec3 (diff) |
other: simplify termination/event loop logic (#1169)
This just simplifies the logic around ctrl-c and termination logic and
event loop logic to something simpler and without the need for timeouts
and/or atomics.
Instead, we just make termination an event sent by ctrl-c and use the
same receiver for event handling to react to it and break the loop.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 11 |
1 files changed, 6 insertions, 5 deletions
@@ -79,6 +79,7 @@ pub enum BottomEvent { PasteEvent(String), Update(Box<data_harvester::Data>), Clean, + Terminate, } #[derive(Debug)] @@ -482,7 +483,7 @@ pub fn create_input_thread( pub fn create_collection_thread( sender: Sender<BottomEvent>, control_receiver: Receiver<ThreadEvent>, - termination_ctrl_lock: Arc<Mutex<bool>>, termination_ctrl_cvar: Arc<Condvar>, + termination_lock: Arc<Mutex<bool>>, termination_cvar: Arc<Condvar>, app_config_fields: &AppConfigFields, filters: DataFilters, used_widget_set: UsedWidgets, ) -> JoinHandle<()> { let temp_type = app_config_fields.temperature_type; @@ -504,7 +505,7 @@ pub fn create_collection_thread( loop { // Check once at the very top... don't block though. - if let Ok(is_terminated) = termination_ctrl_lock.try_lock() { + if let Ok(is_terminated) = termination_lock.try_lock() { if *is_terminated { drop(is_terminated); break; @@ -533,7 +534,7 @@ pub fn create_collection_thread( data_state.update_data(); // Yet another check to bail if needed... do not block! - if let Ok(is_terminated) = termination_ctrl_lock.try_lock() { + if let Ok(is_terminated) = termination_lock.try_lock() { if *is_terminated { drop(is_terminated); break; @@ -547,8 +548,8 @@ pub fn create_collection_thread( } // This is actually used as a "sleep" that can be interrupted by another thread. - if let Ok((is_terminated, _wait_timeout_result)) = termination_ctrl_cvar.wait_timeout( - termination_ctrl_lock.lock().unwrap(), + if let Ok((is_terminated, _)) = termination_cvar.wait_timeout( + termination_lock.lock().unwrap(), Duration::from_millis(update_time), ) { if *is_terminated { |