Skip to content

Commit

Permalink
another parallel test scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Niko Matsakis authored and carljm committed Dec 16, 2024
1 parent b2d4d92 commit 670f88b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tests/parallel/cycle_a_t1_b_t2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! Test a specific cycle scenario:
//!
//! ```text
//! Thread T1 Thread T2
//! --------- ---------
//! | |
//! v |
//! query_a() |
//! ^ | v
//! | +------------> query_b()
//! | |
//! +--------------------+
//! ``````
use salsa::CycleRecoveryAction;

use crate::setup::{Knobs, KnobsDatabase};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
struct CycleValue(u32);

const MIN: CycleValue = CycleValue(0);
const MAX: CycleValue = CycleValue(22);

// Signal 1: T1 has entered `query_a`
// Signal 2: T2 has entered `query_b`

#[salsa::tracked(cycle_fn=query_a_cycle_fn, cycle_initial=query_a_initial)]
fn query_a(db: &dyn KnobsDatabase) -> CycleValue {
db.signal(1);

// Wait for Thread T2 to enter `query_b` before we continue.
db.wait_for(2);

query_b(db)
}

fn query_a_cycle_fn(
_db: &dyn KnobsDatabase,
_value: &CycleValue,
_count: u32,
) -> CycleRecoveryAction<CycleValue> {
CycleRecoveryAction::Iterate
}

fn query_a_initial(_db: &dyn KnobsDatabase) -> CycleValue {
MIN
}

#[salsa::tracked]
fn query_b(db: &dyn KnobsDatabase) -> CycleValue {
// Wait for Thread T1 to enter `query_a` before we continue.
db.wait_for(1);

db.signal(2);

let a_value = query_a(db);
CycleValue(a_value.0 + 1).min(MAX)
}

#[test]
fn the_test() {
std::thread::scope(|scope| {
let db_t1 = Knobs::default();
let db_t2 = db_t1.clone();

// Thread 1:
scope.spawn(move || {
let r = query_a(&db_t1);
assert_eq!(r, MAX);
});

// Thread 2:
scope.spawn(move || {
let r = query_b(&db_t2);
assert_eq!(r, MAX);
});
});
}
1 change: 1 addition & 0 deletions tests/parallel/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod setup;

mod cycle_a_t1_b_t2;
mod cycle_ab_peeping_c;
mod parallel_cancellation;
mod parallel_map;
Expand Down
2 changes: 2 additions & 0 deletions tests/parallel/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use crate::signal::Signal;
/// a certain behavior.
#[salsa::db]
pub(crate) trait KnobsDatabase: Database {
/// Signal that we are entering stage 1.
fn signal(&self, stage: usize);

/// Wait until we reach stage `stage` (no-op if we have already reached that stage).
fn wait_for(&self, stage: usize);
}

Expand Down

0 comments on commit 670f88b

Please sign in to comment.