Skip to content

Commit 6117f52

Browse files
Stop threading Client through into the NeedsToken message
1 parent 1204a52 commit 6117f52

File tree

3 files changed

+25
-38
lines changed

3 files changed

+25
-38
lines changed

src/cargo/core/compiler/context/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ pub struct Context<'a, 'cfg> {
6969
/// metadata files in addition to the rlib itself. This is only filled in
7070
/// when `pipelining` above is enabled.
7171
rmeta_required: HashSet<Unit<'a>>,
72+
73+
/// When we're in jobserver-per-rustc process mode, this keeps those
74+
/// jobserver clients for each Unit (which eventually becomes a rustc
75+
/// process).
76+
pub rustc_clients: HashMap<Unit<'a>, Client>,
7277
}
7378

7479
impl<'a, 'cfg> Context<'a, 'cfg> {
@@ -112,6 +117,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
112117
unit_dependencies,
113118
files: None,
114119
rmeta_required: HashSet::new(),
120+
rustc_clients: HashMap::new(),
115121
pipelining,
116122
})
117123
}

src/cargo/core/compiler/job_queue.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ enum Message {
189189
Finish(JobId, Artifact, CargoResult<()>),
190190

191191
// This client should get release_raw called on it with one of our tokens
192-
NeedsToken(JobId, Client),
192+
NeedsToken(JobId),
193193

194194
// A token previously passed to a NeedsToken client is being released.
195195
ReleaseToken(JobId),
@@ -234,10 +234,10 @@ impl<'a> JobState<'a> {
234234
/// The rustc underlying this Job is about to acquire a jobserver token (i.e., block)
235235
/// on the passed client.
236236
///
237-
/// This should arrange for the passed client to eventually get a token via
237+
/// This should arrange for the associated client to eventually get a token via
238238
/// `client.release_raw()`.
239-
pub fn will_acquire(&self, client: &Client) {
240-
let _ = self.tx.send(Message::NeedsToken(self.id, client.clone()));
239+
pub fn will_acquire(&self) {
240+
let _ = self.tx.send(Message::NeedsToken(self.id));
241241
}
242242

243243
/// The rustc underlying this Job is informing us that it is done with a jobserver token.
@@ -543,9 +543,10 @@ impl<'a, 'cfg> DrainState<'a, 'cfg> {
543543
let token = acquired_token.chain_err(|| "failed to acquire jobserver token")?;
544544
self.tokens.push(token);
545545
}
546-
Message::NeedsToken(id, client) => {
546+
Message::NeedsToken(id) => {
547547
log::info!("queue token request");
548548
jobserver_helper.request_token();
549+
let client = cx.rustc_clients[&self.active[&id]].clone();
549550
self.to_send_clients
550551
.entry(id)
551552
.or_insert_with(Vec::new)

src/cargo/core/compiler/mod.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use crate::util::errors::{self, CargoResult, CargoResultExt, Internal, ProcessEr
4848
use crate::util::machine_message::Message;
4949
use crate::util::{self, machine_message, ProcessBuilder};
5050
use crate::util::{internal, join_paths, paths, profile};
51-
use jobserver::Client;
5251

5352
/// A glorified callback for executing calls to rustc. Rather than calling rustc
5453
/// directly, we'll use an `Executor`, giving clients an opportunity to intercept
@@ -172,7 +171,7 @@ fn rustc<'a, 'cfg>(
172171
unit: &Unit<'a>,
173172
exec: &Arc<dyn Executor>,
174173
) -> CargoResult<Work> {
175-
let (rustc_client, mut rustc) = prepare_rustc(cx, &unit.target.rustc_crate_types(), unit)?;
174+
let mut rustc = prepare_rustc(cx, &unit.target.rustc_crate_types(), unit)?;
176175
let build_plan = cx.bcx.build_config.build_plan;
177176

178177
let name = unit.pkg.name().to_string();
@@ -287,16 +286,7 @@ fn rustc<'a, 'cfg>(
287286
&target,
288287
mode,
289288
&mut |line| on_stdout_line(state, line, package_id, &target),
290-
&mut |line| {
291-
on_stderr_line(
292-
state,
293-
line,
294-
package_id,
295-
&target,
296-
&mut output_options,
297-
&rustc_client,
298-
)
299-
},
289+
&mut |line| on_stderr_line(state, line, package_id, &target, &mut output_options),
300290
)
301291
.map_err(internal_if_simple_exit_code)
302292
.chain_err(|| format!("could not compile `{}`.", name))?;
@@ -544,22 +534,21 @@ fn prepare_rustc<'a, 'cfg>(
544534
cx: &mut Context<'a, 'cfg>,
545535
crate_types: &[&str],
546536
unit: &Unit<'a>,
547-
) -> CargoResult<(Option<Client>, ProcessBuilder)> {
537+
) -> CargoResult<ProcessBuilder> {
548538
let is_primary = cx.is_primary_package(unit);
549539

550540
let mut base = cx.compilation.rustc_process(unit.pkg, is_primary)?;
551-
let client = if cx.bcx.config.cli_unstable().jobserver_per_rustc {
541+
if cx.bcx.config.cli_unstable().jobserver_per_rustc {
552542
let client = cx.new_jobserver()?;
553543
base.inherit_jobserver(&client);
554544
base.arg("-Zjobserver-token-requests");
555-
Some(client)
545+
assert!(cx.rustc_clients.insert(*unit, client).is_none());
556546
} else {
557547
base.inherit_jobserver(&cx.jobserver);
558-
None
559-
};
548+
}
560549
build_base_args(cx, &mut base, unit, crate_types)?;
561550
build_deps_args(&mut base, cx, unit)?;
562-
Ok((client, base))
551+
Ok(base)
563552
}
564553

565554
fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<Work> {
@@ -618,9 +607,7 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
618607
rustdoc
619608
.exec_with_streaming(
620609
&mut |line| on_stdout_line(state, line, package_id, &target),
621-
&mut |line| {
622-
on_stderr_line(state, line, package_id, &target, &mut output_options, &None)
623-
},
610+
&mut |line| on_stderr_line(state, line, package_id, &target, &mut output_options),
624611
false,
625612
)
626613
.chain_err(|| format!("Could not document `{}`.", name))?;
@@ -1101,9 +1088,8 @@ fn on_stderr_line(
11011088
package_id: PackageId,
11021089
target: &Target,
11031090
options: &mut OutputOptions,
1104-
rustc_client: &Option<Client>,
11051091
) -> CargoResult<()> {
1106-
if on_stderr_line_inner(state, line, package_id, target, options, rustc_client)? {
1092+
if on_stderr_line_inner(state, line, package_id, target, options)? {
11071093
// Check if caching is enabled.
11081094
if let Some((path, cell)) = &mut options.cache_cell {
11091095
// Cache the output, which will be replayed later when Fresh.
@@ -1123,7 +1109,6 @@ fn on_stderr_line_inner(
11231109
package_id: PackageId,
11241110
target: &Target,
11251111
options: &mut OutputOptions,
1126-
rustc_client: &Option<Client>,
11271112
) -> CargoResult<bool> {
11281113
// We primarily want to use this function to process JSON messages from
11291114
// rustc. The compiler should always print one JSON message per line, and
@@ -1250,14 +1235,9 @@ fn on_stderr_line_inner(
12501235
"found jobserver directive from rustc: `{:?}`",
12511236
jobserver_event
12521237
);
1253-
match rustc_client {
1254-
Some(client) => match jobserver_event {
1255-
Event::WillAcquire => state.will_acquire(client),
1256-
Event::Release => state.release_token(),
1257-
},
1258-
None => {
1259-
panic!("Received jobserver event without a client");
1260-
}
1238+
match jobserver_event {
1239+
Event::WillAcquire => state.will_acquire(),
1240+
Event::Release => state.release_token(),
12611241
}
12621242
return Ok(false);
12631243
}
@@ -1310,7 +1290,7 @@ fn replay_output_cache(
13101290
break;
13111291
}
13121292
let trimmed = line.trim_end_matches(&['\n', '\r'][..]);
1313-
on_stderr_line(state, trimmed, package_id, &target, &mut options, &None)?;
1293+
on_stderr_line(state, trimmed, package_id, &target, &mut options)?;
13141294
line.clear();
13151295
}
13161296
Ok(())

0 commit comments

Comments
 (0)