Skip to content

Commit 4252e48

Browse files
committed
Add documentation on migrating away from compare_and_swap
1 parent 3abba5e commit 4252e48

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

library/core/src/sync/atomic.rs

Lines changed: 51 additions & 0 deletions
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
/// ```
@@ -1070,6 +1087,23 @@ impl<T> AtomicPtr<T> {
10701087
/// **Note:** This method is only available on platforms that support atomic
10711088
/// operations on pointers.
10721089
///
1090+
/// # Migrating to `compare_exchange` and `compare_exchange_weak`
1091+
///
1092+
/// `compare_and_swap` is equivalent to `compare_exchange` with the following mapping for
1093+
/// memory orderings:
1094+
///
1095+
/// Original | Success | Failure
1096+
/// -------- | ------- | -------
1097+
/// Relaxed | Relaxed | Relaxed
1098+
/// Acquire | Acquire | Acquire
1099+
/// Release | Release | Relaxed
1100+
/// AcqRel | AcqRel | Acquire
1101+
/// SeqCst | SeqCst | SeqCst
1102+
///
1103+
/// `compare_exchange_weak` is allowed to fail spuriously even when the comparison succeeds,
1104+
/// which allows the compiler to generate better assembly code when the compare and swap
1105+
/// is used in a loop.
1106+
///
10731107
/// # Examples
10741108
///
10751109
/// ```
@@ -1612,6 +1646,23 @@ happens, and using [`Release`] makes the load part [`Relaxed`].
16121646
**Note**: This method is only available on platforms that support atomic
16131647
operations on [`", $s_int_type, "`](", $int_ref, ").
16141648
1649+
# Migrating to `compare_exchange` and `compare_exchange_weak`
1650+
1651+
`compare_and_swap` is equivalent to `compare_exchange` with the following mapping for
1652+
memory orderings:
1653+
1654+
Original | Success | Failure
1655+
-------- | ------- | -------
1656+
Relaxed | Relaxed | Relaxed
1657+
Acquire | Acquire | Acquire
1658+
Release | Release | Relaxed
1659+
AcqRel | AcqRel | Acquire
1660+
SeqCst | SeqCst | SeqCst
1661+
1662+
`compare_exchange_weak` is allowed to fail spuriously even when the comparison succeeds,
1663+
which allows the compiler to generate better assembly code when the compare and swap
1664+
is used in a loop.
1665+
16151666
# Examples
16161667
16171668
```

0 commit comments

Comments
 (0)