Skip to content

Commit 14530d0

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 03b3f7c commit 14530d0

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

src/librustc_jobserver/lib.rs

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

45
lazy_static! {
56
// We can only call `from_env` once per process
@@ -35,15 +36,50 @@ 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+
// These are the values for TOKEN_REQUESTS, which is an enum between these
45+
// different options.
46+
//
47+
//
48+
// It takes the following values:
49+
// * EMPTY: not yet set
50+
// * CARGO_REQUESTED: we're in the jobserver-per-rustc mode
51+
// * MAKE_REQUESTED: legacy global jobserver client
52+
const EMPTY: usize = 0;
53+
const CARGO_REQUESTED: usize = 1;
54+
const MAKE_REQUESTED: usize = 2;
55+
static TOKEN_REQUESTS: AtomicUsize = AtomicUsize::new(EMPTY);
56+
57+
fn should_notify() -> bool {
58+
let value = TOKEN_REQUESTS.load(Ordering::SeqCst);
59+
assert!(value != EMPTY, "jobserver must be initialized");
60+
value == CARGO_REQUESTED
3961
}
4062

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

4581
pub struct HelperThread {
46-
helper: jobserver_crate::HelperThread,
82+
helper: jobserver::HelperThread,
4783
}
4884

4985
impl HelperThread {

src/librustc_session/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,4 +970,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
970970
"link the `.rlink` file generated by `-Z no-link`"),
971971
new_llvm_pass_manager: Option<bool> = (None, parse_opt_bool, [TRACKED],
972972
"use new LLVM pass manager"),
973+
jobserver_token_requests: bool = (false, parse_bool, [UNTRACKED],
974+
"Coordinate with caller through JSON messages on acquiring/releasing jobserver tokens"),
973975
}

src/librustc_session/session.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,23 @@ fn build_session_(
10321032
sopts.debugging_opts.time_passes,
10331033
);
10341034

1035-
rustc_jobserver::initialize();
1035+
if sopts.debugging_opts.jobserver_token_requests {
1036+
if let config::ErrorOutputType::Json { .. } = sopts.error_format {
1037+
if is_diagnostic_output_raw {
1038+
panic!("Raw output format not supported with -Zjobserver-token-requests");
1039+
}
1040+
} else {
1041+
parse_sess
1042+
.span_diagnostic
1043+
.fatal(
1044+
"-Zjobserver-token-requests can only be specified if \
1045+
using JSON error output type",
1046+
)
1047+
.raise();
1048+
}
1049+
}
1050+
1051+
rustc_jobserver::initialize(sopts.debugging_opts.jobserver_token_requests);
10361052

10371053
let sess = Session {
10381054
target: target_cfg,

0 commit comments

Comments
 (0)