@@ -1294,7 +1294,7 @@ impl Build {
12941294
12951295 #[ cfg( feature = "parallel" ) ]
12961296 fn compile_objects ( & self , objs : & [ Object ] , print : & PrintThread ) -> Result < ( ) , Error > {
1297- use std:: sync:: mpsc;
1297+ use std:: sync:: { mpsc, Once } ;
12981298
12991299 if objs. len ( ) <= 1 {
13001300 for obj in objs {
@@ -1306,7 +1306,7 @@ impl Build {
13061306 }
13071307
13081308 // Limit our parallelism globally with a jobserver.
1309- let server = unsafe { default_jobserver ( ) } ;
1309+ let server = jobserver ( ) ;
13101310 // Reacquire our process's token on drop
13111311
13121312 // When compiling objects in parallel we do a few dirty tricks to speed
@@ -1438,6 +1438,24 @@ impl Build {
14381438
14391439 return wait_thread. join ( ) . expect ( "wait_thread panics" ) ;
14401440
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+
14411459 unsafe fn default_jobserver ( ) -> jobserver:: Client {
14421460 // Try to use the environmental jobserver which Cargo typically
14431461 // initializes for us...
0 commit comments