Skip to content

Fix wrong statement in compare_exchange doc #34046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2016
Merged

Fix wrong statement in compare_exchange doc #34046

merged 1 commit into from
Jun 10, 2016

Conversation

Vtec234
Copy link
Contributor

@Vtec234 Vtec234 commented Jun 2, 2016

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

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@nagisa
Copy link
Member

nagisa commented Jun 2, 2016

cc @Amanieu.

Overall looks fine to me.

@Amanieu
Copy link
Member

Amanieu commented Jun 2, 2016

Nice catch! This sentence could probably be added to compare_exchange_weak as well, since it also applies to that success case.

@steveklabnik
Copy link
Member

It looks fine to me too, but since atomics are an area where I'm not that strong, a double-check by @rust-lang/libs would be nice, given this is about guarantees.

@aturon
Copy link
Member

aturon commented Jun 7, 2016

@steveklabnik FYI, @Amanieu can definitely head up the technical review here (as a concurrency expert). In any case, LGTM as well. I might even suggest nixing "this value" and just repeating "the return value" for maximum clarity.

@steveklabnik
Copy link
Member

This is one of those modules I haven't gone over yet, so I'm fine merging without the prose being absolutely perfect. Thanks all!

@bors: r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 7, 2016

📌 Commit 8841f26 has been approved by steveklabnik

Manishearth added a commit to Manishearth/rust that referenced this pull request Jun 8, 2016
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
@bors
Copy link
Collaborator

bors commented Jun 10, 2016

⌛ Testing commit 8841f26 with merge c09d546...

bors added a commit that referenced this pull request Jun 10, 2016
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
@bors bors merged commit 8841f26 into rust-lang:master Jun 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants