Skip to content

Commit 87bd0ba

Browse files
committed
Improve parallel binding generation performance
1 parent 286674a commit 87bd0ba

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

build/generator.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ffi::{OsStr, OsString};
22
use std::fs::{File, OpenOptions};
33
use std::io::{BufRead, BufReader, BufWriter, Write};
4+
use std::ops::Deref;
45
use std::path::{Path, PathBuf};
56
use std::process::Command;
67
use std::sync::Arc;
@@ -305,14 +306,19 @@ fn collect_generated_bindings(modules: &[String], target_module_dir: &Path, manu
305306
Ok(())
306307
}
307308

308-
fn build_job_server() -> Result<jobserver::Client> {
309+
fn build_job_server() -> Result<Jobserver> {
309310
unsafe { jobserver::Client::from_env() }
310-
.and_then(|c| {
311-
let available_jobs = c.available().unwrap_or(0);
311+
.and_then(|client| {
312+
let own_token_released = client.release_raw().is_ok();
313+
let available_jobs = client.available().unwrap_or(0);
312314
if available_jobs > 0 {
313315
eprintln!("=== Using environment job server with the the amount of available jobs: {available_jobs}");
314-
Some(c)
316+
Some(Jobserver {
317+
client,
318+
reacquire_token_on_drop: own_token_released,
319+
})
315320
} else {
321+
client.acquire_raw().expect("Can't reacquire build script thread token");
316322
eprintln!(
317323
"=== Available jobs from the environment created jobserver is: {available_jobs} or there is an error reading that value"
318324
);
@@ -326,7 +332,31 @@ fn build_job_server() -> Result<jobserver::Client> {
326332
.unwrap_or(2)
327333
.max(1);
328334
eprintln!("=== Creating a new job server with num_jobs: {num_jobs}");
329-
jobserver::Client::new(num_jobs).ok()
335+
jobserver::Client::new(num_jobs).ok().map(|client| Jobserver {
336+
client,
337+
reacquire_token_on_drop: false,
338+
})
330339
})
331340
.ok_or_else(|| "Can't create job server".into())
332341
}
342+
343+
pub struct Jobserver {
344+
client: jobserver::Client,
345+
reacquire_token_on_drop: bool,
346+
}
347+
348+
impl Drop for Jobserver {
349+
fn drop(&mut self) {
350+
if self.reacquire_token_on_drop {
351+
self.client.acquire_raw().expect("Can't reacquire build script thread token");
352+
}
353+
}
354+
}
355+
356+
impl Deref for Jobserver {
357+
type Target = jobserver::Client;
358+
359+
fn deref(&self) -> &Self::Target {
360+
&self.client
361+
}
362+
}

0 commit comments

Comments
 (0)