Skip to content

Commit 03b3f7c

Browse files
Prepare functions to run on release/acquire of a token
1 parent ac96772 commit 03b3f7c

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

src/librustc_jobserver/lib.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
pub use jobserver::Acquired;
2-
use jobserver::{Client, HelperThread};
1+
use jobserver::Client;
32
use lazy_static::lazy_static;
43

54
lazy_static! {
@@ -30,21 +29,63 @@ lazy_static! {
3029
};
3130
}
3231

32+
// Unlike releasing tokens, there's not really a "one size fits all" approach, as we have two
33+
// primary ways of acquiring a token: via the helper thread, and via the acquire_thread function.
34+
//
35+
// That makes this function necessary unlike in the release case where everything is piped through
36+
// `release_thread`.
37+
fn notify_acquiring_token() {
38+
// this function does nothing for now but will be wired up to send a message to Cargo
39+
}
40+
3341
pub fn initialize() {
3442
lazy_static::initialize(&GLOBAL_CLIENT)
3543
}
3644

45+
pub struct HelperThread {
46+
helper: jobserver_crate::HelperThread,
47+
}
48+
49+
impl HelperThread {
50+
// This API does not block, but is shimmed so that we can inform Cargo of our interest here.
51+
pub fn request_token(&self) {
52+
notify_acquiring_token();
53+
self.helper.request_token();
54+
}
55+
}
56+
57+
pub struct Acquired(());
58+
59+
impl Drop for Acquired {
60+
fn drop(&mut self) {
61+
release_thread();
62+
}
63+
}
64+
3765
pub fn helper_thread<F>(mut cb: F) -> HelperThread
3866
where
3967
F: FnMut(Acquired) + Send + 'static,
4068
{
41-
GLOBAL_CLIENT
69+
let thread = GLOBAL_CLIENT
4270
.clone()
43-
.into_helper_thread(move |token| cb(token.expect("acquire token")))
44-
.expect("failed to spawn helper thread")
71+
.into_helper_thread(move |token| {
72+
// we've acquired a token, but we need to not use it as we have our own
73+
// custom release-on-drop struct since we'll want different logic than
74+
// just normally releasing the token in this case.
75+
//
76+
// On unix this unfortunately means that we lose the specific byte that
77+
// was in the pipe (i.e., we just write back the same byte all the time)
78+
// but that's not expected to be a problem.
79+
std::mem::forget(token.expect("acquire token"));
80+
cb(Acquired(()))
81+
})
82+
.expect("failed to spawn helper thread");
83+
84+
HelperThread { helper: thread }
4585
}
4686

4787
pub fn acquire_thread() {
88+
notify_acquiring_token();
4889
GLOBAL_CLIENT.acquire_raw().ok();
4990
}
5091

0 commit comments

Comments
 (0)