Make hook execution parallel for StoreIdAccessor impl for Aspect

This commit is contained in:
Matthias Beyer 2016-03-04 21:32:51 +01:00
parent d8760c9737
commit 7102a57808
2 changed files with 31 additions and 7 deletions

View file

@ -32,18 +32,41 @@ impl Aspect {
impl StoreIdAccessor for Aspect {
fn access(&self, id: &StoreId) -> HookResult<()> {
use crossbeam;
use std::thread;
use std::thread::JoinHandle;
use hook::error::HookError as HE;
use hook::error::HookErrorKind as HEK;
let accessors : Vec<HDA> = self.hooks.iter().map(|h| h.accessor()).collect();
if !accessors.iter().all(|a| match a { &HDA::StoreIdAccess(_) => true, _ => false }) {
unimplemented!()
}
for accessor in accessors {
let threads : Vec<HookResult<()>> = accessors
.iter()
.map(|accessor| {
crossbeam::scope(|scope| {
scope.spawn(|| {
match accessor {
HDA::StoreIdAccess(accessor) => try!(accessor.access(id)),
&HDA::StoreIdAccess(accessor) => accessor.access(id),
_ => unreachable!(),
}
}
Ok(())
.map_err(|e| ()) // TODO: We're losing the error cause here
})
})
})
.map(|i| i.join().map_err(|_| HE::new(HEK::HookExecutionError, None)))
.collect();
threads
.into_iter()
.fold(Ok(()), |acc, elem| {
acc.and_then(|a| {
elem.map(|_| a).map_err(|_| HE::new(HEK::HookExecutionError, None))
})
})
}
}

View file

@ -9,6 +9,7 @@ use std::convert::Into;
*/
#[derive(Clone, Copy, Debug)]
pub enum HookErrorKind {
HookExecutionError,
Pre(PreHookErrorKind),
Post(PostHookErrorKind)
}