|
1 | 1 | use jobserver_crate::Client;
|
2 | 2 | use lazy_static::lazy_static;
|
| 3 | +use std::sync::atomic::{Ordering, AtomicUsize}; |
3 | 4 |
|
4 | 5 | lazy_static! {
|
5 | 6 | // We can only call `from_env` once per process
|
@@ -35,10 +36,37 @@ 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 | +const EMPTY: usize = 0; |
| 45 | +const CARGO_REQUESTED: usize = 1; |
| 46 | +const MAKE_REQUESTED: usize = 2; |
| 47 | +static TOKEN_REQUESTS: AtomicUsize = AtomicUsize::new(0); |
| 48 | + |
| 49 | +fn should_notify() -> bool { |
| 50 | + let value = TOKEN_REQUESTS.load(Ordering::SeqCst); |
| 51 | + assert_ne!(value, EMPTY, "jobserver must be initialized"); |
| 52 | + value == CARGO_REQUESTED |
39 | 53 | }
|
40 | 54 |
|
41 |
| -pub fn initialize() { |
| 55 | +/// This changes a global value to the new value of token_requests, which means |
| 56 | +/// that you probably don't want to be calling this more than once per process. |
| 57 | +/// Unfortunately the jobserver is inherently a global resource (we can't have |
| 58 | +/// more than one) so the token requesting strategy must likewise be global. |
| 59 | +/// |
| 60 | +/// Usually this doesn't matter too much, as you're not wanting to set the token |
| 61 | +/// requests unless you're in the one-rustc-per-process model, and we help out |
| 62 | +/// here a bit by not resetting it once it's set (i.e., only the first init will |
| 63 | +/// change the value). |
| 64 | +pub fn initialize(token_requests: bool) { |
| 65 | + TOKEN_REQUESTS.compare_and_swap( |
| 66 | + EMPTY, |
| 67 | + if token_requests { CARGO_REQUESTED } else { MAKE_REQUESTED }, |
| 68 | + Ordering::SeqCst, |
| 69 | + ); |
42 | 70 | lazy_static::initialize(&GLOBAL_CLIENT)
|
43 | 71 | }
|
44 | 72 |
|
|
0 commit comments