forked from StrafesNET/strafe-client
wip: implement INWorker
This commit is contained in:
parent
8043862c99
commit
d1b491b6e7
@ -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<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
|
||||
fn test_worker() {
|
||||
println!("hiiiii");
|
||||
|
Loading…
Reference in New Issue
Block a user