-
Notifications
You must be signed in to change notification settings - Fork 960
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[naga xtask] Run validation jobs in parallel, using jobserver.
- Loading branch information
Showing
5 changed files
with
106 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Running jobs in parallel, with a controlled degree of concurrency. | ||
use std::sync::OnceLock; | ||
|
||
use jobserver::Client; | ||
|
||
static JOB_SERVER: OnceLock<Client> = OnceLock::new(); | ||
|
||
pub fn init() { | ||
JOB_SERVER.get_or_init(|| { | ||
// Try to connect to a jobserver inherited from our parent. | ||
if let Some(client) = unsafe { Client::from_env() } { | ||
log::debug!("connected to inherited jobserver client"); | ||
client | ||
} else { | ||
// Otherwise, start our own jobserver. | ||
log::debug!("no inherited jobserver client; creating a new jobserver"); | ||
Client::new(num_cpus::get()).expect("failed to create jobserver") | ||
} | ||
}); | ||
} | ||
|
||
/// Wait until it is okay to start a new job, and then spawn a thread running `body`. | ||
pub fn start_job_thread<F>(body: F) -> anyhow::Result<()> | ||
where | ||
F: FnOnce() + Send + 'static, | ||
{ | ||
let acquired = JOB_SERVER.get().unwrap().acquire()?; | ||
std::thread::spawn(move || { | ||
body(); | ||
drop(acquired); | ||
}); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters