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.
This commit is contained in:
Yuki Okushi 2022-05-05 10:20:34 +09:00 committed by GitHub
commit b792258b32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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"),