Skip to content

Commit c09d546

Browse files
authored
Auto merge of #34046 - Vtec234:fix-atomic-doc, r=steveklabnik
Fix wrong statement in compare_exchange doc The documentation for `core::sync::atomic::AtomicSomething::compare_exchange` contains a wrong, or imprecise, statement about the return value. It goes: The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to `new`. In the second sentence, `this value` is gramatically understood as referring to `return value` from the first sentence. Due to how CAS works, the returned value is always what was in the atomic variable _before_ the operation occurred, not what was written into it during the operation. Hence, the fixed doc should say: The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to `current`. This version is confirmed by the runnable examples in variants of `AtomicSomething`, e.g. assert_eq!(some_bool.compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed), Ok(true)); where the returned value is `Ok(current)`. This PR fixes all occurrences of this bug I could find. An alternative solution would be to modify the second sentence so that it refers to the value _written_ into the Atomic rather than what was there before, in which case it would be correct. Example alternative formulation: On success the value written into the `bool`/`usize`/`whatever` is guaranteed to be equal to `new`. r? @steveklabnik
2 parents 2798772 + 8841f26 commit c09d546

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/libcore/sync/atomic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl AtomicBool {
289289
/// Stores a value into the `bool` if the current value is the same as the `current` value.
290290
///
291291
/// The return value is a result indicating whether the new value was written and containing
292-
/// the previous value. On success this value is guaranteed to be equal to `new`.
292+
/// the previous value. On success this value is guaranteed to be equal to `current`.
293293
///
294294
/// `compare_exchange` takes two `Ordering` arguments to describe the memory ordering of this
295295
/// operation. The first describes the required ordering if the operation succeeds while the
@@ -633,7 +633,7 @@ impl<T> AtomicPtr<T> {
633633
/// Stores a value into the pointer if the current value is the same as the `current` value.
634634
///
635635
/// The return value is a result indicating whether the new value was written and containing
636-
/// the previous value. On success this value is guaranteed to be equal to `new`.
636+
/// the previous value. On success this value is guaranteed to be equal to `current`.
637637
///
638638
/// `compare_exchange` takes two `Ordering` arguments to describe the memory ordering of this
639639
/// operation. The first describes the required ordering if the operation succeeds while the
@@ -886,7 +886,7 @@ macro_rules! atomic_int {
886886
///
887887
/// The return value is a result indicating whether the new value was written and
888888
/// containing the previous value. On success this value is guaranteed to be equal to
889-
/// `new`.
889+
/// `current`.
890890
///
891891
/// `compare_exchange` takes two `Ordering` arguments to describe the memory ordering of
892892
/// this operation. The first describes the required ordering if the operation succeeds

0 commit comments

Comments
 (0)