-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters