|
1 |
| -pub use jobserver_crate::Acquired; |
2 |
| -use jobserver_crate::{HelperThread, Client}; |
| 1 | +use jobserver_crate::Client; |
3 | 2 | use lazy_static::lazy_static;
|
4 | 3 |
|
5 | 4 | lazy_static! {
|
@@ -30,19 +29,61 @@ lazy_static! {
|
30 | 29 | };
|
31 | 30 | }
|
32 | 31 |
|
| 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 | + |
33 | 41 | pub fn initialize() {
|
34 | 42 | lazy_static::initialize(&GLOBAL_CLIENT)
|
35 | 43 | }
|
36 | 44 |
|
| 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 | + |
37 | 65 | pub fn helper_thread<F>(mut cb: F) -> HelperThread
|
38 | 66 | where F: FnMut(Acquired) + Send + 'static,
|
39 | 67 | {
|
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 | + } |
43 | 83 | }
|
44 | 84 |
|
45 | 85 | pub fn acquire_thread() {
|
| 86 | + notify_acquiring_token(); |
46 | 87 | GLOBAL_CLIENT.acquire_raw().ok();
|
47 | 88 | }
|
48 | 89 |
|
|
0 commit comments