Skip to content

Commit 7ff11d4

Browse files
Prepare functions to run on release/acquire of a token
1 parent 2c9854c commit 7ff11d4

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_crate::Acquired;
2-
use jobserver_crate::{HelperThread, Client};
1+
use jobserver_crate::Client;
32
use lazy_static::lazy_static;
43

54
lazy_static! {
@@ -30,19 +29,61 @@ 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 F: FnMut(Acquired) + Send + 'static,
3967
{
40-
GLOBAL_CLIENT.clone().into_helper_thread(move |token| {
41-
cb(token.expect("acquire token"))
42-
}).expect("failed to spawn helper thread")
68+
let thread = GLOBAL_CLIENT.clone().into_helper_thread(move |token| {
69+
// we've acquired a token, but we need to not use it as we have our own
70+
// custom release-on-drop struct since we'll want different logic than
71+
// just normally releasing the token in this case.
72+
//
73+
// On unix this unfortunately means that we lose the specific byte that
74+
// was in the pipe (i.e., we just write back the same byte all the time)
75+
// but that's not expected to be a problem.
76+
std::mem::forget(token.expect("acquire token"));
77+
cb(Acquired(()))
78+
}).expect("failed to spawn helper thread");
79+
80+
HelperThread {
81+
helper: thread,
82+
}
4383
}
4484

4585
pub fn acquire_thread() {
86+
notify_acquiring_token();
4687
GLOBAL_CLIENT.acquire_raw().ok();
4788
}
4889

0 commit comments

Comments
 (0)