other: try using flume for channel (#884)

This commit is contained in:
Clement Tsang 2022-11-10 01:08:06 -05:00 committed by GitHub
parent 7e6e098e2b
commit f5ec9191f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 137 additions and 16 deletions

123
Cargo.lock generated
View File

@ -219,6 +219,7 @@ dependencies = [
"ctrlc",
"dirs",
"fern",
"flume",
"futures",
"futures-timer",
"fxhash",
@ -261,6 +262,12 @@ dependencies = [
"regex-automata",
]
[[package]]
name = "bumpalo"
version = "3.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
[[package]]
name = "byteorder"
version = "1.4.3"
@ -575,6 +582,19 @@ dependencies = [
"num-traits",
]
[[package]]
name = "flume"
version = "0.10.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577"
dependencies = [
"futures-core",
"futures-sink",
"nanorand",
"pin-project",
"spin",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -707,8 +727,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
dependencies = [
"cfg-if",
"js-sys",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
"wasm-bindgen",
]
[[package]]
@ -907,6 +929,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "js-sys"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3fac17f7123a73ca62df411b1bf727ccc805daa070338fda671c86dac1bdc27"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kstring"
version = "2.0.0"
@ -1023,6 +1054,15 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "nanorand"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3"
dependencies = [
"getrandom",
]
[[package]]
name = "nix"
version = "0.19.1"
@ -1205,6 +1245,26 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "pin-project"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.2.9"
@ -1492,6 +1552,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "spin"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6002a767bff9e83f8eeecf883ecb8011875a21ae8da43bffb817a57e78cc09"
dependencies = [
"lock_api",
]
[[package]]
name = "starship-battery"
version = "0.7.9"
@ -1744,6 +1813,60 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be"
[[package]]
name = "wepoll-ffi"
version = "0.1.2"

View File

@ -69,6 +69,7 @@ crossterm = "0.25.0"
ctrlc = { version = "3.1.9", features = ["termination"] }
dirs = "4.0.0"
fern = { version = "0.6.1", optional = true }
flume = "0.10.14"
futures = "0.3.21"
futures-timer = "3.0.2"
fxhash = "0.2.1"

View File

@ -18,7 +18,7 @@ use std::{
panic,
sync::{
atomic::{AtomicBool, Ordering},
mpsc, Arc, Condvar, Mutex,
Arc, Condvar, Mutex,
},
thread,
time::Duration,
@ -78,7 +78,7 @@ fn main() -> Result<()> {
let thread_termination_cvar = Arc::new(Condvar::new());
// Set up input handling
let (sender, receiver) = mpsc::channel();
let (sender, receiver) = flume::unbounded();
let _input_thread = create_input_thread(sender.clone(), thread_termination_lock.clone());
// Cleaning loop
@ -107,7 +107,7 @@ fn main() -> Result<()> {
};
// Event loop
let (collection_thread_ctrl_sender, collection_thread_ctrl_receiver) = mpsc::channel();
let (collection_thread_ctrl_sender, collection_thread_ctrl_receiver) = flume::unbounded();
let _collection_thread = create_collection_thread(
sender,
collection_thread_ctrl_receiver,
@ -144,6 +144,7 @@ fn main() -> Result<()> {
let mut first_run = true;
while !is_terminated.load(Ordering::SeqCst) {
// TODO: Would be good to instead use a mix of is_terminated check + recv. Probably use a termination event instead.
if let Ok(recv) = receiver.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
match recv {
BottomEvent::KeyInput(event) => {

View File

@ -19,9 +19,8 @@ use std::{
io::{stderr, stdout, Write},
panic::PanicInfo,
path::PathBuf,
sync::Arc,
sync::Condvar,
sync::Mutex,
sync::{Arc, Condvar},
thread::{self, JoinHandle},
time::{Duration, Instant},
};
@ -36,6 +35,8 @@ use crossterm::{
terminal::{disable_raw_mode, LeaveAlternateScreen},
};
use flume::{Receiver, Sender};
use app::{
data_harvester,
frozen_state::FrozenState,
@ -106,7 +107,7 @@ pub fn handle_mouse_event(event: MouseEvent, app: &mut App) {
}
pub fn handle_key_event_or_break(
event: KeyEvent, app: &mut App, reset_sender: &std::sync::mpsc::Sender<ThreadControlEvent>,
event: KeyEvent, app: &mut App, reset_sender: &Sender<ThreadControlEvent>,
) -> bool {
// debug!("KeyEvent: {:?}", event);
@ -411,9 +412,7 @@ pub fn update_data(app: &mut App) {
}
pub fn create_input_thread(
sender: std::sync::mpsc::Sender<
BottomEvent<crossterm::event::KeyEvent, crossterm::event::MouseEvent>,
>,
sender: Sender<BottomEvent<crossterm::event::KeyEvent, crossterm::event::MouseEvent>>,
termination_ctrl_lock: Arc<Mutex<bool>>,
) -> JoinHandle<()> {
thread::spawn(move || {
@ -466,13 +465,10 @@ pub fn create_input_thread(
}
pub fn create_collection_thread(
sender: std::sync::mpsc::Sender<
BottomEvent<crossterm::event::KeyEvent, crossterm::event::MouseEvent>,
>,
control_receiver: std::sync::mpsc::Receiver<ThreadControlEvent>,
termination_ctrl_lock: Arc<Mutex<bool>>, termination_ctrl_cvar: Arc<Condvar>,
app_config_fields: &app::AppConfigFields, filters: app::DataFilters,
used_widget_set: UsedWidgets,
sender: Sender<BottomEvent<crossterm::event::KeyEvent, crossterm::event::MouseEvent>>,
control_receiver: Receiver<ThreadControlEvent>, termination_ctrl_lock: Arc<Mutex<bool>>,
termination_ctrl_cvar: Arc<Condvar>, app_config_fields: &app::AppConfigFields,
filters: app::DataFilters, used_widget_set: UsedWidgets,
) -> JoinHandle<()> {
let temp_type = app_config_fields.temperature_type;
let use_current_cpu_total = app_config_fields.use_current_cpu_total;