Skip to content

Commit a3e381a

Browse files
committed
add support for loom tests
1 parent b2b82bc commit a3e381a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+546
-207
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ jobs:
5959
run: cargo test --workspace --all-features --doc
6060
- name: Check (without default features)
6161
run: cargo check --workspace --no-default-features
62+
- name: Check (loom)
63+
run: RUSTFLAGs="--cfg loom" cargo check --workspace
6264

6365
miri:
6466
name: Miri

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rayon = { version = "1.10.0", optional = true }
3030
# Stuff we want Update impls for by default
3131
compact_str = { version = "0.9", optional = true }
3232
thin-vec = "0.2.13"
33+
loom = "0.7.2"
3334

3435
[features]
3536
default = ["salsa_unstable", "rayon", "macros"]
@@ -65,6 +66,9 @@ half = "=2.4.1"
6566
[target.'cfg(all(not(target_os = "windows"), not(target_os = "openbsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dev-dependencies]
6667
tikv-jemallocator = "0.6.0"
6768

69+
[lints.rust]
70+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] }
71+
6872
[[bench]]
6973
name = "compare"
7074
harness = false

components/salsa-macro-rules/src/setup_accumulator_impl.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ macro_rules! setup_accumulator_impl {
2121
use salsa::plumbing as $zalsa;
2222
use salsa::plumbing::accumulator as $zalsa_struct;
2323

24-
static $CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Struct>> =
25-
$zalsa::IngredientCache::new();
26-
24+
// Suppress the lint against `cfg(loom)`.
25+
#[allow(unexpected_cfgs)]
2726
fn $ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<$Struct> {
27+
$zalsa::__maybe_lazy_static! {
28+
static $CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Struct>> =
29+
$zalsa::IngredientCache::new();
30+
}
31+
2832
let zalsa = db.zalsa();
2933
$CACHE.get_or_create(zalsa, || {
3034
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>()

components/salsa-macro-rules/src/setup_input_struct.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@ macro_rules! setup_input_struct {
9191
}
9292

9393
impl $Configuration {
94+
// Suppress the lint against `cfg(loom)`.
95+
#[allow(unexpected_cfgs)]
9496
pub fn ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<Self> {
95-
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
96-
$zalsa::IngredientCache::new();
97+
zalsa_::__maybe_lazy_static! {
98+
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
99+
$zalsa::IngredientCache::new();
100+
}
101+
97102
let zalsa = db.zalsa();
98103
CACHE.get_or_create(zalsa, || {
99104
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()

components/salsa-macro-rules/src/setup_interned_struct.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,17 @@ macro_rules! setup_interned_struct {
131131
}
132132

133133
impl $Configuration {
134+
// Suppress the lint against `cfg(loom)`.
135+
#[allow(unexpected_cfgs)]
134136
pub fn ingredient<Db>(db: &Db) -> &$zalsa_struct::IngredientImpl<Self>
135137
where
136138
Db: ?Sized + $zalsa::Database,
137139
{
138-
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
139-
$zalsa::IngredientCache::new();
140+
$zalsa::__maybe_lazy_static! {
141+
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
142+
$zalsa::IngredientCache::new();
143+
}
144+
140145
let zalsa = db.zalsa();
141146
CACHE.get_or_create(zalsa, || {
142147
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()

components/salsa-macro-rules/src/setup_tracked_fn.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ macro_rules! setup_tracked_fn {
7474
) => {
7575
// Suppress this clippy lint because we sometimes require `'db` where the ordinary Rust rules would not.
7676
#[allow(clippy::needless_lifetimes)]
77+
// Suppress the lint against `cfg(loom)`.
78+
#[allow(unexpected_cfgs)]
7779
$(#[$attr])*
7880
$vis fn $fn_name<$db_lt>(
7981
$db: &$db_lt dyn $Db,
@@ -89,8 +91,10 @@ macro_rules! setup_tracked_fn {
8991

9092
struct $Configuration;
9193

92-
static $FN_CACHE: $zalsa::IngredientCache<$zalsa::function::IngredientImpl<$Configuration>> =
93-
$zalsa::IngredientCache::new();
94+
$zalsa::__maybe_lazy_static! {
95+
static $FN_CACHE: $zalsa::IngredientCache<$zalsa::function::IngredientImpl<$Configuration>> =
96+
$zalsa::IngredientCache::new();
97+
}
9498

9599
$zalsa::macro_if! {
96100
if $needs_interner {
@@ -100,8 +104,10 @@ macro_rules! setup_tracked_fn {
100104
std::marker::PhantomData<&$db_lt $zalsa::interned::Value<$Configuration>>,
101105
);
102106

103-
static $INTERN_CACHE: $zalsa::IngredientCache<$zalsa::interned::IngredientImpl<$Configuration>> =
104-
$zalsa::IngredientCache::new();
107+
$zalsa::__maybe_lazy_static! {
108+
static $INTERN_CACHE: $zalsa::IngredientCache<$zalsa::interned::IngredientImpl<$Configuration>> =
109+
$zalsa::IngredientCache::new();
110+
}
105111

106112
impl $zalsa::SalsaStructInDb for $InternedData<'_> {
107113
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;

components/salsa-macro-rules/src/setup_tracked_struct.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ macro_rules! setup_tracked_struct {
169169
}
170170

171171
impl $Configuration {
172+
// Suppress the lint against `cfg(loom)`.
173+
#[allow(unexpected_cfgs)]
172174
pub fn ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<$Configuration> {
173-
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
174-
$zalsa::IngredientCache::new();
175+
$zalsa::__maybe_lazy_static! {
176+
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
177+
$zalsa::IngredientCache::new();
178+
}
175179

176180
let zalsa = db.zalsa();
177181
CACHE.get_or_create(zalsa, || {

src/accumulator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use std::any::{Any, TypeId};
44
use std::fmt;
55
use std::marker::PhantomData;
66
use std::panic::UnwindSafe;
7-
use std::sync::Arc;
87

98
use accumulated::{Accumulated, AnyAccumulated};
109

1110
use crate::function::VerifyResult;
1211
use crate::ingredient::{Ingredient, Jar};
12+
use crate::loom::sync::Arc;
1313
use crate::plumbing::IngredientIndices;
1414
use crate::table::memo::MemoTableTypes;
1515
use crate::zalsa::{IngredientIndex, Zalsa};

src/accumulator/accumulated_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::ops;
2-
use std::sync::atomic::{AtomicBool, Ordering};
32

43
use rustc_hash::FxHashMap;
54

65
use crate::accumulator::accumulated::Accumulated;
76
use crate::accumulator::{Accumulator, AnyAccumulated};
7+
use crate::loom::sync::atomic::{AtomicBool, Ordering};
88
use crate::IngredientIndex;
99

1010
#[derive(Default)]

src/active_query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::ops::Not;
2-
use std::sync::atomic::AtomicBool;
32
use std::{fmt, mem, ops};
43

54
use crate::accumulator::accumulated_map::{
@@ -9,6 +8,7 @@ use crate::cycle::CycleHeads;
98
use crate::durability::Durability;
109
use crate::hash::FxIndexSet;
1110
use crate::key::DatabaseKeyIndex;
11+
use crate::loom::sync::atomic::AtomicBool;
1212
use crate::runtime::Stamp;
1313
use crate::tracked_struct::{Disambiguator, DisambiguatorMap, IdentityHash, IdentityMap};
1414
use crate::zalsa_local::{QueryEdge, QueryEdges, QueryOrigin, QueryRevisions};

src/attach.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
use std::cell::Cell;
21
use std::ptr::NonNull;
32

3+
use crate::loom::cell::Cell;
44
use crate::Database;
55

6-
thread_local! {
6+
#[cfg(loom)]
7+
crate::loom::thread_local! {
8+
/// The thread-local state salsa requires for a given thread
9+
static ATTACHED: Attached = Attached::new();
10+
}
11+
12+
// loom's `thread_local` macro does not support const-initialization.
13+
#[cfg(not(loom))]
14+
crate::loom::thread_local! {
715
/// The thread-local state salsa requires for a given thread
816
static ATTACHED: Attached = const { Attached::new() }
917
}
@@ -20,6 +28,14 @@ struct Attached {
2028
}
2129

2230
impl Attached {
31+
#[cfg(loom)]
32+
fn new() -> Self {
33+
Self {
34+
database: Cell::new(None),
35+
}
36+
}
37+
38+
#[cfg(not(loom))]
2339
const fn new() -> Self {
2440
Self {
2541
database: Cell::new(None),

src/cycle.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,15 @@ impl From<CycleHead> for CycleHeads {
193193
}
194194
}
195195

196+
#[cfg(not(loom))]
196197
pub(crate) static EMPTY_CYCLE_HEADS: std::sync::LazyLock<CycleHeads> =
197198
std::sync::LazyLock::new(|| CycleHeads(ThinVec::new()));
198199

200+
#[cfg(loom)]
201+
loom::lazy_static! {
202+
pub(crate) static ref EMPTY_CYCLE_HEADS: CycleHeads = CycleHeads(ThinVec::new());
203+
}
204+
199205
#[derive(Debug, PartialEq, Eq)]
200206
pub enum CycleHeadKind {
201207
Provisional,

src/event.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::thread::ThreadId;
2-
31
use crate::key::DatabaseKeyIndex;
2+
use crate::loom::thread::{self, ThreadId};
43
use crate::Revision;
54

65
/// The `Event` struct identifies various notable things that can
@@ -18,7 +17,7 @@ pub struct Event {
1817
impl Event {
1918
pub fn new(kind: EventKind) -> Self {
2019
Self {
21-
thread_id: std::thread::current().id(),
20+
thread_id: thread::current().id(),
2221
kind,
2322
}
2423
}

src/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::any::Any;
22
use std::fmt;
33
use std::ptr::NonNull;
4-
use std::sync::Arc;
54

65
pub(crate) use maybe_changed_after::VerifyResult;
76

@@ -11,6 +10,7 @@ use crate::function::delete::DeletedEntries;
1110
use crate::function::sync::{ClaimResult, SyncTable};
1211
use crate::ingredient::Ingredient;
1312
use crate::key::DatabaseKeyIndex;
13+
use crate::loom::sync::Arc;
1414
use crate::plumbing::MemoIngredientMap;
1515
use crate::salsa_struct::SalsaStructInDb;
1616
use crate::table::memo::MemoTableTypes;

src/function/execute.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::sync::atomic::{AtomicBool, Ordering};
2-
31
use crate::cycle::{CycleRecoveryStrategy, MAX_ITERATIONS};
42
use crate::function::memo::Memo;
53
use crate::function::{Configuration, IngredientImpl};
4+
use crate::loom::sync::atomic::{AtomicBool, Ordering};
65
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
76
use crate::zalsa_local::{ActiveQueryGuard, QueryRevisions};
87
use crate::{Database, Event, EventKind, Id, Revision};

src/function/fetch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::cycle::{CycleHeads, CycleRecoveryStrategy};
22
use crate::function::memo::Memo;
33
use crate::function::sync::ClaimResult;
44
use crate::function::{Configuration, IngredientImpl, VerifyResult};
5+
use crate::loom::sync::AtomicMut;
56
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
67
use crate::zalsa_local::QueryRevisions;
78
use crate::Id;
@@ -160,7 +161,7 @@ where
160161
let mut revisions = active_query.pop();
161162
revisions.cycle_heads = CycleHeads::initial(database_key_index);
162163
// We need this for `cycle_heads()` to work. We will unset this in the outer `execute()`.
163-
*revisions.verified_final.get_mut() = false;
164+
revisions.verified_final.write_mut(false);
164165
Some(self.insert_memo(
165166
zalsa,
166167
id,

src/function/lru.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::num::NonZeroUsize;
22

3-
use parking_lot::Mutex;
4-
53
use crate::hash::FxLinkedHashSet;
4+
use crate::loom::sync::Mutex;
65
use crate::Id;
76

87
pub(super) struct Lru {
@@ -14,7 +13,7 @@ impl Lru {
1413
pub fn new(cap: usize) -> Self {
1514
Self {
1615
capacity: NonZeroUsize::new(cap),
17-
set: Mutex::new(FxLinkedHashSet::default()),
16+
set: Mutex::default(),
1817
}
1918
}
2019

src/function/maybe_changed_after.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::sync::atomic::Ordering;
2-
31
use crate::accumulator::accumulated_map::InputAccumulatedValues;
42
use crate::cycle::{CycleHeadKind, CycleHeads, CycleRecoveryStrategy};
53
use crate::function::memo::Memo;
64
use crate::function::sync::ClaimResult;
75
use crate::function::{Configuration, IngredientImpl};
86
use crate::key::DatabaseKeyIndex;
7+
use crate::loom::sync::atomic::Ordering;
98
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
109
use crate::zalsa_local::{QueryEdge, QueryOrigin};
1110
use crate::{AsDynDatabase as _, Id, Revision};

src/function/memo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::any::Any;
22
use std::fmt::{Debug, Formatter};
33
use std::mem::transmute;
44
use std::ptr::NonNull;
5-
use std::sync::atomic::Ordering;
65

76
use crate::cycle::{CycleHeadKind, CycleHeads, CycleRecoveryStrategy, EMPTY_CYCLE_HEADS};
87
use crate::function::{Configuration, IngredientImpl};
98
use crate::key::DatabaseKeyIndex;
9+
use crate::loom::sync::atomic::Ordering;
1010
use crate::revision::AtomicRevision;
1111
use crate::table::memo::MemoTableWithTypesMut;
1212
use crate::zalsa::{MemoIngredientIndex, Zalsa};
@@ -111,7 +111,7 @@ pub struct Memo<V> {
111111
}
112112

113113
// Memo's are stored a lot, make sure their size is doesn't randomly increase.
114-
// #[cfg(test)]
114+
#[cfg(not(loom))]
115115
const _: [(); std::mem::size_of::<Memo<std::num::NonZeroUsize>>()] =
116116
[(); std::mem::size_of::<[usize; 13]>()];
117117

src/function/specify.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::sync::atomic::AtomicBool;
2-
1+
use crate::loom::sync::atomic::AtomicBool;
32
use crate::accumulator::accumulated_map::InputAccumulatedValues;
43
use crate::function::memo::Memo;
54
use crate::function::{Configuration, IngredientImpl};

src/function/sync.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use std::thread::ThreadId;
2-
3-
use parking_lot::Mutex;
41
use rustc_hash::FxHashMap;
52

6-
use crate::{
7-
key::DatabaseKeyIndex,
8-
runtime::{BlockResult, WaitResult},
9-
zalsa::Zalsa,
10-
Database, Id, IngredientIndex,
11-
};
3+
use crate::key::DatabaseKeyIndex;
4+
use crate::loom::sync::Mutex;
5+
use crate::loom::thread::{self, ThreadId};
6+
use crate::runtime::{BlockResult, WaitResult};
7+
use crate::zalsa::Zalsa;
8+
use crate::{Database, Id, IngredientIndex};
129

1310
/// Tracks the keys that are currently being processed; used to coordinate between
1411
/// worker threads.
@@ -71,7 +68,7 @@ impl SyncTable {
7168
}
7269
std::collections::hash_map::Entry::Vacant(vacant_entry) => {
7370
vacant_entry.insert(SyncState {
74-
id: std::thread::current().id(),
71+
id: thread::current().id(),
7572
anyone_waiting: false,
7673
});
7774
ClaimResult::Claimed(ClaimGuard {

src/ingredient.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::any::{Any, TypeId};
22
use std::fmt;
3-
use std::sync::Arc;
43

4+
use crate::loom::sync::Arc;
55
use crate::accumulator::accumulated_map::{AccumulatedMap, InputAccumulatedValues};
66
use crate::cycle::{CycleHeadKind, CycleRecoveryStrategy};
77
use crate::function::VerifyResult;

0 commit comments

Comments
 (0)