Skip to content

Commit

Permalink
Merge pull request #45 from davvid/parallel
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
davvid committed Sep 27, 2024
2 parents 1a0f9ac + 47990d6 commit 09c77c4
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 142 deletions.
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ allow = [
# encoding_rs
"BSD-3-Clause",
"MIT",
# dirs -> dirs-sys -> options-ext
"MPL-2.0",
# clap -> clap_derive -> proc-macro2 -> unicode-ident
"Unicode-DFS-2016"
Expand Down
6 changes: 6 additions & 0 deletions doc/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

**Features**:

- `garden cmd` and custom commands now have a `--jobs | -j` option that enables
running multiple in parallel. Specifying `--jobs=0` will detect and set the
concurrency level to use all available cores.
([#43](https://github.com/garden-rs/garden/issues/43))
([#45](https://github.com/garden-rs/garden/pull/45))

- `garden ls` now has a `--reverse | -r` option to display trees in reverse order.
([#44](https://github.com/garden-rs/garden/pull/44))

Expand Down
33 changes: 32 additions & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where
}

// Create an Exec object.
let mut exec = exec_in_dir(&command_vec, path);
let mut exec = exec_in_dir(&command_vec, &path);

// Update the command environment
for (name, value) in &env {
Expand Down Expand Up @@ -315,3 +315,34 @@ pub(crate) fn shell_quote(arg: &str) -> String {
.map(|quoted_arg| quoted_arg.to_string())
.unwrap_or_else(|_| arg.to_string())
}

/// Get the default number of jobs to run in parallel
pub(crate) fn default_num_jobs() -> usize {
match std::thread::available_parallelism() {
Ok(value) => std::cmp::max(value.get(), 3), // "prune" requires at minimum three threads.
Err(_) => 4,
}
}

/// Initialize the global thread pool.
pub(crate) fn initialize_threads(num_jobs: usize) -> anyhow::Result<()> {
let num_jobs = if num_jobs == 0 {
default_num_jobs()
} else {
num_jobs
};
rayon::ThreadPoolBuilder::new()
.num_threads(num_jobs)
.build_global()?;

Ok(())
}

/// Initialize the global thread pool when the num_jobs option is provided.
pub(crate) fn initialize_threads_option(num_jobs: Option<usize>) -> anyhow::Result<()> {
if let Some(num_jobs) = num_jobs {
initialize_threads(num_jobs)?;
}

Ok(())
}
Loading

0 comments on commit 09c77c4

Please sign in to comment.