]> git.proxmox.com Git - rustc.git/blob - vendor/salsa/tests/parallel/signal.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / salsa / tests / parallel / signal.rs
1 use parking_lot::{Condvar, Mutex};
2
3 #[derive(Default)]
4 pub(crate) struct Signal {
5 value: Mutex<usize>,
6 cond_var: Condvar,
7 }
8
9 impl Signal {
10 pub(crate) fn signal(&self, stage: usize) {
11 log::debug!("signal({})", stage);
12
13 // This check avoids acquiring the lock for things that will
14 // clearly be a no-op. Not *necessary* but helps to ensure we
15 // are more likely to encounter weird race conditions;
16 // otherwise calls to `sum` will tend to be unnecessarily
17 // synchronous.
18 if stage > 0 {
19 let mut v = self.value.lock();
20 if stage > *v {
21 *v = stage;
22 self.cond_var.notify_all();
23 }
24 }
25 }
26
27 /// Waits until the given condition is true; the fn is invoked
28 /// with the current stage.
29 pub(crate) fn wait_for(&self, stage: usize) {
30 log::debug!("wait_for({})", stage);
31
32 // As above, avoid lock if clearly a no-op.
33 if stage > 0 {
34 let mut v = self.value.lock();
35 while *v < stage {
36 self.cond_var.wait(&mut v);
37 }
38 }
39 }
40 }