-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
atomics documentation for conflicting access uses undefined term "write" leading to ambiguity about data races #136669
Comments
The language being discussed here was added in PR #128778 by @RalfJung. Again, I think the main issues are:
|
I think "write" is fine terminology, we just have to specify whether a failing RMW is a write or not. In Miri, we answered this with "no, not a write". So I think the answer is indeed that a failing compare_exchange is not a write. This is all happening in the C++ memory model; there can't be anything platform-dependent here. Cc @gnzlbg @chorman0773 to be sure.
With the newly proposed wording, the term "modifies" is not defined either... (EDIT: it does seem to be the term used in the C++ spec, but I couldn't find a definition there either) I think "write" is a strictly better term than "modifies". Saying "modifies" introduces extra ambiguity since it may sound like writing the value that is already stored in memory might not "modify" memory, but it unambiguously is a "write". |
atomic: clarify that failing conditional RMW operations are not 'writes' Fixes rust-lang#136669 r? `@Amanieu` Cc `@rust-lang/opsem` `@chorman0773` `@gnzlbg` `@briansmith`
Rollup merge of rust-lang#138000 - RalfJung:atomic-rmw, r=Amanieu atomic: clarify that failing conditional RMW operations are not 'writes' Fixes rust-lang#136669 r? ``@Amanieu`` Cc ``@rust-lang/opsem`` ``@chorman0773`` ``@gnzlbg`` ``@briansmith``
Location
https://doc.rust-lang.org/nightly/core/sync/atomic/index.html
Summary
At https://doc.rust-lang.org/nightly/core/sync/atomic/index.html we have:
I propose we change this to:
With the existing wording, the term "write" is not defined and isn't used in the reference C++ spec.
For motivation, consider these two operations happening concurrently in separate threads:
thread 1:
thread 2:
Obviously, if thread 2 succeeds in modifying
my_atomic_usize
(its value was zero) then its operation must be considered a "write". But, what if it doesn't succeed (the value was non-zero)? Is thatcompare_exchange
still considered a "write"?The real-world use case is
once_cell::OnceNonZeroUsize
, a one-time initialize type where a nonzero value indicates that initialization has already been done. When the application has already read a non-zero value ([1] above), it would like to use non-synchronized reads ([2] above) for subsequent loads, because the optimizer can then eliminate redundant loads. We think this is safe but only this presumes thatcompare_exchange
is only considered a write if it succeeds, for the purpose of the quoted statement.I understand
compare_exchange
that fails is still considered an "attempt to write" write when it is done on read-only memory, and thus leads to UB, according to a later section in same documentation.But, "write" and "attempt to write" are not necessarily the same thing.
The text was updated successfully, but these errors were encountered: