@@ -1294,7 +1294,7 @@ impl Build {
1294
1294
1295
1295
#[ cfg( feature = "parallel" ) ]
1296
1296
fn compile_objects ( & self , objs : & [ Object ] , print : & PrintThread ) -> Result < ( ) , Error > {
1297
- use std:: sync:: mpsc;
1297
+ use std:: sync:: { mpsc, Once } ;
1298
1298
1299
1299
if objs. len ( ) <= 1 {
1300
1300
for obj in objs {
@@ -1306,7 +1306,7 @@ impl Build {
1306
1306
}
1307
1307
1308
1308
// Limit our parallelism globally with a jobserver.
1309
- let server = unsafe { default_jobserver ( ) } ;
1309
+ let server = jobserver ( ) ;
1310
1310
// Reacquire our process's token on drop
1311
1311
1312
1312
// When compiling objects in parallel we do a few dirty tricks to speed
@@ -1438,6 +1438,24 @@ impl Build {
1438
1438
1439
1439
return wait_thread. join ( ) . expect ( "wait_thread panics" ) ;
1440
1440
1441
+ /// Returns a suitable `jobserver::Client` used to coordinate
1442
+ /// parallelism between build scripts.
1443
+ fn jobserver ( ) -> jobserver:: Client {
1444
+ static INIT : Once = Once :: new ( ) ;
1445
+ static mut JOBSERVER : Option < jobserver:: Client > = None ;
1446
+
1447
+ fn _assert_sync < T : Sync > ( ) { }
1448
+ _assert_sync :: < jobserver:: Client > ( ) ;
1449
+
1450
+ unsafe {
1451
+ INIT . call_once ( || {
1452
+ let server = default_jobserver ( ) ;
1453
+ JOBSERVER = Some ( server) ;
1454
+ } ) ;
1455
+ JOBSERVER . clone ( ) . unwrap ( )
1456
+ }
1457
+ }
1458
+
1441
1459
unsafe fn default_jobserver ( ) -> jobserver:: Client {
1442
1460
// Try to use the environmental jobserver which Cargo typically
1443
1461
// initializes for us...
0 commit comments