Skip to content

Commit 71fceab

Browse files
Track whether to emit jobserver messages
This adds the -Zjobserver-token-requests debugging flag which Cargo will pass down to us, but for now that flag doesn't do anything. We don't really have a good way to deal with multiple initialize calls so for now this implementation just ignores all but the first construction's token requesting mode. It's plausible we should assert that all calls have the same mode, but it's unclear whether that really brings us any benefits (and it does have a cost of forcing people to thread things through on their end).
1 parent 7ff11d4 commit 71fceab

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/librustc_jobserver/lib.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use jobserver_crate::Client;
22
use lazy_static::lazy_static;
3+
use std::sync::atomic::{Ordering, AtomicUsize};
34

45
lazy_static! {
56
// We can only call `from_env` once per process
@@ -35,10 +36,37 @@ lazy_static! {
3536
// That makes this function necessary unlike in the release case where everything is piped through
3637
// `release_thread`.
3738
fn notify_acquiring_token() {
38-
// this function does nothing for now but will be wired up to send a message to Cargo
39+
if should_notify() {
40+
// FIXME: tell Cargo of our interest
41+
}
42+
}
43+
44+
const EMPTY: usize = 0;
45+
const CARGO_REQUESTED: usize = 1;
46+
const MAKE_REQUESTED: usize = 2;
47+
static TOKEN_REQUESTS: AtomicUsize = AtomicUsize::new(0);
48+
49+
fn should_notify() -> bool {
50+
let value = TOKEN_REQUESTS.load(Ordering::SeqCst);
51+
assert_ne!(value, EMPTY, "jobserver must be initialized");
52+
value == CARGO_REQUESTED
3953
}
4054

41-
pub fn initialize() {
55+
/// This changes a global value to the new value of token_requests, which means
56+
/// that you probably don't want to be calling this more than once per process.
57+
/// Unfortunately the jobserver is inherently a global resource (we can't have
58+
/// more than one) so the token requesting strategy must likewise be global.
59+
///
60+
/// Usually this doesn't matter too much, as you're not wanting to set the token
61+
/// requests unless you're in the one-rustc-per-process model, and we help out
62+
/// here a bit by not resetting it once it's set (i.e., only the first init will
63+
/// change the value).
64+
pub fn initialize(token_requests: bool) {
65+
TOKEN_REQUESTS.compare_and_swap(
66+
EMPTY,
67+
if token_requests { CARGO_REQUESTED } else { MAKE_REQUESTED },
68+
Ordering::SeqCst,
69+
);
4270
lazy_static::initialize(&GLOBAL_CLIENT)
4371
}
4472

src/librustc_session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
15701570
insert_sideeffect: bool = (false, parse_bool, [TRACKED],
15711571
"fix undefined behavior when a thread doesn't eventually make progress \
15721572
(such as entering an empty infinite loop) by inserting llvm.sideeffect"),
1573+
jobserver_token_requests: bool = (false, parse_bool, [UNTRACKED],
1574+
"Coordinate with caller through JSON messages on acquiring/releasing jobserver tokens"),
15731575
}
15741576

15751577
pub const fn default_lib_output() -> CrateType {

src/librustc_session/session.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,20 @@ fn build_session_(
11261126
CguReuseTracker::new_disabled()
11271127
};
11281128

1129-
rustc_jobserver::initialize();
1129+
if sopts.debugging_opts.jobserver_token_requests {
1130+
if let config::ErrorOutputType::Json { .. } = sopts.error_format {
1131+
if is_diagnostic_output_raw {
1132+
panic!("Raw output format not supported with -Zjobserver-token-requests");
1133+
}
1134+
} else {
1135+
parse_sess.span_diagnostic
1136+
.fatal("-Zjobserver-token-requests can only be specified if \
1137+
using JSON error output type")
1138+
.raise();
1139+
}
1140+
}
1141+
1142+
rustc_jobserver::initialize(sopts.debugging_opts.jobserver_token_requests);
11301143

11311144
let sess = Session {
11321145
target: target_cfg,

0 commit comments

Comments
 (0)