mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-12 16:35:21 +00:00
show that executing queries on distinct keys can proceed in parallel
This commit is contained in:
parent
e9fadfcfe7
commit
3318921717
2 changed files with 42 additions and 0 deletions
|
@ -3,3 +3,4 @@ mod setup;
|
|||
mod cancellation;
|
||||
mod independent;
|
||||
mod race;
|
||||
mod true_parallel;
|
||||
|
|
41
tests/parallel/true_parallel.rs
Normal file
41
tests/parallel/true_parallel.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use crate::setup::{Input, Knobs, ParDatabase, ParDatabaseImpl, WithValue};
|
||||
use salsa::{Database, ParallelDatabase};
|
||||
|
||||
/// Test where two threads are executing sum. We show that they can
|
||||
/// both be executing sum in parallel by having thread1 wait for
|
||||
/// thread2 to send a signal before it leaves (similarly, thread2
|
||||
/// waits for thread1 to send a signal before it enters).
|
||||
#[test]
|
||||
fn true_parallel_different_keys() {
|
||||
let db = ParDatabaseImpl::default();
|
||||
|
||||
db.query(Input).set('a', 100);
|
||||
db.query(Input).set('b', 010);
|
||||
db.query(Input).set('c', 001);
|
||||
|
||||
// Thread 1 will signal stage 1 when it enters and wait for stage 2.
|
||||
let thread1 = std::thread::spawn({
|
||||
let db = db.fork();
|
||||
move || {
|
||||
let v = db.knobs().sum_signal_on_entry.with_value(1, || {
|
||||
db.knobs().sum_await_on_exit.with_value(2, || db.sum("a"))
|
||||
});
|
||||
v
|
||||
}
|
||||
});
|
||||
|
||||
// Thread 2 will await stage 1 when it enters and signal stage 2
|
||||
// when it leaves.
|
||||
let thread2 = std::thread::spawn({
|
||||
let db = db.fork();
|
||||
move || {
|
||||
let v = db.knobs().sum_await_on_entry.with_value(1, || {
|
||||
db.knobs().sum_signal_on_exit.with_value(2, || db.sum("b"))
|
||||
});
|
||||
v
|
||||
}
|
||||
});
|
||||
|
||||
assert_eq!(thread1.join().unwrap(), 100);
|
||||
assert_eq!(thread2.join().unwrap(), 010);
|
||||
}
|
Loading…
Reference in a new issue