parallel_handler: formatting cleanup, doc comment typo fixup

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-09-28 09:43:03 +02:00
parent b56c111e93
commit ae3cfa8f0d
1 changed files with 25 additions and 29 deletions

View File

@ -1,16 +1,16 @@
use std::thread::{JoinHandle};
use std::sync::{Arc, Mutex};
use crossbeam_channel::{bounded, Sender};
use anyhow::{bail, format_err, Error};
use std::thread::JoinHandle;
/// A handle to send data toö the worker thread (implements clone)
use anyhow::{bail, format_err, Error};
use crossbeam_channel::{bounded, Sender};
/// A handle to send data to the worker thread (implements clone)
pub struct SendHandle<I> {
input: Sender<I>,
abort: Arc<Mutex<Option<String>>>,
}
impl<I: Send> SendHandle<I> {
/// Returns the first error happened, if any
pub fn check_abort(&self) -> Result<(), Error> {
let guard = self.abort.lock().unwrap();
@ -47,19 +47,17 @@ pub struct ParallelHandler<'a, I> {
impl<I> Clone for SendHandle<I> {
fn clone(&self) -> Self {
Self { input: self.input.clone(), abort: self.abort.clone() }
Self {
input: self.input.clone(),
abort: self.abort.clone(),
}
}
}
impl<'a, I: Send + 'static> ParallelHandler<'a, I> {
/// Create a new thread pool, each thread processing incoming data
/// with 'handler_fn'.
pub fn new<F>(
name: &str,
threads: usize,
handler_fn: F,
) -> Self
pub fn new<F>(name: &str, threads: usize, handler_fn: F) -> Self
where F: Fn(I) -> Result<(), Error> + Send + Clone + 'a,
{
let mut handles = Vec::new();
@ -81,14 +79,13 @@ impl <'a, I: Send + 'static> ParallelHandler<'a, I> {
handles.push(
std::thread::Builder::new()
.name(format!("{} ({})", name, i))
.spawn(move || {
loop {
.spawn(move || loop {
let data = match input_rx.recv() {
Ok(data) => data,
Err(_) => return,
};
match (handler_fn)(data) {
Ok(()) => {},
Ok(()) => (),
Err(err) => {
let mut guard = abort.lock().unwrap();
if guard.is_none() {
@ -96,7 +93,6 @@ impl <'a, I: Send + 'static> ParallelHandler<'a, I> {
}
}
}
}
})
.unwrap()
);