HexoDSP/src/nodes/drop_thread.rs
2021-08-04 03:58:43 +02:00

50 lines
1.4 KiB
Rust

// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.
use super::DropMsg;
use ringbuf::Consumer;
/// For receiving deleted/overwritten nodes from the backend
/// thread and dropping them.
pub(crate) struct DropThread {
terminate: std::sync::Arc<std::sync::atomic::AtomicBool>,
th: Option<std::thread::JoinHandle<()>>,
}
impl DropThread {
pub(crate) fn new(mut graph_drop_con: Consumer<DropMsg>) -> Self {
let terminate =
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let th_terminate = terminate.clone();
let th = std::thread::spawn(move || {
loop {
if th_terminate.load(std::sync::atomic::Ordering::Relaxed) {
return;
}
while let Some(_node) = graph_drop_con.pop() {
// drop it ...
//d// println!("Dropped some shit...");
}
std::thread::sleep(std::time::Duration::from_millis(250));
}
});
Self {
th: Some(th),
terminate,
}
}
}
impl Drop for DropThread {
fn drop(&mut self) {
self.terminate.store(true, std::sync::atomic::Ordering::Relaxed);
let _ = self.th.take().unwrap().join();
}
}