Skip to content

Commit 796bc7c

Browse files
authored
Rollup merge of #98383 - m-ou-se:remove-memory-order-restrictions, r=joshtriplett
Remove restrictions on compare-exchange memory ordering. We currently don't allow the failure memory ordering of compare-exchange operations to be stronger than the success ordering, as was the case in C++11 when its memory model was copied to Rust. However, this restriction was lifted in C++17 as part of [p0418r2](https://wg21.link/p0418r2). It's time we lift the restriction too. | Success | Failure | Before | After | |---------|---------|--------|-------| | Relaxed | Relaxed | ✔️ | ✔️ | | Relaxed | Acquire | ❌ | ✔️ | | Relaxed | SeqCst | ❌ | ✔️ | | Acquire | Relaxed | ✔️ | ✔️ | | Acquire | Acquire | ✔️ | ✔️ | | Acquire | SeqCst | ❌ | ✔️ | | Release | Relaxed | ✔️ | ✔️ | | Release | Acquire | ❌ | ✔️ | | Release | SeqCst | ❌ | ✔️ | | AcqRel | Relaxed | ✔️ | ✔️ | | AcqRel | Acquire | ✔️ | ✔️ | | AcqRel | SeqCst | ❌ | ✔️ | | SeqCst | Relaxed | ✔️ | ✔️ | | SeqCst | Acquire | ✔️ | ✔️ | | SeqCst | SeqCst | ✔️ | ✔️ | | \* | Release | ❌ | ❌ | | \* | AcqRel | ❌ | ❌ | Fixes #68464
2 parents 263edd4 + a898f41 commit 796bc7c

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

library/core/src/sync/atomic.rs

+35-30
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ impl AtomicBool {
581581
/// `failure` describes the required ordering for the load operation that takes place when
582582
/// the comparison fails. Using [`Acquire`] as success ordering makes the store part
583583
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
584-
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
585-
/// and must be equivalent to or weaker than the success ordering.
584+
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
586585
///
587586
/// **Note:** This method is only available on platforms that support atomic
588587
/// operations on `u8`.
@@ -640,8 +639,7 @@ impl AtomicBool {
640639
/// `failure` describes the required ordering for the load operation that takes place when
641640
/// the comparison fails. Using [`Acquire`] as success ordering makes the store part
642641
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
643-
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
644-
/// and must be equivalent to or weaker than the success ordering.
642+
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
645643
///
646644
/// **Note:** This method is only available on platforms that support atomic
647645
/// operations on `u8`.
@@ -941,8 +939,7 @@ impl AtomicBool {
941939
/// Using [`Acquire`] as success ordering makes the store part of this
942940
/// operation [`Relaxed`], and using [`Release`] makes the final successful
943941
/// load [`Relaxed`]. The (failed) load ordering can only be [`SeqCst`],
944-
/// [`Acquire`] or [`Relaxed`] and must be equivalent to or weaker than the
945-
/// success ordering.
942+
/// [`Acquire`] or [`Relaxed`].
946943
///
947944
/// **Note:** This method is only available on platforms that support atomic
948945
/// operations on `u8`.
@@ -1301,8 +1298,7 @@ impl<T> AtomicPtr<T> {
13011298
/// `failure` describes the required ordering for the load operation that takes place when
13021299
/// the comparison fails. Using [`Acquire`] as success ordering makes the store part
13031300
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
1304-
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
1305-
/// and must be equivalent to or weaker than the success ordering.
1301+
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
13061302
///
13071303
/// **Note:** This method is only available on platforms that support atomic
13081304
/// operations on pointers.
@@ -1347,8 +1343,7 @@ impl<T> AtomicPtr<T> {
13471343
/// `failure` describes the required ordering for the load operation that takes place when
13481344
/// the comparison fails. Using [`Acquire`] as success ordering makes the store part
13491345
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
1350-
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
1351-
/// and must be equivalent to or weaker than the success ordering.
1346+
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
13521347
///
13531348
/// **Note:** This method is only available on platforms that support atomic
13541349
/// operations on pointers.
@@ -1404,8 +1399,7 @@ impl<T> AtomicPtr<T> {
14041399
/// Using [`Acquire`] as success ordering makes the store part of this
14051400
/// operation [`Relaxed`], and using [`Release`] makes the final successful
14061401
/// load [`Relaxed`]. The (failed) load ordering can only be [`SeqCst`],
1407-
/// [`Acquire`] or [`Relaxed`] and must be equivalent to or weaker than the
1408-
/// success ordering.
1402+
/// [`Acquire`] or [`Relaxed`].
14091403
///
14101404
/// **Note:** This method is only available on platforms that support atomic
14111405
/// operations on pointers.
@@ -2227,8 +2221,7 @@ macro_rules! atomic_int {
22272221
/// `failure` describes the required ordering for the load operation that takes place when
22282222
/// the comparison fails. Using [`Acquire`] as success ordering makes the store part
22292223
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
2230-
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
2231-
/// and must be equivalent to or weaker than the success ordering.
2224+
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
22322225
///
22332226
/// **Note**: This method is only available on platforms that support atomic operations on
22342227
#[doc = concat!("[`", $s_int_type, "`].")]
@@ -2279,8 +2272,7 @@ macro_rules! atomic_int {
22792272
/// `failure` describes the required ordering for the load operation that takes place when
22802273
/// the comparison fails. Using [`Acquire`] as success ordering makes the store part
22812274
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
2282-
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
2283-
/// and must be equivalent to or weaker than the success ordering.
2275+
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
22842276
///
22852277
/// **Note**: This method is only available on platforms that support atomic operations on
22862278
#[doc = concat!("[`", $s_int_type, "`].")]
@@ -2517,8 +2509,7 @@ macro_rules! atomic_int {
25172509
///
25182510
/// Using [`Acquire`] as success ordering makes the store part
25192511
/// of this operation [`Relaxed`], and using [`Release`] makes the final successful load
2520-
/// [`Relaxed`]. The (failed) load ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
2521-
/// and must be equivalent to or weaker than the success ordering.
2512+
/// [`Relaxed`]. The (failed) load ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`].
25222513
///
25232514
/// **Note**: This method is only available on platforms that support atomic operations on
25242515
#[doc = concat!("[`", $s_int_type, "`].")]
@@ -3035,22 +3026,29 @@ unsafe fn atomic_compare_exchange<T: Copy>(
30353026
let (val, ok) = unsafe {
30363027
match (success, failure) {
30373028
(Relaxed, Relaxed) => intrinsics::atomic_cxchg_relaxed_relaxed(dst, old, new),
3038-
//(Relaxed, Acquire) => intrinsics::atomic_cxchg_relaxed_acquire(dst, old, new),
3039-
//(Relaxed, SeqCst) => intrinsics::atomic_cxchg_relaxed_seqcst(dst, old, new),
3029+
#[cfg(not(bootstrap))]
3030+
(Relaxed, Acquire) => intrinsics::atomic_cxchg_relaxed_acquire(dst, old, new),
3031+
#[cfg(not(bootstrap))]
3032+
(Relaxed, SeqCst) => intrinsics::atomic_cxchg_relaxed_seqcst(dst, old, new),
30403033
(Acquire, Relaxed) => intrinsics::atomic_cxchg_acquire_relaxed(dst, old, new),
30413034
(Acquire, Acquire) => intrinsics::atomic_cxchg_acquire_acquire(dst, old, new),
3042-
//(Acquire, SeqCst) => intrinsics::atomic_cxchg_acquire_seqcst(dst, old, new),
3035+
#[cfg(not(bootstrap))]
3036+
(Acquire, SeqCst) => intrinsics::atomic_cxchg_acquire_seqcst(dst, old, new),
30433037
(Release, Relaxed) => intrinsics::atomic_cxchg_release_relaxed(dst, old, new),
3044-
//(Release, Acquire) => intrinsics::atomic_cxchg_release_acquire(dst, old, new),
3045-
//(Release, SeqCst) => intrinsics::atomic_cxchg_release_seqcst(dst, old, new),
3038+
#[cfg(not(bootstrap))]
3039+
(Release, Acquire) => intrinsics::atomic_cxchg_release_acquire(dst, old, new),
3040+
#[cfg(not(bootstrap))]
3041+
(Release, SeqCst) => intrinsics::atomic_cxchg_release_seqcst(dst, old, new),
30463042
(AcqRel, Relaxed) => intrinsics::atomic_cxchg_acqrel_relaxed(dst, old, new),
30473043
(AcqRel, Acquire) => intrinsics::atomic_cxchg_acqrel_acquire(dst, old, new),
3048-
//(AcqRel, SeqCst) => intrinsics::atomic_cxchg_acqrel_seqcst(dst, old, new),
3044+
#[cfg(not(bootstrap))]
3045+
(AcqRel, SeqCst) => intrinsics::atomic_cxchg_acqrel_seqcst(dst, old, new),
30493046
(SeqCst, Relaxed) => intrinsics::atomic_cxchg_seqcst_relaxed(dst, old, new),
30503047
(SeqCst, Acquire) => intrinsics::atomic_cxchg_seqcst_acquire(dst, old, new),
30513048
(SeqCst, SeqCst) => intrinsics::atomic_cxchg_seqcst_seqcst(dst, old, new),
30523049
(_, AcqRel) => panic!("there is no such thing as an acquire-release failure ordering"),
30533050
(_, Release) => panic!("there is no such thing as a release failure ordering"),
3051+
#[cfg(bootstrap)]
30543052
_ => panic!("a failure ordering can't be stronger than a success ordering"),
30553053
}
30563054
};
@@ -3070,22 +3068,29 @@ unsafe fn atomic_compare_exchange_weak<T: Copy>(
30703068
let (val, ok) = unsafe {
30713069
match (success, failure) {
30723070
(Relaxed, Relaxed) => intrinsics::atomic_cxchgweak_relaxed_relaxed(dst, old, new),
3073-
//(Relaxed, Acquire) => intrinsics::atomic_cxchgweak_relaxed_acquire(dst, old, new),
3074-
//(Relaxed, SeqCst) => intrinsics::atomic_cxchgweak_relaxed_seqcst(dst, old, new),
3071+
#[cfg(not(bootstrap))]
3072+
(Relaxed, Acquire) => intrinsics::atomic_cxchgweak_relaxed_acquire(dst, old, new),
3073+
#[cfg(not(bootstrap))]
3074+
(Relaxed, SeqCst) => intrinsics::atomic_cxchgweak_relaxed_seqcst(dst, old, new),
30753075
(Acquire, Relaxed) => intrinsics::atomic_cxchgweak_acquire_relaxed(dst, old, new),
30763076
(Acquire, Acquire) => intrinsics::atomic_cxchgweak_acquire_acquire(dst, old, new),
3077-
//(Acquire, SeqCst) => intrinsics::atomic_cxchgweak_acquire_seqcst(dst, old, new),
3077+
#[cfg(not(bootstrap))]
3078+
(Acquire, SeqCst) => intrinsics::atomic_cxchgweak_acquire_seqcst(dst, old, new),
30783079
(Release, Relaxed) => intrinsics::atomic_cxchgweak_release_relaxed(dst, old, new),
3079-
//(Release, Acquire) => intrinsics::atomic_cxchgweak_release_acquire(dst, old, new),
3080-
//(Release, SeqCst) => intrinsics::atomic_cxchgweak_release_seqcst(dst, old, new),
3080+
#[cfg(not(bootstrap))]
3081+
(Release, Acquire) => intrinsics::atomic_cxchgweak_release_acquire(dst, old, new),
3082+
#[cfg(not(bootstrap))]
3083+
(Release, SeqCst) => intrinsics::atomic_cxchgweak_release_seqcst(dst, old, new),
30813084
(AcqRel, Relaxed) => intrinsics::atomic_cxchgweak_acqrel_relaxed(dst, old, new),
30823085
(AcqRel, Acquire) => intrinsics::atomic_cxchgweak_acqrel_acquire(dst, old, new),
3083-
//(AcqRel, SeqCst) => intrinsics::atomic_cxchgweak_acqrel_seqcst(dst, old, new),
3086+
#[cfg(not(bootstrap))]
3087+
(AcqRel, SeqCst) => intrinsics::atomic_cxchgweak_acqrel_seqcst(dst, old, new),
30843088
(SeqCst, Relaxed) => intrinsics::atomic_cxchgweak_seqcst_relaxed(dst, old, new),
30853089
(SeqCst, Acquire) => intrinsics::atomic_cxchgweak_seqcst_acquire(dst, old, new),
30863090
(SeqCst, SeqCst) => intrinsics::atomic_cxchgweak_seqcst_seqcst(dst, old, new),
30873091
(_, AcqRel) => panic!("there is no such thing as an acquire-release failure ordering"),
30883092
(_, Release) => panic!("there is no such thing as a release failure ordering"),
3093+
#[cfg(bootstrap)]
30893094
_ => panic!("a failure ordering can't be stronger than a success ordering"),
30903095
}
30913096
};

0 commit comments

Comments
 (0)