From df4457e20bb6017de97ff69af58a0a92a3a5b423 Mon Sep 17 00:00:00 2001 From: Austin Kiekintveld Date: Sun, 1 May 2022 16:46:19 -0700 Subject: [PATCH] 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. --- library/std/src/sys_common/condvar/check.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys_common/condvar/check.rs b/library/std/src/sys_common/condvar/check.rs index 7671850ac55b..69cad21b0960 100644 --- a/library/std/src/sys_common/condvar/check.rs +++ b/library/std/src/sys_common/condvar/check.rs @@ -24,7 +24,7 @@ impl SameMutexCheck { } pub fn verify(&self, mutex: &MovableMutex) { let addr = mutex.raw() as *const imp::Mutex as *const () as *mut _; - match self.addr.compare_exchange(ptr::null_mut(), addr, Ordering::SeqCst, Ordering::SeqCst) + match self.addr.compare_exchange(ptr::null_mut(), addr, Ordering::Relaxed, Ordering::Relaxed) { Ok(_) => {} // Stored the address Err(n) if n == addr => {} // Lost a race to store the same address