]> git.proxmox.com Git - rustc.git/blame - src/librustc_data_structures/jobserver.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc_data_structures / jobserver.rs
CommitLineData
60c5eb7d 1pub use jobserver_crate::Client;
532ac7d7 2use lazy_static::lazy_static;
532ac7d7
XL
3
4lazy_static! {
5 // We can only call `from_env` once per process
6
7 // Note that this is unsafe because it may misinterpret file descriptors
8 // on Unix as jobserver file descriptors. We hopefully execute this near
9 // the beginning of the process though to ensure we don't get false
10 // positives, or in other words we try to execute this before we open
11 // any file descriptors ourselves.
12 //
13 // Pick a "reasonable maximum" if we don't otherwise have
14 // a jobserver in our environment, capping out at 32 so we
15 // don't take everything down by hogging the process run queue.
16 // The fixed number is used to have deterministic compilation
17 // across machines.
18 //
19 // Also note that we stick this in a global because there could be
20 // multiple rustc instances in this process, and the jobserver is
21 // per-process.
22 static ref GLOBAL_CLIENT: Client = unsafe {
23 Client::from_env().unwrap_or_else(|| {
48663c56
XL
24 let client = Client::new(32).expect("failed to create jobserver");
25 // Acquire a token for the main thread which we can release later
26 client.acquire_raw().ok();
27 client
532ac7d7
XL
28 })
29 };
532ac7d7
XL
30}
31
32pub fn client() -> Client {
33 GLOBAL_CLIENT.clone()
34}
35
36pub fn acquire_thread() {
48663c56 37 GLOBAL_CLIENT.acquire_raw().ok();
532ac7d7
XL
38}
39
40pub fn release_thread() {
48663c56 41 GLOBAL_CLIENT.release_raw().ok();
532ac7d7 42}