Skip to content

Commit 7bd770c

Browse files
committed
Add documentation on migrating away from compare_and_swap
1 parent 0698922 commit 7bd770c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

library/core/src/sync/atomic.rs

+51
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,23 @@ impl AtomicBool {
464464
/// **Note:** This method is only available on platforms that support atomic
465465
/// operations on `u8`.
466466
///
467+
/// # Migrating to `compare_exchange` and `compare_exchange_weak`
468+
///
469+
/// `compare_and_swap` is equivalent to `compare_exchange` with the following mapping for
470+
/// memory orderings:
471+
///
472+
/// Original | Success | Failure
473+
/// -------- | ------- | -------
474+
/// Relaxed | Relaxed | Relaxed
475+
/// Acquire | Acquire | Acquire
476+
/// Release | Release | Relaxed
477+
/// AcqRel | AcqRel | Acquire
478+
/// SeqCst | SeqCst | SeqCst
479+
///
480+
/// `compare_exchange_weak` is allowed to fail spuriously even when the comparison succeeds,
481+
/// which allows the compiler to generate better assembly code when the compare and swap
482+
/// is used in a loop.
483+
///
467484
/// # Examples
468485
///
469486
/// ```
@@ -1048,6 +1065,23 @@ impl<T> AtomicPtr<T> {
10481065
/// **Note:** This method is only available on platforms that support atomic
10491066
/// operations on pointers.
10501067
///
1068+
/// # Migrating to `compare_exchange` and `compare_exchange_weak`
1069+
///
1070+
/// `compare_and_swap` is equivalent to `compare_exchange` with the following mapping for
1071+
/// memory orderings:
1072+
///
1073+
/// Original | Success | Failure
1074+
/// -------- | ------- | -------
1075+
/// Relaxed | Relaxed | Relaxed
1076+
/// Acquire | Acquire | Acquire
1077+
/// Release | Release | Relaxed
1078+
/// AcqRel | AcqRel | Acquire
1079+
/// SeqCst | SeqCst | SeqCst
1080+
///
1081+
/// `compare_exchange_weak` is allowed to fail spuriously even when the comparison succeeds,
1082+
/// which allows the compiler to generate better assembly code when the compare and swap
1083+
/// is used in a loop.
1084+
///
10511085
/// # Examples
10521086
///
10531087
/// ```
@@ -1575,6 +1609,23 @@ happens, and using [`Release`] makes the load part [`Relaxed`].
15751609
**Note**: This method is only available on platforms that support atomic
15761610
operations on [`", $s_int_type, "`](", $int_ref, ").
15771611
1612+
# Migrating to `compare_exchange` and `compare_exchange_weak`
1613+
1614+
`compare_and_swap` is equivalent to `compare_exchange` with the following mapping for
1615+
memory orderings:
1616+
1617+
Original | Success | Failure
1618+
-------- | ------- | -------
1619+
Relaxed | Relaxed | Relaxed
1620+
Acquire | Acquire | Acquire
1621+
Release | Release | Relaxed
1622+
AcqRel | AcqRel | Acquire
1623+
SeqCst | SeqCst | SeqCst
1624+
1625+
`compare_exchange_weak` is allowed to fail spuriously even when the comparison succeeds,
1626+
which allows the compiler to generate better assembly code when the compare and swap
1627+
is used in a loop.
1628+
15781629
# Examples
15791630
15801631
```

0 commit comments

Comments
 (0)