Skip to main content

parallel_task/
worker_thread.rs

1//! WorkerThreads follow a Push based strategy wherein Threads are managed by a WorkerController that
2//! spawns WorkerThreads. These worker threads can be communicated with via sync and async channels to 
3//! send data for processing and to close the same
4
5use crate::{collector::Collector, errors::WorkThreadError, for_each::ParallelForEach, iterators::iterator::AtomicIterator, map::ParallelMap, push_workers::{priorisation::ThreadPrioritization, worker_controller::WorkerController}};
6pub struct WorkerThreads {pub nthreads:usize }
7
8#[allow(dead_code)]
9impl WorkerThreads
10{
11    pub fn collect<I,F,T,V,C>(self, task:ParallelMap<V,F,T,I>) -> C
12    where I:AtomicIterator<AtomicItem = V> + Send + Sized,
13    F: Fn(V) -> T + Send + Sync,
14    V: Send + Sync,
15    T:Send + Sync,
16    C: Collector<T> {          
17        let fnc = task.f;       
18        let q = task.iter.iter;        
19        match WorkerController::new(fnc,q, ThreadPrioritization::Remaining)
20        .run::<C>() {
21            Ok(res) => { res }
22            Err(e) => {
23                if let WorkThreadError::ThreadAdd(e) = e {
24                    panic!("Error: {}",e);
25                } else {
26                    panic!("Unknown error occurred in worker controller");
27                }
28            }
29        }               
30    }  
31
32    pub fn run<I,F,V>(self, task:ParallelForEach<V,F,I>)
33    where I:AtomicIterator<AtomicItem = V> + Send + Sized,
34    F: Fn(V) + Send + Sync,
35    V: Send + Sync,    
36    {
37        let fnc = task.f;       
38        let q = task.iter.iter;            
39
40        if let Err(e) = WorkerController::new(fnc,q,ThreadPrioritization::Remaining)
41         .run::<Vec<_>>() {            
42            if let WorkThreadError::ThreadAdd(e) = e {
43                panic!("Error: {}",e);
44            } else {
45                panic!("Unknown error occurred in worker controller");
46            }        
47        };             
48    }    
49}
50
51