wip: implement INWorker

This commit is contained in:
Quaternions 2023-10-19 17:59:45 -07:00
parent 8043862c99
commit d1b491b6e7

View File

@ -1,6 +1,6 @@
use std::thread; use std::thread;
use std::sync::{mpsc,Arc}; use std::sync::{mpsc,Arc};
use parking_lot::Mutex; use parking_lot::{Mutex,Condvar};
//WorkerPool //WorkerPool
struct Pool(u32); struct Pool(u32);
@ -120,6 +120,37 @@ impl<Task:Send+'static> QNWorker<Task>{
} }
} }
/*
IN = WorkerDescription{
input:Immediate,
output:None(Single),
}
*/
//Inputs are dropped if the worker is busy
pub struct INWorker<Task:Clone>{
input:Arc<(Mutex<Task>,Condvar)>,
}
impl<Task:Clone+Send+'static> INWorker<Task>{
pub fn new<F:FnMut(Task)+Send+'static>(task:Task,mut f:F)->Self{
let ret=Self {
input:Arc::new((Mutex::new(task),Condvar::new())),
};
let input=ret.input.clone();
thread::spawn(move ||{
loop {
input.1.wait(&mut input.0.lock());
f(input.0.lock().clone());
}
});
ret
}
pub fn send(&self,task:Task){
*self.input.0.lock()=task;
self.input.1.notify_one();
}
}
#[test]//How to run this test with printing: cargo test --release -- --nocapture #[test]//How to run this test with printing: cargo test --release -- --nocapture
fn test_worker() { fn test_worker() {
println!("hiiiii"); println!("hiiiii");