Skip to content

Commit 62aea72

Browse files
authored
Merge pull request rust-lang#4469 from Patrick-6/add-spinloop-hints
Add std::hint::spin_loop() to tests
2 parents da40065 + 9a76dde commit 62aea72

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ fn static_atomic_bool(val: bool) -> &'static AtomicBool {
4141
}
4242

4343
/// Spins until it acquires a pre-determined value.
44-
fn loads_value(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
44+
fn spin_until_i32(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
45+
while loc.load(ord) != val {
46+
std::hint::spin_loop();
47+
}
48+
val
49+
}
50+
51+
/// Spins until it acquires a pre-determined boolean.
52+
fn spin_until_bool(loc: &AtomicBool, ord: Ordering, val: bool) -> bool {
4553
while loc.load(ord) != val {
4654
std::hint::spin_loop();
4755
}
@@ -65,7 +73,7 @@ fn test_corr() {
6573
}); // | |
6674
#[rustfmt::skip] // |synchronizes-with |happens-before
6775
let j3 = spawn(move || { // | |
68-
loads_value(&y, Acquire, 1); // <------------+ |
76+
spin_until_i32(&y, Acquire, 1); // <---------+ |
6977
x.load(Relaxed) // <----------------------------------------------+
7078
// The two reads on x are ordered by hb, so they cannot observe values
7179
// differently from the modification order. If the first read observed
@@ -90,12 +98,12 @@ fn test_wrc() {
9098
}); // | |
9199
#[rustfmt::skip] // |synchronizes-with |
92100
let j2 = spawn(move || { // | |
93-
loads_value(&x, Acquire, 1); // <------------+ |
101+
spin_until_i32(&x, Acquire, 1); // <---------+ |
94102
y.store(1, Release); // ---------------------+ |happens-before
95103
}); // | |
96104
#[rustfmt::skip] // |synchronizes-with |
97105
let j3 = spawn(move || { // | |
98-
loads_value(&y, Acquire, 1); // <------------+ |
106+
spin_until_i32(&y, Acquire, 1); // <---------+ |
99107
x.load(Relaxed) // <-----------------------------------------------+
100108
});
101109

@@ -121,7 +129,7 @@ fn test_message_passing() {
121129
#[rustfmt::skip] // |synchronizes-with | happens-before
122130
let j2 = spawn(move || { // | |
123131
let x = x; // avoid field capturing | |
124-
loads_value(&y, Acquire, 1); // <------------+ |
132+
spin_until_i32(&y, Acquire, 1); // <---------+ |
125133
unsafe { *x.0 } // <---------------------------------------------+
126134
});
127135

@@ -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+
spin_until_bool(go, Relaxed, true);
220228
rdmw(y, x, z)
221229
});
222230

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

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ fn static_atomic_bool(val: bool) -> &'static AtomicBool {
2020
}
2121

2222
/// Spins until it acquires a pre-determined value.
23-
fn loads_value(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
23+
fn spin_until_i32(loc: &AtomicI32, ord: Ordering, val: i32) -> i32 {
24+
while loc.load(ord) != val {
25+
std::hint::spin_loop();
26+
}
27+
val
28+
}
29+
30+
/// Spins until it acquires a pre-determined boolean.
31+
fn spin_until_bool(loc: &AtomicBool, ord: Ordering, val: bool) -> bool {
2432
while loc.load(ord) != val {
2533
std::hint::spin_loop();
2634
}
@@ -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+
spin_until_bool(x, SeqCst, true);
6472
y.load(SeqCst)
6573
});
6674
let d = spawn(move || {
67-
while !y.load(SeqCst) {}
75+
spin_until_bool(y, SeqCst, true);
6876
x.load(SeqCst)
6977
});
7078

@@ -136,7 +144,7 @@ fn test_cpp20_rwc_syncs() {
136144
});
137145

138146
let j2 = spawn(move || {
139-
loads_value(&x, Relaxed, 1);
147+
spin_until_i32(&x, Relaxed, 1);
140148
fence(SeqCst);
141149
y.load(Relaxed)
142150
});

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn static_atomic(val: usize) -> &'static AtomicUsize {
2424
}
2525

2626
// Spins until it reads the given value
27-
fn reads_value(loc: &AtomicUsize, val: usize) -> usize {
27+
fn spin_until(loc: &AtomicUsize, val: usize) -> usize {
2828
while loc.load(Relaxed) != val {
2929
std::hint::spin_loop();
3030
}
@@ -85,7 +85,7 @@ fn initialization_write(add_fence: bool) -> bool {
8585
});
8686

8787
let j2 = spawn(move || {
88-
reads_value(wait, 1);
88+
spin_until(wait, 1);
8989
if add_fence {
9090
fence(AcqRel);
9191
}
@@ -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+
spin_until(go, 1);
123123
rdmw(y, x, z)
124124
});
125125

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

0 commit comments

Comments
 (0)