Skip to content

Commit b792258

Browse files
authored
Rollup merge of #96619 - akiekintveld:same_mutex_check_relaxed_ordering, r=m-ou-se
Relax memory ordering used in SameMutexCheck `SameMutexCheck` only requires atomicity for `self.addr`, but does not need ordering of other memory accesses in either the success or failure case. Using `Relaxed`, the code still correctly handles the case when two threads race to store an address.
2 parents 8385d17 + 55a7d18 commit b792258

File tree

1 file changed

+8
-2
lines changed
  • library/std/src/sys_common/condvar

1 file changed

+8
-2
lines changed

library/std/src/sys_common/condvar/check.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ impl SameMutexCheck {
2424
}
2525
pub fn verify(&self, mutex: &MovableMutex) {
2626
let addr = mutex.raw() as *const imp::Mutex as *const () as *mut _;
27-
match self.addr.compare_exchange(ptr::null_mut(), addr, Ordering::SeqCst, Ordering::SeqCst)
28-
{
27+
// Relaxed is okay here because we never read through `self.addr`, and only use it to
28+
// compare addresses.
29+
match self.addr.compare_exchange(
30+
ptr::null_mut(),
31+
addr,
32+
Ordering::Relaxed,
33+
Ordering::Relaxed,
34+
) {
2935
Ok(_) => {} // Stored the address
3036
Err(n) if n == addr => {} // Lost a race to store the same address
3137
_ => panic!("attempted to use a condition variable with two mutexes"),

0 commit comments

Comments
 (0)