From 6439e899ba438e9b16ee99d754b02ef720bdae96 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sun, 5 Mar 2023 17:55:12 +0100 Subject: [PATCH] make parallelisation optional --- src/justfile.rs | 18 +++++++++--------- src/parallel.rs | 13 ++++++++++--- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/justfile.rs b/src/justfile.rs index c698f4704d..501134821e 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -255,13 +255,13 @@ impl<'src> Justfile<'src> { }; // let mut ran = BTreeSet::new(); - parallel::task_scope(|scope| { + parallel::task_scope(config.parallel, |scope| { for (recipe, arguments) in grouped { - scope.spawn(|| { + scope.run(|| { Self::run_recipe( &context, recipe, arguments, &dotenv, search, /*&mut ran*/ ) - }); + })?; } Ok(()) })?; @@ -313,14 +313,14 @@ impl<'src> Justfile<'src> { let mut evaluator = Evaluator::recipe_evaluator(context.config, dotenv, &scope, context.settings, search); - parallel::task_scope(|scope| { + parallel::task_scope(context.config.parallel, |scope| { for Dependency { recipe, arguments } in recipe.dependencies.iter().take(recipe.priors) { let arguments = arguments .iter() .map(|argument| evaluator.evaluate_expression(argument)) .collect::>>()?; - scope.spawn(move || { + scope.run(move || { Self::run_recipe( context, recipe, @@ -329,7 +329,7 @@ impl<'src> Justfile<'src> { search, // ran, ) - }); + })?; } Ok(()) })?; @@ -339,7 +339,7 @@ impl<'src> Justfile<'src> { { // let mut ran = BTreeSet::new(); - parallel::task_scope(|scope| { + parallel::task_scope(context.config.parallel, |scope| { for Dependency { recipe, arguments } in recipe.dependencies.iter().skip(recipe.priors) { let mut evaluated = Vec::new(); @@ -351,7 +351,7 @@ impl<'src> Justfile<'src> { ); } - scope.spawn(move || { + scope.run(move || { Self::run_recipe( context, recipe, @@ -360,7 +360,7 @@ impl<'src> Justfile<'src> { search, // &mut ran, ) - }); + })?; } Ok(()) })?; diff --git a/src/parallel.rs b/src/parallel.rs index fffd612849..d38990e8c7 100644 --- a/src/parallel.rs +++ b/src/parallel.rs @@ -6,16 +6,22 @@ type ScopeResult<'src> = RunResult<'src, ()>; pub(crate) struct TaskScope<'env, 'src, 'inner_scope> { inner: &'inner_scope thread::Scope<'env>, join_handles: Vec>>, + parallel: bool, } impl<'env, 'src, 'inner_scope> TaskScope<'env, 'src, 'inner_scope> { - pub(crate) fn spawn<'scope, F>(&'scope mut self, f: F) + pub(crate) fn run<'scope, F>(&'scope mut self, f: F) -> ScopeResult<'src> where 'src: 'env, F: FnOnce() -> ScopeResult<'src>, F: Send + 'env, { - self.join_handles.push(self.inner.spawn(|_scope| f())); + if self.parallel { + self.join_handles.push(self.inner.spawn(|_scope| f())); + Ok(()) + } else { + f() + } } } @@ -25,12 +31,13 @@ impl<'env, 'src, 'inner_scope> TaskScope<'env, 'src, 'inner_scope> { /// run. The first error will be returned as result of this `task_scope`. /// /// Only works for tasks with an `RunResult<'src, ()>` result type. -pub(crate) fn task_scope<'env, 'src, F>(f: F) -> ScopeResult<'src> +pub(crate) fn task_scope<'env, 'src, F>(parallel: bool, f: F) -> ScopeResult<'src> where F: for<'inner_scope> FnOnce(&mut TaskScope<'env, 'src, 'inner_scope>) -> ScopeResult<'src>, { thread::scope(|scope| { let mut task_scope = TaskScope { + parallel, inner: scope, join_handles: Vec::new(), };