Skip to content

Commit 09c77c4

Browse files
committed
Merge pull request #45 from davvid/parallel
* davvid/parallel: cmd: allow "-j" / "--jobs" to take zero arguments cmd: teach garden to run commands in parallel cargo deny: add commentary detailing why MPL-2.0 is present
2 parents 1a0f9ac + 47990d6 commit 09c77c4

File tree

10 files changed

+431
-142
lines changed

10 files changed

+431
-142
lines changed

deny.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ allow = [
33
# encoding_rs
44
"BSD-3-Clause",
55
"MIT",
6+
# dirs -> dirs-sys -> options-ext
67
"MPL-2.0",
78
# clap -> clap_derive -> proc-macro2 -> unicode-ident
89
"Unicode-DFS-2016"

doc/src/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
**Features**:
66

7+
- `garden cmd` and custom commands now have a `--jobs | -j` option that enables
8+
running multiple in parallel. Specifying `--jobs=0` will detect and set the
9+
concurrency level to use all available cores.
10+
([#43](https://github.com/garden-rs/garden/issues/43))
11+
([#45](https://github.com/garden-rs/garden/pull/45))
12+
713
- `garden ls` now has a `--reverse | -r` option to display trees in reverse order.
814
([#44](https://github.com/garden-rs/garden/pull/44))
915

src/cmd.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ where
166166
}
167167

168168
// Create an Exec object.
169-
let mut exec = exec_in_dir(&command_vec, path);
169+
let mut exec = exec_in_dir(&command_vec, &path);
170170

171171
// Update the command environment
172172
for (name, value) in &env {
@@ -315,3 +315,34 @@ pub(crate) fn shell_quote(arg: &str) -> String {
315315
.map(|quoted_arg| quoted_arg.to_string())
316316
.unwrap_or_else(|_| arg.to_string())
317317
}
318+
319+
/// Get the default number of jobs to run in parallel
320+
pub(crate) fn default_num_jobs() -> usize {
321+
match std::thread::available_parallelism() {
322+
Ok(value) => std::cmp::max(value.get(), 3), // "prune" requires at minimum three threads.
323+
Err(_) => 4,
324+
}
325+
}
326+
327+
/// Initialize the global thread pool.
328+
pub(crate) fn initialize_threads(num_jobs: usize) -> anyhow::Result<()> {
329+
let num_jobs = if num_jobs == 0 {
330+
default_num_jobs()
331+
} else {
332+
num_jobs
333+
};
334+
rayon::ThreadPoolBuilder::new()
335+
.num_threads(num_jobs)
336+
.build_global()?;
337+
338+
Ok(())
339+
}
340+
341+
/// Initialize the global thread pool when the num_jobs option is provided.
342+
pub(crate) fn initialize_threads_option(num_jobs: Option<usize>) -> anyhow::Result<()> {
343+
if let Some(num_jobs) = num_jobs {
344+
initialize_threads(num_jobs)?;
345+
}
346+
347+
Ok(())
348+
}

0 commit comments

Comments
 (0)