mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-11-25 04:27:52 +00:00
93c30a953d
Switch to a procedural implementation of the `query_group!` macro, residing in the `components/salsa_macros` subcrate. Allow the user to override the invoked function via `salsa::invoke(...)` and the name of the generated query type via `salsa::query_type(...)`. In all tests, replace the `salsa::query_group! { ... }` invocations with the new attribute-style `#[salsa::query_group]` macro, and change them to the new naming scheme for query types (`...Query`). Update README, examples, and documentation.
38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
use crate::setup::{InputQuery, ParDatabase, ParDatabaseImpl};
|
|
use salsa::{Database, ParallelDatabase};
|
|
|
|
/// Test where a read and a set are racing with one another.
|
|
/// Should be atomic.
|
|
#[test]
|
|
fn in_par_get_set_race() {
|
|
let mut db = ParDatabaseImpl::default();
|
|
|
|
db.query_mut(InputQuery).set('a', 100);
|
|
db.query_mut(InputQuery).set('b', 010);
|
|
db.query_mut(InputQuery).set('c', 001);
|
|
|
|
let thread1 = std::thread::spawn({
|
|
let db = db.snapshot();
|
|
move || {
|
|
let v = db.sum("abc");
|
|
v
|
|
}
|
|
});
|
|
|
|
let thread2 = std::thread::spawn(move || {
|
|
db.query_mut(InputQuery).set('a', 1000);
|
|
db.sum("a")
|
|
});
|
|
|
|
// If the 1st thread runs first, you get 111, otherwise you get
|
|
// 1011; if they run concurrently and the 1st thread observes the
|
|
// cancelation, you get back usize::max.
|
|
let value1 = thread1.join().unwrap();
|
|
assert!(
|
|
value1 == 111 || value1 == 1011 || value1 == std::usize::MAX,
|
|
"illegal result {}",
|
|
value1
|
|
);
|
|
|
|
assert_eq!(thread2.join().unwrap(), 1000);
|
|
}
|