@@ -1368,9 +1368,8 @@ mod tests {
1368
1368
use std:: mem:: size_of_val;
1369
1369
use std:: path:: Path ;
1370
1370
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
1371
- use std:: sync:: Arc ;
1372
- use std:: thread:: { sleep, spawn} ;
1373
- use std:: time:: Duration ;
1371
+ use std:: sync:: { Arc , Barrier } ;
1372
+ use std:: thread:: spawn;
1374
1373
1375
1374
use matches:: assert_matches;
1376
1375
use vmm_sys_util:: tempfile:: TempFile ;
@@ -1563,36 +1562,20 @@ mod tests {
1563
1562
let a = VecMem :: new ( 1 ) ;
1564
1563
let a_clone = a. clone ( ) ;
1565
1564
let v_ref = a. get_ref :: < u8 > ( 0 ) . unwrap ( ) ;
1565
+ let barrier = Arc :: new ( Barrier :: new ( 2 ) ) ;
1566
+ let barrier1 = barrier. clone ( ) ;
1567
+
1566
1568
v_ref. store ( 99 ) ;
1567
1569
spawn ( move || {
1568
- sleep ( Duration :: from_millis ( 10 ) ) ;
1570
+ barrier1 . wait ( ) ;
1569
1571
let clone_v_ref = a_clone. get_ref :: < u8 > ( 0 ) . unwrap ( ) ;
1570
1572
clone_v_ref. store ( 0 ) ;
1573
+ barrier1. wait ( ) ;
1571
1574
} ) ;
1572
1575
1573
- // Technically this is a race condition but we have to observe the v_ref's value changing
1574
- // somehow and this helps to ensure the sleep actually happens before the store rather then
1575
- // being reordered by the compiler.
1576
1576
assert_eq ! ( v_ref. load( ) , 99 ) ;
1577
-
1578
- // Granted we could have a machine that manages to perform this many volatile loads in the
1579
- // amount of time the spawned thread sleeps, but the most likely reason the retry limit will
1580
- // get reached is because v_ref.load() is not actually performing the required volatile read
1581
- // or v_ref.store() is not doing a volatile write. A timer based solution was avoided
1582
- // because that might use a syscall which could hint the optimizer to reload v_ref's pointer
1583
- // regardless of volatile status. Note that we use a longer retry duration for optimized
1584
- // builds.
1585
- #[ cfg( debug_assertions) ]
1586
- const RETRY_MAX : usize = 500_000_000 ;
1587
- #[ cfg( not( debug_assertions) ) ]
1588
- const RETRY_MAX : usize = 10_000_000_000 ;
1589
-
1590
- let mut retry = 0 ;
1591
- while v_ref. load ( ) == 99 && retry < RETRY_MAX {
1592
- retry += 1 ;
1593
- }
1594
-
1595
- assert_ne ! ( retry, RETRY_MAX , "maximum retry exceeded" ) ;
1577
+ barrier. wait ( ) ;
1578
+ barrier. wait ( ) ;
1596
1579
assert_eq ! ( v_ref. load( ) , 0 ) ;
1597
1580
}
1598
1581
0 commit comments