-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wyhaya
committed
Sep 9, 2022
1 parent
84658e8
commit 0adacc2
Showing
6 changed files
with
100 additions
and
84 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::util::num_cpus; | ||
use crossbeam_deque::{Stealer, Worker}; | ||
use std::any::Any; | ||
use std::thread::{self, JoinHandle}; | ||
|
||
#[derive(Debug)] | ||
pub struct Tasks<T, R> { | ||
worker: Worker<Work<T>>, | ||
threads: Vec<JoinHandle<Vec<R>>>, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Work<T> { | ||
Exit, | ||
Task(T), | ||
} | ||
|
||
impl<T, R> Tasks<T, R> | ||
where | ||
T: FnOnce() -> Option<R>, | ||
T: Send + 'static, | ||
R: Send + 'static, | ||
{ | ||
pub fn new() -> Self { | ||
let cpus = num_cpus(); | ||
let worker = Worker::new_fifo(); | ||
let mut threads = Vec::with_capacity(cpus); | ||
|
||
for _ in 0..cpus { | ||
let stealer: Stealer<Work<T>> = worker.stealer().clone(); | ||
threads.push(thread::spawn(move || { | ||
let mut result = Vec::new(); | ||
loop { | ||
let work = match stealer.steal().success() { | ||
Some(work) => work, | ||
None => continue, | ||
}; | ||
|
||
match work { | ||
Work::Task(task) => match task() { | ||
Some(data) => result.push(data), | ||
None => continue, | ||
}, | ||
Work::Exit => break, | ||
} | ||
} | ||
result | ||
})); | ||
} | ||
|
||
Self { worker, threads } | ||
} | ||
|
||
pub fn push(&self, f: T) { | ||
self.worker.push(Work::Task(f)); | ||
} | ||
|
||
pub fn result(self) -> Vec<Result<Vec<R>, Box<dyn Any + Send + 'static>>> { | ||
let mut rst = Vec::with_capacity(self.threads.len()); | ||
for _ in 0..self.threads.len() { | ||
self.worker.push(Work::Exit); | ||
} | ||
for thread in self.threads { | ||
rst.push(thread.join()); | ||
} | ||
rst | ||
} | ||
} |