From d1b491b6e7be43b271c108352deef6c587633886 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 19 Oct 2023 17:59:45 -0700 Subject: [PATCH] wip: implement INWorker --- src/worker.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/worker.rs b/src/worker.rs index de4d4c1..1383981 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -1,6 +1,6 @@ use std::thread; use std::sync::{mpsc,Arc}; -use parking_lot::Mutex; +use parking_lot::{Mutex,Condvar}; //WorkerPool struct Pool(u32); @@ -120,6 +120,37 @@ impl QNWorker{ } } +/* +IN = WorkerDescription{ + input:Immediate, + output:None(Single), +} +*/ +//Inputs are dropped if the worker is busy +pub struct INWorker{ + input:Arc<(Mutex,Condvar)>, +} + +impl INWorker{ + pub fn new(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 fn test_worker() { println!("hiiiii");