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