salsa/tests/parallel/setup.rs

165 lines
4.1 KiB
Rust
Raw Normal View History

2018-10-19 09:59:19 +00:00
use crate::signal::Signal;
2018-10-13 09:22:38 +00:00
use salsa::Database;
use salsa::ParallelDatabase;
use std::cell::Cell;
use std::sync::Arc;
salsa::query_group! {
pub(crate) trait ParDatabase: Knobs + salsa::Database {
fn input(key: char) -> usize {
type Input;
storage input;
}
fn sum(key: &'static str) -> usize {
type Sum;
}
fn sum2(key: &'static str) -> usize {
type Sum2;
2018-10-13 09:22:38 +00:00
}
}
}
/// Various "knobs" and utilities used by tests to force
/// a certain behavior.
pub(crate) trait Knobs {
fn knobs(&self) -> &KnobsStruct;
2018-10-13 09:22:38 +00:00
fn signal(&self, stage: usize);
2018-10-13 09:22:38 +00:00
fn wait_for(&self, stage: usize);
2018-10-13 09:22:38 +00:00
}
pub(crate) trait WithValue<T> {
fn with_value<R>(&self, value: T, closure: impl FnOnce() -> R) -> R;
}
impl<T> WithValue<T> for Cell<T> {
fn with_value<R>(&self, value: T, closure: impl FnOnce() -> R) -> R {
let old_value = self.replace(value);
let result = closure();
self.set(old_value);
result
}
}
/// Various "knobs" that can be used to customize how the queries
/// behave on one specific thread. Note that this state is
/// intentionally thread-local (apart from `signal`).
2018-10-13 09:22:38 +00:00
#[derive(Clone, Default)]
pub(crate) struct KnobsStruct {
/// A kind of flexible barrier used to coordinate execution across
/// threads to ensure we reach various weird states.
pub(crate) signal: Arc<Signal>,
2018-10-23 09:25:09 +00:00
/// When this database is about to block, send a signal.
pub(crate) signal_on_will_block: Cell<usize>,
/// Invocations of `sum` will signal this stage on entry.
pub(crate) sum_signal_on_entry: Cell<usize>,
/// Invocations of `sum` will wait for this stage on entry.
pub(crate) sum_wait_for_on_entry: Cell<usize>,
/// If true, invocations of `sum` will wait for cancellation before
/// they exit.
pub(crate) sum_wait_for_cancellation: Cell<bool>,
/// Invocations of `sum` will wait for this stage prior to exiting.
pub(crate) sum_wait_for_on_exit: Cell<usize>,
/// Invocations of `sum` will signal this stage prior to exiting.
pub(crate) sum_signal_on_exit: Cell<usize>,
2018-10-13 09:22:38 +00:00
}
fn sum(db: &impl ParDatabase, key: &'static str) -> usize {
let mut sum = 0;
db.signal(db.knobs().sum_signal_on_entry.get());
db.wait_for(db.knobs().sum_wait_for_on_entry.get());
2018-10-13 09:22:38 +00:00
for ch in key.chars() {
sum += db.input(ch);
}
if db.knobs().sum_wait_for_cancellation.get() {
log::debug!("waiting for cancellation");
2018-10-13 09:22:38 +00:00
while !db.salsa_runtime().is_current_revision_canceled() {
std::thread::yield_now();
}
log::debug!("cancellation observed");
return std::usize::MAX; // when we are cancelled, we return usize::MAX.
}
db.wait_for(db.knobs().sum_wait_for_on_exit.get());
db.signal(db.knobs().sum_signal_on_exit.get());
2018-10-13 09:22:38 +00:00
sum
}
fn sum2(db: &impl ParDatabase, key: &'static str) -> usize {
sum(db, key)
}
2018-10-13 09:22:38 +00:00
#[derive(Default)]
pub struct ParDatabaseImpl {
runtime: salsa::Runtime<ParDatabaseImpl>,
knobs: KnobsStruct,
}
impl Database for ParDatabaseImpl {
fn salsa_runtime(&self) -> &salsa::Runtime<ParDatabaseImpl> {
&self.runtime
}
2018-10-23 09:25:09 +00:00
fn salsa_event(&self, event_fn: impl Fn() -> salsa::Event<Self>) {
let event = event_fn();
match event.kind {
salsa::EventKind::WillBlockOn { .. } => {
self.signal(self.knobs().signal_on_will_block.get());
}
_ => {}
}
}
2018-10-13 09:22:38 +00:00
}
impl ParallelDatabase for ParDatabaseImpl {
2018-10-31 10:03:33 +00:00
fn fork_mut(&self) -> Self {
2018-10-13 09:22:38 +00:00
ParDatabaseImpl {
2018-10-31 10:03:33 +00:00
runtime: self.runtime.fork_mut(),
2018-10-13 09:22:38 +00:00
knobs: self.knobs.clone(),
}
}
}
impl Knobs for ParDatabaseImpl {
fn knobs(&self) -> &KnobsStruct {
&self.knobs
2018-10-13 09:22:38 +00:00
}
fn signal(&self, stage: usize) {
self.knobs.signal.signal(stage);
2018-10-13 09:22:38 +00:00
}
fn wait_for(&self, stage: usize) {
self.knobs.signal.wait_for(stage);
2018-10-13 09:22:38 +00:00
}
}
salsa::database_storage! {
pub struct DatabaseImplStorage for ParDatabaseImpl {
impl ParDatabase {
fn input() for Input;
fn sum() for Sum;
fn sum2() for Sum2;
2018-10-13 09:22:38 +00:00
}
}
}