rename fork to fork_mut

This commit is contained in:
Niko Matsakis 2018-10-31 06:03:33 -04:00
parent 2d8dbce6c1
commit 9cac418ac8
9 changed files with 54 additions and 49 deletions

View file

@ -183,7 +183,7 @@ pub trait ParallelDatabase: Database + Send {
/// **Warning.** This second handle is intended to be used from a
/// separate thread. Using two database handles from the **same
/// thread** can lead to deadlock.
fn fork(&self) -> Self;
fn fork_mut(&self) -> Self;
}
pub trait Query<DB: Database>: Debug + Default + Sized + 'static {

View file

@ -69,7 +69,7 @@ where
/// **Warning.** This second handle is intended to be used from a
/// separate thread. Using two database handles from the **same
/// thread** can lead to deadlock.
pub fn fork(&self) -> Self {
pub fn fork_mut(&self) -> Self {
Runtime {
id: RuntimeId {
counter: self.shared_state.next_id.fetch_add(1, Ordering::SeqCst),

View file

@ -14,7 +14,7 @@ fn in_par_get_set_cancellation() {
db.query(Input).set('d', 0);
let thread1 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
let v1 = db.knobs().sum_signal_on_entry.with_value(1, || {
db.knobs()
@ -38,7 +38,7 @@ fn in_par_get_set_cancellation() {
});
let thread2 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
// Wait until we have entered `sum` in the other thread.
db.wait_for(1);
@ -68,7 +68,7 @@ fn in_par_get_set_transitive_cancellation() {
db.query(Input).set('d', 0);
let thread1 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
let v1 = db.knobs().sum_signal_on_entry.with_value(1, || {
db.knobs()
@ -92,7 +92,7 @@ fn in_par_get_set_transitive_cancellation() {
});
let thread2 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
// Wait until we have entered `sum` in the other thread.
db.wait_for(1);

View file

@ -6,7 +6,7 @@ use salsa::{Database, ParallelDatabase};
#[test]
fn in_par_two_independent_queries() {
let db1 = ParDatabaseImpl::default();
let db2 = db1.fork();
let db2 = db1.fork_mut();
db1.query(Input).set('a', 100);
db1.query(Input).set('b', 010);

View file

@ -6,7 +6,7 @@ use salsa::{Database, ParallelDatabase};
#[test]
fn in_par_get_set_race() {
let db1 = ParDatabaseImpl::default();
let db2 = db1.fork();
let db2 = db1.fork_mut();
db1.query(Input).set('a', 100);
db1.query(Input).set('b', 010);

View file

@ -16,7 +16,7 @@ fn in_par_get_set_cancellation() {
let lock = db.salsa_runtime().lock_revision();
let thread1 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
let signal = signal.clone();
move || {
// Check that cancellation flag is not yet set, because
@ -47,7 +47,7 @@ fn in_par_get_set_cancellation() {
});
let thread2 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
let signal = signal.clone();
move || {
// Wait until thread 1 has asserted that they are not cancelled

View file

@ -131,9 +131,9 @@ impl Database for ParDatabaseImpl {
}
impl ParallelDatabase for ParDatabaseImpl {
fn fork(&self) -> Self {
fn fork_mut(&self) -> Self {
ParDatabaseImpl {
runtime: self.runtime.fork(),
runtime: self.runtime.fork_mut(),
knobs: self.knobs.clone(),
}
}

View file

@ -38,7 +38,7 @@ fn c(db: &impl StressDatabase, key: usize) -> Cancelable<usize> {
#[derive(Default)]
struct StressDatabaseImpl {
runtime: salsa::Runtime<StressDatabaseImpl>
runtime: salsa::Runtime<StressDatabaseImpl>,
}
impl salsa::Database for StressDatabaseImpl {
@ -48,8 +48,10 @@ impl salsa::Database for StressDatabaseImpl {
}
impl salsa::ParallelDatabase for StressDatabaseImpl {
fn fork(&self) -> StressDatabaseImpl {
StressDatabaseImpl { runtime: self.runtime.fork() }
fn fork_mut(&self) -> StressDatabaseImpl {
StressDatabaseImpl {
runtime: self.runtime.fork_mut(),
}
}
}
@ -64,7 +66,11 @@ salsa::database_storage! {
}
#[derive(Clone, Copy, Debug)]
enum Query { A, B, C }
enum Query {
A,
B,
C,
}
#[derive(Debug)]
enum Op {
@ -111,32 +117,28 @@ fn db_thread(db: StressDatabaseImpl, ops: Vec<Op>) {
Op::SetA(key, value) => {
db.query(A).set(key, value);
}
Op::Get(query, key) => {
match query {
Query::A => {
db.a(key);
},
Query::B => {
let _ = db.b(key);
},
Query::C => {
let _ = db.c(key);
},
Op::Get(query, key) => match query {
Query::A => {
db.a(key);
}
}
Op::Gc(query, strategy) => {
match query {
Query::A => {
db.query(A).sweep(strategy);
},
Query::B => {
db.query(B).sweep(strategy);
},
Query::C => {
db.query(C).sweep(strategy);
},
Query::B => {
let _ = db.b(key);
}
}
Query::C => {
let _ = db.c(key);
}
},
Op::Gc(query, strategy) => match query {
Query::A => {
db.query(A).sweep(strategy);
}
Query::B => {
db.query(B).sweep(strategy);
}
Query::C => {
db.query(C).sweep(strategy);
}
},
Op::GcAll(strategy) => {
db.sweep_all(strategy);
}
@ -151,17 +153,20 @@ fn random_ops(n_ops: usize) -> Vec<Op> {
#[test]
fn stress_test() {
let db = StressDatabaseImpl::default();
let db = StressDatabaseImpl::default();
for i in 0..10 {
db.query(A).set(i, i);
}
let n_threads = 20;
let n_ops = 100;
let ops = (0..n_threads).map(|_| random_ops(n_ops));
let threads = ops.into_iter().map(|ops| {
let db = db.fork();
std::thread::spawn(move || db_thread(db, ops))
}).collect::<Vec<_>>();
let threads = ops
.into_iter()
.map(|ops| {
let db = db.fork_mut();
std::thread::spawn(move || db_thread(db, ops))
})
.collect::<Vec<_>>();
std::mem::drop(db);
for thread in threads {
thread.join().unwrap();

View file

@ -15,7 +15,7 @@ fn true_parallel_different_keys() {
// Thread 1 will signal stage 1 when it enters and wait for stage 2.
let thread1 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
let v = db.knobs().sum_signal_on_entry.with_value(1, || {
db.knobs()
@ -29,7 +29,7 @@ fn true_parallel_different_keys() {
// Thread 2 will wait_for stage 1 when it enters and signal stage 2
// when it leaves.
let thread2 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
let v = db.knobs().sum_wait_for_on_entry.with_value(1, || {
db.knobs().sum_signal_on_exit.with_value(2, || db.sum("b"))
@ -55,7 +55,7 @@ fn true_parallel_same_keys() {
// Thread 1 will wait_for a barrier in the start of `sum`
let thread1 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
let v = db.knobs().sum_signal_on_entry.with_value(1, || {
db.knobs()
@ -71,7 +71,7 @@ fn true_parallel_same_keys() {
// continue. This way, we test out the mechanism of one thread
// blocking on another.
let thread2 = std::thread::spawn({
let db = db.fork();
let db = db.fork_mut();
move || {
db.knobs().signal.wait_for(1);
db.knobs().signal_on_will_block.set(2);