From 6f3d69b336288ee74f486672755b52c4c0e6e7f5 Mon Sep 17 00:00:00 2001 From: Austin Kiekintveld Date: Mon, 25 Apr 2022 15:00:58 -0700 Subject: [PATCH] Remove SeqCst usage from Arc Prior to this commit, there were multiple loads from the weak and strong counts that used SeqCst ordering. For plain loads, SeqCst behaves like Acquire with the additional guarantee that all *sequentially consistent* operations are observed in the same order by all threads. However, this additional guarantee does not apply here because we never do any store or load-with-store operations that use SeqCst ordering on either of these counts. This commit relaxes the ordering to Acquire to better reflect the requirements and guarantees of this code to the reader, compiler, and hardware. --- library/alloc/src/sync.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index a19999cd72580..a6e5c9760ceff 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -25,7 +25,7 @@ use core::ptr::{self, NonNull}; #[cfg(not(no_global_oom_handling))] use core::slice::from_raw_parts_mut; use core::sync::atomic; -use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; +use core::sync::atomic::Ordering::{Acquire, Relaxed, Release}; #[cfg(not(no_global_oom_handling))] use crate::alloc::handle_alloc_error; @@ -971,7 +971,7 @@ impl Arc { #[must_use] #[stable(feature = "arc_counts", since = "1.15.0")] pub fn weak_count(this: &Self) -> usize { - let cnt = this.inner().weak.load(SeqCst); + let cnt = this.inner().weak.load(Acquire); // If the weak count is currently locked, the value of the // count was 0 just before taking the lock. if cnt == usize::MAX { 0 } else { cnt - 1 } @@ -1001,7 +1001,7 @@ impl Arc { #[must_use] #[stable(feature = "arc_counts", since = "1.15.0")] pub fn strong_count(this: &Self) -> usize { - this.inner().strong.load(SeqCst) + this.inner().strong.load(Acquire) } /// Increments the strong reference count on the `Arc` associated with the @@ -1963,7 +1963,7 @@ impl Weak { #[must_use] #[stable(feature = "weak_counts", since = "1.41.0")] pub fn strong_count(&self) -> usize { - if let Some(inner) = self.inner() { inner.strong.load(SeqCst) } else { 0 } + if let Some(inner) = self.inner() { inner.strong.load(Acquire) } else { 0 } } /// Gets an approximation of the number of `Weak` pointers pointing to this @@ -1982,8 +1982,8 @@ impl Weak { pub fn weak_count(&self) -> usize { self.inner() .map(|inner| { - let weak = inner.weak.load(SeqCst); - let strong = inner.strong.load(SeqCst); + let weak = inner.weak.load(Acquire); + let strong = inner.strong.load(Acquire); if strong == 0 { 0 } else {