1
1
use std:: ffi:: { OsStr , OsString } ;
2
2
use std:: fs:: { File , OpenOptions } ;
3
3
use std:: io:: { BufRead , BufReader , BufWriter , Write } ;
4
+ use std:: ops:: Deref ;
4
5
use std:: path:: { Path , PathBuf } ;
5
6
use std:: process:: Command ;
6
7
use std:: sync:: Arc ;
@@ -305,14 +306,19 @@ fn collect_generated_bindings(modules: &[String], target_module_dir: &Path, manu
305
306
Ok ( ( ) )
306
307
}
307
308
308
- fn build_job_server ( ) -> Result < jobserver :: Client > {
309
+ fn build_job_server ( ) -> Result < Jobserver > {
309
310
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 ) ;
312
314
if available_jobs > 0 {
313
315
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
+ } )
315
320
} else {
321
+ client. acquire_raw ( ) . expect ( "Can't reacquire build script thread token" ) ;
316
322
eprintln ! (
317
323
"=== Available jobs from the environment created jobserver is: {available_jobs} or there is an error reading that value"
318
324
) ;
@@ -326,7 +332,31 @@ fn build_job_server() -> Result<jobserver::Client> {
326
332
. unwrap_or ( 2 )
327
333
. max ( 1 ) ;
328
334
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
+ } )
330
339
} )
331
340
. ok_or_else ( || "Can't create job server" . into ( ) )
332
341
}
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