From 7102a578087678769c0f94ebd14cb77ad1e11e72 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 4 Mar 2016 21:32:51 +0100 Subject: [PATCH] Make hook execution parallel for StoreIdAccessor impl for Aspect --- libimagstore/src/hook/aspect.rs | 37 ++++++++++++++++++++++++++------- libimagstore/src/hook/error.rs | 1 + 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/libimagstore/src/hook/aspect.rs b/libimagstore/src/hook/aspect.rs index 8d831de7..276e1f53 100644 --- a/libimagstore/src/hook/aspect.rs +++ b/libimagstore/src/hook/aspect.rs @@ -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 = self.hooks.iter().map(|h| h.accessor()).collect(); if !accessors.iter().all(|a| match a { &HDA::StoreIdAccess(_) => true, _ => false }) { unimplemented!() } - for accessor in accessors { - match accessor { - HDA::StoreIdAccess(accessor) => try!(accessor.access(id)), - _ => unreachable!(), - } - } - Ok(()) + let threads : Vec> = accessors + .iter() + .map(|accessor| { + crossbeam::scope(|scope| { + scope.spawn(|| { + match accessor { + &HDA::StoreIdAccess(accessor) => accessor.access(id), + _ => unreachable!(), + } + .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)) + }) + }) } } diff --git a/libimagstore/src/hook/error.rs b/libimagstore/src/hook/error.rs index daa8af23..fd0683aa 100644 --- a/libimagstore/src/hook/error.rs +++ b/libimagstore/src/hook/error.rs @@ -9,6 +9,7 @@ use std::convert::Into; */ #[derive(Clone, Copy, Debug)] pub enum HookErrorKind { + HookExecutionError, Pre(PreHookErrorKind), Post(PostHookErrorKind) }