]>
Commit | Line | Data |
---|---|---|
f035d41b XL |
1 | use crate::setup::{ParDatabase, ParDatabaseImpl}; |
2 | use salsa::ParallelDatabase; | |
3 | ||
4 | /// Test where a read and a set are racing with one another. | |
5 | /// Should be atomic. | |
6 | #[test] | |
7 | fn in_par_get_set_race() { | |
8 | let mut db = ParDatabaseImpl::default(); | |
9 | ||
10 | db.set_input('a', 100); | |
11 | db.set_input('b', 010); | |
12 | db.set_input('c', 001); | |
13 | ||
14 | let thread1 = std::thread::spawn({ | |
15 | let db = db.snapshot(); | |
16 | move || { | |
17 | let v = db.sum("abc"); | |
18 | v | |
19 | } | |
20 | }); | |
21 | ||
22 | let thread2 = std::thread::spawn(move || { | |
23 | db.set_input('a', 1000); | |
24 | db.sum("a") | |
25 | }); | |
26 | ||
27 | // If the 1st thread runs first, you get 111, otherwise you get | |
28 | // 1011; if they run concurrently and the 1st thread observes the | |
29 | // cancelation, you get back usize::max. | |
30 | let value1 = thread1.join().unwrap(); | |
31 | assert!( | |
32 | value1 == 111 || value1 == 1011 || value1 == std::usize::MAX, | |
33 | "illegal result {}", | |
34 | value1 | |
35 | ); | |
36 | ||
37 | assert_eq!(thread2.join().unwrap(), 1000); | |
38 | } |