Skip to content

Commit

Permalink
make parallelisation optional
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Mar 5, 2023
1 parent c21d46a commit 6439e89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
})?;
Expand Down Expand Up @@ -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::<RunResult<Vec<String>>>()?;

scope.spawn(move || {
scope.run(move || {
Self::run_recipe(
context,
recipe,
Expand All @@ -329,7 +329,7 @@ impl<'src> Justfile<'src> {
search,
// ran,
)
});
})?;
}
Ok(())
})?;
Expand All @@ -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();

Expand All @@ -351,7 +351,7 @@ impl<'src> Justfile<'src> {
);
}

scope.spawn(move || {
scope.run(move || {
Self::run_recipe(
context,
recipe,
Expand All @@ -360,7 +360,7 @@ impl<'src> Justfile<'src> {
search,
// &mut ran,
)
});
})?;
}
Ok(())
})?;
Expand Down
13 changes: 10 additions & 3 deletions src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<thread::ScopedJoinHandle<'inner_scope, ScopeResult<'src>>>,
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()
}
}
}

Expand All @@ -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(),
};
Expand Down

0 comments on commit 6439e89

Please sign in to comment.