|
1 | 1 | use jobserver::Client;
|
2 | 2 | use lazy_static::lazy_static;
|
| 3 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
3 | 4 |
|
4 | 5 | lazy_static! {
|
5 | 6 | // We can only call `from_env` once per process
|
@@ -35,15 +36,50 @@ lazy_static! {
|
35 | 36 | // That makes this function necessary unlike in the release case where everything is piped through
|
36 | 37 | // `release_thread`.
|
37 | 38 | 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 |
39 | 61 | }
|
40 | 62 |
|
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 | + ); |
42 | 78 | lazy_static::initialize(&GLOBAL_CLIENT)
|
43 | 79 | }
|
44 | 80 |
|
45 | 81 | pub struct HelperThread {
|
46 |
| - helper: jobserver_crate::HelperThread, |
| 82 | + helper: jobserver::HelperThread, |
47 | 83 | }
|
48 | 84 |
|
49 | 85 | impl HelperThread {
|
|
0 commit comments