about summary refs log tree commit diff
path: root/library/std/src/sys_common
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-05-05 10:20:34 +0900
committerGitHub <noreply@github.com>2022-05-05 10:20:34 +0900
commitb792258b320fa926f31bd7b5367fd151deea25c4 (patch)
tree3c4e3807019a01d92a02238183020033ca55e4c1 /library/std/src/sys_common
parent8385d1713e2507df36f3c33a1eae5539dd6b0f0a (diff)
parent55a7d18189cfa65a35dacf63ee9e70aedd85e3c4 (diff)
downloadrust-b792258b320fa926f31bd7b5367fd151deea25c4.tar.gz
rust-b792258b320fa926f31bd7b5367fd151deea25c4.zip
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.
Diffstat (limited to 'library/std/src/sys_common')
-rw-r--r--library/std/src/sys_common/condvar/check.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/library/std/src/sys_common/condvar/check.rs b/library/std/src/sys_common/condvar/check.rs
index 7671850ac55..d0d0d596518 100644
--- a/library/std/src/sys_common/condvar/check.rs
+++ b/library/std/src/sys_common/condvar/check.rs
@@ -24,8 +24,14 @@ 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)
-        {
+        // Relaxed is okay here because we never read through `self.addr`, and only use it to
+        // compare addresses.
+        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
             _ => panic!("attempted to use a condition variable with two mutexes"),