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;
|
2018-11-01 00:06:06 +00:00
|
|
|
use salsa::Snapshot;
|
2018-10-13 09:22:38 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
#[salsa::query_group]
|
|
|
|
pub(crate) trait ParDatabase: Knobs + salsa::ParallelDatabase {
|
|
|
|
#[salsa::input]
|
|
|
|
fn input(&self, key: char) -> usize;
|
2018-10-13 09:22:38 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
fn sum(&self, key: &'static str) -> usize;
|
2018-10-25 14:06:55 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
/// Invokes `sum`
|
|
|
|
fn sum2(&self, key: &'static str) -> usize;
|
2018-10-31 20:03:03 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
/// Invokes `sum` but doesn't really care about the result.
|
|
|
|
fn sum2_drop_sum(&self, key: &'static str) -> usize;
|
2019-01-04 18:39:18 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
/// Invokes `sum2`
|
|
|
|
fn sum3(&self, key: &'static str) -> usize;
|
2018-12-30 07:50:20 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
/// Invokes `sum2_drop_sum`
|
|
|
|
fn sum3_drop_sum(&self, key: &'static str) -> usize;
|
2019-01-04 18:39:18 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
fn snapshot_me(&self) -> ();
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:32:52 +00:00
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub(crate) struct Canceled;
|
|
|
|
|
|
|
|
impl Canceled {
|
|
|
|
fn throw() -> ! {
|
|
|
|
// Don't print backtrace
|
|
|
|
std::panic::resume_unwind(Box::new(Canceled));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 09:22:38 +00:00
|
|
|
/// Various "knobs" and utilities used by tests to force
|
|
|
|
/// a certain behavior.
|
|
|
|
pub(crate) trait Knobs {
|
2018-10-13 09:39:51 +00:00
|
|
|
fn knobs(&self) -> &KnobsStruct;
|
2018-10-13 09:22:38 +00:00
|
|
|
|
2018-10-13 09:39:51 +00:00
|
|
|
fn signal(&self, stage: usize);
|
2018-10-13 09:22:38 +00:00
|
|
|
|
2018-10-19 09:17:26 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:32:52 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub(crate) enum CancelationFlag {
|
|
|
|
Down,
|
|
|
|
Panic,
|
|
|
|
SpecialValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CancelationFlag {
|
|
|
|
fn default() -> CancelationFlag {
|
|
|
|
CancelationFlag::Down
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 09:39:51 +00:00
|
|
|
/// 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)]
|
2018-10-13 09:39:51 +00:00
|
|
|
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>,
|
|
|
|
|
2018-10-13 09:39:51 +00:00
|
|
|
/// Invocations of `sum` will signal this stage on entry.
|
|
|
|
pub(crate) sum_signal_on_entry: Cell<usize>,
|
|
|
|
|
2018-10-19 09:17:26 +00:00
|
|
|
/// Invocations of `sum` will wait for this stage on entry.
|
|
|
|
pub(crate) sum_wait_for_on_entry: Cell<usize>,
|
2018-10-13 09:39:51 +00:00
|
|
|
|
2018-10-31 05:25:00 +00:00
|
|
|
/// If true, invocations of `sum` will panic before they exit.
|
|
|
|
pub(crate) sum_should_panic: Cell<bool>,
|
|
|
|
|
2018-10-19 09:17:26 +00:00
|
|
|
/// If true, invocations of `sum` will wait for cancellation before
|
2018-10-13 09:39:51 +00:00
|
|
|
/// they exit.
|
2019-01-10 10:32:52 +00:00
|
|
|
pub(crate) sum_wait_for_cancellation: Cell<CancelationFlag>,
|
2018-10-13 09:39:51 +00:00
|
|
|
|
2018-10-19 09:17:26 +00:00
|
|
|
/// Invocations of `sum` will wait for this stage prior to exiting.
|
|
|
|
pub(crate) sum_wait_for_on_exit: Cell<usize>,
|
2018-10-13 09:39:51 +00:00
|
|
|
|
|
|
|
/// Invocations of `sum` will signal this stage prior to exiting.
|
|
|
|
pub(crate) sum_signal_on_exit: Cell<usize>,
|
2019-01-04 18:39:18 +00:00
|
|
|
|
|
|
|
/// Invocations of `sum3_drop_sum` will panic unconditionally
|
|
|
|
pub(crate) sum3_drop_sum_should_panic: Cell<bool>,
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sum(db: &impl ParDatabase, key: &'static str) -> usize {
|
|
|
|
let mut sum = 0;
|
|
|
|
|
2018-10-13 09:39:51 +00:00
|
|
|
db.signal(db.knobs().sum_signal_on_entry.get());
|
|
|
|
|
2018-10-19 09:17:26 +00:00
|
|
|
db.wait_for(db.knobs().sum_wait_for_on_entry.get());
|
2018-10-13 09:22:38 +00:00
|
|
|
|
2018-10-31 05:25:00 +00:00
|
|
|
if db.knobs().sum_should_panic.get() {
|
|
|
|
panic!("query set to panic before exit")
|
|
|
|
}
|
|
|
|
|
2018-10-13 09:22:38 +00:00
|
|
|
for ch in key.chars() {
|
|
|
|
sum += db.input(ch);
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:32:52 +00:00
|
|
|
match db.knobs().sum_wait_for_cancellation.get() {
|
|
|
|
CancelationFlag::Down => (),
|
2019-01-11 07:13:38 +00:00
|
|
|
flag => {
|
2019-01-10 10:32:52 +00:00
|
|
|
log::debug!("waiting for cancellation");
|
|
|
|
while !db.salsa_runtime().is_current_revision_canceled() {
|
|
|
|
std::thread::yield_now();
|
|
|
|
}
|
|
|
|
log::debug!("observed cancelation");
|
2019-01-11 07:13:38 +00:00
|
|
|
if flag == CancelationFlag::Panic {
|
|
|
|
Canceled::throw();
|
2019-01-10 10:32:52 +00:00
|
|
|
}
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
2019-01-04 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for cancelation and return MAX if so. Note that we check
|
|
|
|
// for cancelation *deterministically* -- but if
|
|
|
|
// `sum_wait_for_cancellation` is set, we will block
|
|
|
|
// beforehand. Deterministic execution is a requirement for valid
|
|
|
|
// salsa user code. It's also important to some tests that `sum`
|
|
|
|
// *attempts* to invoke `is_current_revision_canceled` even if we
|
|
|
|
// know it will not be canceled, because that helps us keep the
|
|
|
|
// accounting up to date.
|
|
|
|
if db.salsa_runtime().is_current_revision_canceled() {
|
2018-10-13 09:22:38 +00:00
|
|
|
return std::usize::MAX; // when we are cancelled, we return usize::MAX.
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:17:26 +00:00
|
|
|
db.wait_for(db.knobs().sum_wait_for_on_exit.get());
|
2018-10-13 09:39:51 +00:00
|
|
|
|
|
|
|
db.signal(db.knobs().sum_signal_on_exit.get());
|
|
|
|
|
2018-10-13 09:22:38 +00:00
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:06:55 +00:00
|
|
|
fn sum2(db: &impl ParDatabase, key: &'static str) -> usize {
|
2018-10-31 16:01:36 +00:00
|
|
|
db.sum(key)
|
2018-10-25 14:06:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 18:39:18 +00:00
|
|
|
fn sum2_drop_sum(db: &impl ParDatabase, key: &'static str) -> usize {
|
|
|
|
let _ = db.sum(key);
|
|
|
|
22
|
|
|
|
}
|
|
|
|
|
2018-12-30 07:50:20 +00:00
|
|
|
fn sum3(db: &impl ParDatabase, key: &'static str) -> usize {
|
|
|
|
db.sum2(key)
|
|
|
|
}
|
|
|
|
|
2019-01-04 18:39:18 +00:00
|
|
|
fn sum3_drop_sum(db: &impl ParDatabase, key: &'static str) -> usize {
|
|
|
|
if db.knobs().sum3_drop_sum_should_panic.get() {
|
|
|
|
panic!("sum3_drop_sum executed")
|
|
|
|
}
|
|
|
|
db.sum2_drop_sum(key)
|
|
|
|
}
|
|
|
|
|
2018-11-01 00:05:31 +00:00
|
|
|
fn snapshot_me(db: &impl ParDatabase) {
|
2018-10-31 20:03:03 +00:00
|
|
|
// this should panic
|
2018-11-01 00:05:31 +00:00
|
|
|
db.snapshot();
|
2018-10-31 20:03:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 09:22:38 +00:00
|
|
|
#[derive(Default)]
|
2019-01-23 02:53:06 +00:00
|
|
|
pub(crate) struct ParDatabaseImpl {
|
2018-10-13 09:22:38 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 10:32:52 +00:00
|
|
|
|
|
|
|
fn on_propagated_panic(&self) -> ! {
|
|
|
|
Canceled::throw()
|
|
|
|
}
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParallelDatabase for ParDatabaseImpl {
|
2018-11-01 00:06:06 +00:00
|
|
|
fn snapshot(&self) -> Snapshot<Self> {
|
|
|
|
Snapshot::new(ParDatabaseImpl {
|
2018-11-01 00:05:31 +00:00
|
|
|
runtime: self.runtime.snapshot(self),
|
2018-10-13 09:22:38 +00:00
|
|
|
knobs: self.knobs.clone(),
|
2018-10-31 16:01:36 +00:00
|
|
|
})
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Knobs for ParDatabaseImpl {
|
2018-10-13 09:39:51 +00:00
|
|
|
fn knobs(&self) -> &KnobsStruct {
|
|
|
|
&self.knobs
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 09:39:51 +00:00
|
|
|
fn signal(&self, stage: usize) {
|
|
|
|
self.knobs.signal.signal(stage);
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:17:26 +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! {
|
2019-01-23 02:53:06 +00:00
|
|
|
pub(crate) struct DatabaseImplStorage for ParDatabaseImpl {
|
2018-10-13 09:22:38 +00:00
|
|
|
impl ParDatabase {
|
2019-01-12 10:11:59 +00:00
|
|
|
fn input() for InputQuery;
|
|
|
|
fn sum() for SumQuery;
|
|
|
|
fn sum2() for Sum2Query;
|
|
|
|
fn sum2_drop_sum() for Sum2DropSumQuery;
|
|
|
|
fn sum3() for Sum3Query;
|
|
|
|
fn sum3_drop_sum() for Sum3DropSumQuery;
|
|
|
|
fn snapshot_me() for SnapshotMeQuery;
|
2018-10-13 09:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|