1use std::marker::PhantomData;
23use crate::{utilities::OnceLock, Error};
45pub(crate) struct JobToken(PhantomData<()>);
67impl JobToken {
8fn new() -> Self {
9Self(PhantomData)
10 }
11}
1213impl Drop for JobToken {
14fn drop(&mut self) {
15match JobTokenServer::new() {
16 JobTokenServer::Inherited(jobserver) => jobserver.release_token_raw(),
17 JobTokenServer::InProcess(jobserver) => jobserver.release_token_raw(),
18 }
19 }
20}
2122enum JobTokenServer {
23 Inherited(inherited_jobserver::JobServer),
24 InProcess(inprocess_jobserver::JobServer),
25}
2627impl JobTokenServer {
28/// This function returns a static reference to the jobserver because
29 /// - creating a jobserver from env is a bit fd-unsafe (e.g. the fd might
30 /// be closed by other jobserver users in the process) and better do it
31 /// at the start of the program.
32 /// - in case a jobserver cannot be created from env (e.g. it's not
33 /// present), we will create a global in-process only jobserver
34 /// that has to be static so that it will be shared by all cc
35 /// compilation.
36fn new() -> &'static Self {
37// TODO: Replace with a OnceLock once MSRV is 1.70
38static JOBSERVER: OnceLock<JobTokenServer> = OnceLock::new();
3940 JOBSERVER.get_or_init(|| {
41unsafe { inherited_jobserver::JobServer::from_env() }
42 .map(Self::Inherited)
43 .unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new()))
44 })
45 }
46}
4748pub(crate) enum ActiveJobTokenServer {
49 Inherited(inherited_jobserver::ActiveJobServer<'static>),
50 InProcess(&'static inprocess_jobserver::JobServer),
51}
5253impl ActiveJobTokenServer {
54pub(crate) fn new() -> Self {
55match JobTokenServer::new() {
56 JobTokenServer::Inherited(inherited_jobserver) => {
57Self::Inherited(inherited_jobserver.enter_active())
58 }
59 JobTokenServer::InProcess(inprocess_jobserver) => Self::InProcess(inprocess_jobserver),
60 }
61 }
6263pub(crate) async fn acquire(&mut self) -> Result<JobToken, Error> {
64match self {
65Self::Inherited(jobserver) => jobserver.acquire().await,
66Self::InProcess(jobserver) => Ok(jobserver.acquire().await),
67 }
68 }
69}
7071mod inherited_jobserver {
72use super::JobToken;
7374use crate::{parallel::async_executor::YieldOnce, Error, ErrorKind};
7576use std::{
77 io, mem,
78 sync::{mpsc, Mutex, MutexGuard, PoisonError},
79 };
8081pub(super) struct JobServer {
82/// Implicit token for this process which is obtained and will be
83 /// released in parent. Since JobTokens only give back what they got,
84 /// there should be at most one global implicit token in the wild.
85 ///
86 /// Since Rust does not execute any `Drop` for global variables,
87 /// we can't just put it back to jobserver and then re-acquire it at
88 /// the end of the process.
89 ///
90 /// Use `Mutex` to avoid race between acquire and release.
91 /// If an `AtomicBool` is used, then it's possible for:
92 /// - `release_token_raw`: Tries to set `global_implicit_token` to true, but it is already
93 /// set to `true`, continue to release it to jobserver
94 /// - `acquire` takes the global implicit token, set `global_implicit_token` to false
95 /// - `release_token_raw` now writes the token back into the jobserver, while
96 /// `global_implicit_token` is `false`
97 ///
98 /// If the program exits here, then cc effectively increases parallelism by one, which is
99 /// incorrect, hence we use a `Mutex` here.
100global_implicit_token: Mutex<bool>,
101 inner: jobserver::Client,
102 }
103104impl JobServer {
105pub(super) unsafe fn from_env() -> Option<Self> {
106 jobserver::Client::from_env().map(|inner| Self {
107 inner,
108 global_implicit_token: Mutex::new(true),
109 })
110 }
111112fn get_global_implicit_token(&self) -> MutexGuard<'_, bool> {
113self.global_implicit_token
114 .lock()
115 .unwrap_or_else(PoisonError::into_inner)
116 }
117118/// All tokens except for the global implicit token will be put back into the jobserver
119 /// immediately and they cannot be cached, since Rust does not call `Drop::drop` on
120 /// global variables.
121pub(super) fn release_token_raw(&self) {
122let mut global_implicit_token = self.get_global_implicit_token();
123124if *global_implicit_token {
125// There's already a global implicit token, so this token must
126 // be released back into jobserver.
127 //
128 // `release_raw` should not block
129let _ = self.inner.release_raw();
130 } else {
131*global_implicit_token = true;
132 }
133 }
134135pub(super) fn enter_active(&self) -> ActiveJobServer<'_> {
136 ActiveJobServer {
137 jobserver: self,
138 helper_thread: None,
139 }
140 }
141 }
142143struct HelperThread {
144 inner: jobserver::HelperThread,
145/// When rx is dropped, all the token stored within it will be dropped.
146rx: mpsc::Receiver<io::Result<jobserver::Acquired>>,
147 }
148149impl HelperThread {
150fn new(jobserver: &JobServer) -> Result<Self, Error> {
151let (tx, rx) = mpsc::channel();
152153Ok(Self {
154 rx,
155 inner: jobserver.inner.clone().into_helper_thread(move |res| {
156let _ = tx.send(res);
157 })?,
158 })
159 }
160 }
161162pub(crate) struct ActiveJobServer<'a> {
163 jobserver: &'a JobServer,
164 helper_thread: Option<HelperThread>,
165 }
166167impl<'a> ActiveJobServer<'a> {
168pub(super) async fn acquire(&mut self) -> Result<JobToken, Error> {
169let mut has_requested_token = false;
170171loop {
172// Fast path
173if mem::replace(&mut *self.jobserver.get_global_implicit_token(), false) {
174break Ok(JobToken::new());
175 }
176177match self.jobserver.inner.try_acquire() {
178Ok(Some(acquired)) => {
179 acquired.drop_without_releasing();
180break Ok(JobToken::new());
181 }
182Ok(None) => YieldOnce::default().await,
183Err(err) if err.kind() == io::ErrorKind::Unsupported => {
184// Fallback to creating a help thread with blocking acquire
185let helper_thread = if let Some(thread) = self.helper_thread.as_ref() {
186 thread
187 } else {
188self.helper_thread
189 .insert(HelperThread::new(self.jobserver)?)
190 };
191192match helper_thread.rx.try_recv() {
193Ok(res) => {
194let acquired = res?;
195 acquired.drop_without_releasing();
196break Ok(JobToken::new());
197 }
198Err(mpsc::TryRecvError::Disconnected) => break Err(Error::new(
199 ErrorKind::JobserverHelpThreadError,
200"jobserver help thread has returned before ActiveJobServer is dropped",
201 )),
202Err(mpsc::TryRecvError::Empty) => {
203if !has_requested_token {
204 helper_thread.inner.request_token();
205 has_requested_token = true;
206 }
207 YieldOnce::default().await
208}
209 }
210 }
211Err(err) => break Err(err.into()),
212 }
213 }
214 }
215 }
216}
217218mod inprocess_jobserver {
219use super::JobToken;
220221use crate::parallel::async_executor::YieldOnce;
222223use std::{
224 env::var,
225 sync::atomic::{
226 AtomicU32,
227 Ordering::{AcqRel, Acquire},
228 },
229 };
230231pub(crate) struct JobServer(AtomicU32);
232233impl JobServer {
234pub(super) fn new() -> Self {
235// Use `NUM_JOBS` if set (it's configured by Cargo) and otherwise
236 // just fall back to a semi-reasonable number.
237 //
238 // Note that we could use `num_cpus` here but it's an extra
239 // dependency that will almost never be used, so
240 // it's generally not too worth it.
241let mut parallelism = 4;
242// TODO: Use std::thread::available_parallelism as an upper bound
243 // when MSRV is bumped.
244if let Ok(amt) = var("NUM_JOBS") {
245if let Ok(amt) = amt.parse() {
246 parallelism = amt;
247 }
248 }
249250Self(AtomicU32::new(parallelism))
251 }
252253pub(super) async fn acquire(&self) -> JobToken {
254loop {
255let res = self
256.0
257.fetch_update(AcqRel, Acquire, |tokens| tokens.checked_sub(1));
258259if res.is_ok() {
260break JobToken::new();
261 }
262263 YieldOnce::default().await
264}
265 }
266267pub(super) fn release_token_raw(&self) {
268self.0.fetch_add(1, AcqRel);
269 }
270 }
271}