]> git.proxmox.com Git - rustc.git/blob - vendor/salsa/tests/parallel/frozen.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / salsa / tests / parallel / frozen.rs
1 use crate::setup::{ParDatabase, ParDatabaseImpl};
2 use crate::signal::Signal;
3 use salsa::{Database, ParallelDatabase};
4 use std::sync::Arc;
5
6 /// Add test where a call to `sum` is cancelled by a simultaneous
7 /// write. Check that we recompute the result in next revision, even
8 /// though none of the inputs have changed.
9 #[test]
10 fn in_par_get_set_cancellation() {
11 let mut db = ParDatabaseImpl::default();
12
13 db.set_input('a', 1);
14
15 let signal = Arc::new(Signal::default());
16
17 let thread1 = std::thread::spawn({
18 let db = db.snapshot();
19 let signal = signal.clone();
20 move || {
21 // Check that cancellation flag is not yet set, because
22 // `set` cannot have been called yet.
23 assert!(!db.salsa_runtime().is_current_revision_canceled());
24
25 // Signal other thread to proceed.
26 signal.signal(1);
27
28 // Wait for other thread to signal cancellation
29 while !db.salsa_runtime().is_current_revision_canceled() {
30 std::thread::yield_now();
31 }
32
33 // Since we have not yet released revision lock, we should
34 // see 1 here.
35 let v = db.input('a');
36
37 // Since this is a snapshotted database, we are in a consistent
38 // revision, so this must yield the same value.
39 let w = db.input('a');
40
41 (v, w)
42 }
43 });
44
45 let thread2 = std::thread::spawn({
46 let signal = signal.clone();
47 move || {
48 // Wait until thread 1 has asserted that they are not cancelled
49 // before we invoke `set.`
50 signal.wait_for(1);
51
52 // This will block until thread1 drops the revision lock.
53 db.set_input('a', 2);
54
55 db.input('a')
56 }
57 });
58
59 let (a, b) = thread1.join().unwrap();
60 assert_eq!(a, 1);
61 assert_eq!(b, 1);
62
63 let c = thread2.join().unwrap();
64 assert_eq!(c, 2);
65 }