Skip to content
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

core::atomic docs: what guarantees hold when compare_and_swap returns other values? #21668

Closed
pnkfelix opened this issue Jan 26, 2015 · 3 comments · Fixed by #23793
Closed

core::atomic docs: what guarantees hold when compare_and_swap returns other values? #21668

pnkfelix opened this issue Jan 26, 2015 · 3 comments · Fixed by #23793

Comments

@pnkfelix
Copy link
Member

The documentation for the various compare_and_swap methods (there's one each for AtomicBool, AtomicIsize, and AtomicUsize) says:

    /// If the return value is equal to `old` then the value was updated.

But it says nothing about other return values.

In particular, it does not say if the return value actually corresponds to a value that had been stored at some point in the memory. For all that this says, maybe its going to return old + 1 or !old, rather than the contents of memory at the time that the failed swap was attempted.

So right now, if I want to be portable and follow these docs, I need to issue a separate call to the load method to load a current value, e.g. if my plan is to loop around and recompute the next value I want to store from the concurrently updated current value.


Now, maybe this is not an oversight in the documentation, but is in fact a deliberate underspecification of the return value for compare_and_swap -- i.e. maybe we do not want to guarantee too much about the underling behavior of the target system.

But in my opinion, a return value that is so woefully underspecified is no better than returning a boolean flag indicating whether the swap was successful or not.

And in any case, if this is a deliberate underspecification on the part of the documentation, then it should explicitly say something like "when the value is not updated, then the return value will be unequal to old, but is otherwise unspecified." (Because right now it is very easy for a reader to infer that the "currently stored" value that is unequal to old is what is returned.)

@ranma42
Copy link
Contributor

ranma42 commented Jan 27, 2015

Some code in libstd seems to assume that compare_and_swap will always return the original value of the atomic entity.

Examples:

My understanding is that the same semantics holds for compare_and_swap in Rust, except that no flag is returned.

@steveklabnik
Copy link
Member

@pnkfelix who do you think i should ask about clarifying this behavior?

@alexcrichton
Copy link
Member

The semantics (just forgotten to be documented) are what @ranma42 pointed out. The previous value is always returned.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 27, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this issue Mar 27, 2015
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 a pull request may close this issue.

4 participants