forked from StrafesNET/strafe-client
true gamer
This commit is contained in:
parent
f14b30138b
commit
2ee70138d1
@ -104,17 +104,15 @@ QN = WorkerDescription{
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//None Output Worker does all its work internally from the perspective of the work submitter
|
//None Output Worker does all its work internally from the perspective of the work submitter
|
||||||
pub struct QNWorker<Task:Send> {
|
pub struct QNWorker<'a,Task:Send>{
|
||||||
sender: mpsc::Sender<Task>,
|
sender: mpsc::Sender<Task>,
|
||||||
|
handle:thread::ScopedJoinHandle<'a,()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Task:Send+'static> QNWorker<Task>{
|
impl<'a,Task:Send+'a> QNWorker<'a,Task>{
|
||||||
pub fn new<F:FnMut(Task)+Send+'static>(mut f:F)->Self{
|
pub fn new<F:FnMut(Task)+Send+'a>(scope:&'a thread::Scope<'a,'_>,mut f:F)->QNWorker<'a,Task>{
|
||||||
let (sender,receiver)=mpsc::channel::<Task>();
|
let (sender,receiver)=mpsc::channel::<Task>();
|
||||||
let ret=Self {
|
let handle=scope.spawn(move ||{
|
||||||
sender,
|
|
||||||
};
|
|
||||||
thread::spawn(move ||{
|
|
||||||
loop {
|
loop {
|
||||||
match receiver.recv() {
|
match receiver.recv() {
|
||||||
Ok(task)=>f(task),
|
Ok(task)=>f(task),
|
||||||
@ -125,7 +123,10 @@ impl<Task:Send+'static> QNWorker<Task>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ret
|
Self{
|
||||||
|
sender,
|
||||||
|
handle,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn send(&self,task:Task)->Result<(),mpsc::SendError<Task>>{
|
pub fn send(&self,task:Task)->Result<(),mpsc::SendError<Task>>{
|
||||||
self.sender.send(task)
|
self.sender.send(task)
|
||||||
@ -139,27 +140,36 @@ IN = WorkerDescription{
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//Inputs are dropped if the worker is busy
|
//Inputs are dropped if the worker is busy
|
||||||
pub struct INWorker<Task:Clone>{
|
pub struct INWorker<'a,Task:Send>{
|
||||||
input:Arc<(Mutex<Task>,Condvar)>,
|
sender: mpsc::SyncSender<Task>,
|
||||||
|
handle:thread::ScopedJoinHandle<'a,()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Task:Clone+Send+'static> INWorker<Task>{
|
impl<'a,Task:Send+'a> INWorker<'a,Task>{
|
||||||
pub fn new<F:FnMut(Task)+Send+'static>(task:Task,mut f:F)->Self{
|
pub fn new<F:FnMut(Task)+Send+'a>(scope:&'a thread::Scope<'a,'_>,mut f:F)->INWorker<'a,Task>{
|
||||||
let ret=Self {
|
let (sender,receiver)=mpsc::sync_channel::<Task>(1);
|
||||||
input:Arc::new((Mutex::new(task),Condvar::new())),
|
let handle=scope.spawn(move ||{
|
||||||
};
|
|
||||||
let input=ret.input.clone();
|
|
||||||
thread::spawn(move ||{
|
|
||||||
loop {
|
loop {
|
||||||
input.1.wait(&mut input.0.lock());
|
match receiver.recv() {
|
||||||
f(input.0.lock().clone());
|
Ok(task)=>f(task),
|
||||||
|
Err(_)=>{
|
||||||
|
println!("Worker stopping.",);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ret
|
Self{
|
||||||
|
sender,
|
||||||
|
handle,
|
||||||
}
|
}
|
||||||
pub fn send(&self,task:Task){
|
}
|
||||||
*self.input.0.lock()=task;
|
//blocking!
|
||||||
self.input.1.notify_one();
|
pub fn blocking_send(&self,task:Task)->Result<(), mpsc::SendError<Task>>{
|
||||||
|
self.sender.send(task)
|
||||||
|
}
|
||||||
|
pub fn send(&self,task:Task)->Result<(), mpsc::TrySendError<Task>>{
|
||||||
|
self.sender.try_send(task)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user