Skip to content

Commit e1d35c5

Browse files
committed
Add std::hint::spin_loop()
1 parent 3c3f1e4 commit e1d35c5

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/tools/miri/tests/pass/0weak_memory_consistency.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ fn loads_value(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
4848
val
4949
}
5050

51+
/// Spins until it acquires a pre-determined boolean.
52+
fn loads_bool(loc: &AtomicBool, ord: Ordering, val: bool) -> bool {
53+
while loc.load(ord) != val {
54+
std::hint::spin_loop();
55+
}
56+
val
57+
}
58+
5159
fn test_corr() {
5260
let x = static_atomic(0);
5361
let y = static_atomic(0);
@@ -216,12 +224,12 @@ fn test_sync_through_rmw_and_fences() {
216224
let go = static_atomic_bool(false);
217225

218226
let t1 = spawn(move || {
219-
while !go.load(Relaxed) {}
227+
loads_bool(go, Relaxed, true);
220228
rdmw(y, x, z)
221229
});
222230

223231
let t2 = spawn(move || {
224-
while !go.load(Relaxed) {}
232+
loads_bool(go, Relaxed, true);
225233
rdmw(z, x, y)
226234
});
227235

src/tools/miri/tests/pass/0weak_memory_consistency_sc.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ fn loads_value(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
2727
val
2828
}
2929

30+
/// Spins until it acquires a pre-determined boolean.
31+
fn loads_bool(loc: &AtomicBool, ord: Ordering, val: bool) -> bool {
32+
while loc.load(ord) != val {
33+
std::hint::spin_loop();
34+
}
35+
val
36+
}
37+
3038
// Test case SB taken from Repairing Sequential Consistency in C/C++11
3139
// by Lahav et al.
3240
// https://plv.mpi-sws.org/scfix/paper.pdf
@@ -60,11 +68,11 @@ fn test_iriw_sc_rlx() {
6068
let a = spawn(move || x.store(true, Relaxed));
6169
let b = spawn(move || y.store(true, Relaxed));
6270
let c = spawn(move || {
63-
while !x.load(SeqCst) {}
71+
loads_bool(x, SeqCst, true);
6472
y.load(SeqCst)
6573
});
6674
let d = spawn(move || {
67-
while !y.load(SeqCst) {}
75+
loads_bool(y, SeqCst, true);
6876
x.load(SeqCst)
6977
});
7078

src/tools/miri/tests/pass/weak_memory/weak.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ fn faa_replaced_by_load() -> bool {
119119
let go = static_atomic(0);
120120

121121
let t1 = spawn(move || {
122-
while go.load(Relaxed) == 0 {}
122+
reads_value(go, 1);
123123
rdmw(y, x, z)
124124
});
125125

126126
let t2 = spawn(move || {
127-
while go.load(Relaxed) == 0 {}
127+
reads_value(go, 1);
128128
rdmw(z, x, y)
129129
});
130130

0 commit comments

Comments
 (0)