Skip to content

Commit e209e85

Browse files
committed
Auto merge of rust-lang#95183 - ibraheemdev:arc-count-acquire, r=Amanieu
Weaken needlessly restrictive orderings on `Arc::*_count` There is no apparent reason for these to be `SeqCst`. For reference, [the Boost C++ implementation relies on acquire semantics](https://github.com/boostorg/smart_ptr/blob/f2cc84a23c64b8a73c9b72b34799d0854d7e0787/include/boost/smart_ptr/detail/sp_counted_base_std_atomic.hpp#L137-L140).
2 parents 9a25164 + 7e93032 commit e209e85

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

library/alloc/src/sync.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::ptr::{self, NonNull};
2525
#[cfg(not(no_global_oom_handling))]
2626
use core::slice::from_raw_parts_mut;
2727
use core::sync::atomic;
28-
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
28+
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
2929

3030
#[cfg(not(no_global_oom_handling))]
3131
use crate::alloc::handle_alloc_error;
@@ -984,7 +984,7 @@ impl<T: ?Sized> Arc<T> {
984984
#[must_use]
985985
#[stable(feature = "arc_counts", since = "1.15.0")]
986986
pub fn weak_count(this: &Self) -> usize {
987-
let cnt = this.inner().weak.load(SeqCst);
987+
let cnt = this.inner().weak.load(Acquire);
988988
// If the weak count is currently locked, the value of the
989989
// count was 0 just before taking the lock.
990990
if cnt == usize::MAX { 0 } else { cnt - 1 }
@@ -1014,7 +1014,7 @@ impl<T: ?Sized> Arc<T> {
10141014
#[must_use]
10151015
#[stable(feature = "arc_counts", since = "1.15.0")]
10161016
pub fn strong_count(this: &Self) -> usize {
1017-
this.inner().strong.load(SeqCst)
1017+
this.inner().strong.load(Acquire)
10181018
}
10191019

10201020
/// Increments the strong reference count on the `Arc<T>` associated with the
@@ -1976,7 +1976,7 @@ impl<T: ?Sized> Weak<T> {
19761976
#[must_use]
19771977
#[stable(feature = "weak_counts", since = "1.41.0")]
19781978
pub fn strong_count(&self) -> usize {
1979-
if let Some(inner) = self.inner() { inner.strong.load(SeqCst) } else { 0 }
1979+
if let Some(inner) = self.inner() { inner.strong.load(Acquire) } else { 0 }
19801980
}
19811981

19821982
/// Gets an approximation of the number of `Weak` pointers pointing to this
@@ -1995,8 +1995,8 @@ impl<T: ?Sized> Weak<T> {
19951995
pub fn weak_count(&self) -> usize {
19961996
self.inner()
19971997
.map(|inner| {
1998-
let weak = inner.weak.load(SeqCst);
1999-
let strong = inner.strong.load(SeqCst);
1998+
let weak = inner.weak.load(Acquire);
1999+
let strong = inner.strong.load(Acquire);
20002000
if strong == 0 {
20012001
0
20022002
} else {

0 commit comments

Comments
 (0)