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