-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Rewrite docs for fetch_update for clarity #136036
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
Once the proposed text replacement is approved, I will copy it to the other two places that define fetch_update. |
☔ The latest upstream changes (presumably #136185) made this pull request unmergeable. Please resolve the merge conflicts. |
library/core/src/sync/atomic.rs
Outdated
mut f: F) -> Result<$int_type, $int_type> | ||
where F: FnMut($int_type) -> Option<$int_type> { | ||
let mut prev = self.load(fetch_order); | ||
let mut prev = self.load(failure); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps a comment explaining why we are loading with the 'failure' order could be added here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sold on this particular naming, but this change tries to bring this into line with compare_exchange's use of the terms. It explains the terms like this: "success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails.". Perhaps update_order
and load_order
are better terms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing set_order
and fetch_order
are probably fine. It would indeed seem weird to immediately load a value using failure
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I'll go the other way then, and eliminate the success and failure orders from compare_exchange*. But, euhm, perhaps in a separate PR...
library/core/src/sync/atomic.rs
Outdated
/// Note: susceptible to the [ABA Problem](https://en.wikipedia.org/wiki/ABA_problem). | ||
/// | ||
/// **Note**: This method is only available on platforms that support atomic operations on | ||
#[doc = concat!("[`", $s_int_type, "`].")] | ||
/// | ||
/// # Considerations | ||
/// | ||
/// This method is not magic; it is not provided by the hardware. | ||
/// It is implemented in terms of | ||
#[doc = concat!("[`", stringify!($atomic_type), "::compare_exchange_weak`],")] | ||
/// and suffers from the same drawbacks. | ||
/// In particular, this method will not circumvent the [ABA Problem]. | ||
/// | ||
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed? IMO the longer version is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation using compare_exchange_weak is now explicitly described. This makes it unnecessary to say it is not magic, because that is now obvious. Also, users are more likely to look at the docs for compare_exchange_weak. That leaves the "in particular ... ABA problem", which I've shortened to its core, which is that this method is "susceptible to the ABA Problem".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explicit description of how the implementation uses compare_exchange_weak is also why I removed the copy of the description of the constraints on the memory orderings which comes directly from using compare_exchange_weak. Thanks for reviewing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with @Sky9x, this section should remain. It's one thing to explain that this uses compare_exchange_weak
, another thing to confirm that it doesn't somehow do some tricks to avoid compare_exchange_weak
's limitations.
Plus I have definitely seen this specific section get linked around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very well. This section also got a significant improvement in #142252, so my reasons for removing it don't seem to apply anymore.
☔ The latest upstream changes (presumably #142432) made this pull request unmergeable. Please resolve the merge conflicts. |
library/core/src/sync/atomic.rs
Outdated
/// If the closure returns Some(new value), then this method calls | ||
#[doc = concat!("[`", stringify!($atomic_type), "::compare_exchange_weak`]")] | ||
/// to try to store the new value. | ||
/// If storing a new value fails, because another thread changed the current value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// If storing a new value fails, because another thread changed the current value, | |
/// If storing a new value fails (due to another thread changing the current value) |
Suggested reword to avoid using comma+"because" for an interjection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parenthetical is indeed probably more appropriate here. I'm not sure I understand the difference in nuance between "because"/"due to", but I'll copy that too. If you have any articulable insight there, I'd love to hear it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have any articulable insight there, I'd love to hear it.
Not at all 😆 I probably just rewrote it a few times before finalizing the suggestion, feel free to pick either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So disappointing ;P
library/core/src/sync/atomic.rs
Outdated
/// then the given closure will be called again on the new current value | ||
/// (that was just returned by compare_exchange_weak), | ||
/// until either the closure returns None, | ||
/// or compare_exchange_weak succeeds in storing a new value. | ||
/// Returns `Ok(previous value)` if it ever succeeds in storing a new value. | ||
/// Takes a success and a failure [`Ordering`] to pass on to compare_exchange_weak, | ||
/// and also uses the failure ordering for the initial load. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// then the given closure will be called again on the new current value | |
/// (that was just returned by compare_exchange_weak), | |
/// until either the closure returns None, | |
/// or compare_exchange_weak succeeds in storing a new value. | |
/// Returns `Ok(previous value)` if it ever succeeds in storing a new value. | |
/// Takes a success and a failure [`Ordering`] to pass on to compare_exchange_weak, | |
/// and also uses the failure ordering for the initial load. | |
/// then the given closure will be called again on the new current value, | |
/// which was just returned by `compare_exchange_weak`. | |
/// This will repeat until either the closure returns None, | |
/// or `compare_exchange_weak` succeeds in storing a new value. The return value is | |
/// `Ok(previous value)` if a new value was ever successfully stored, otherwise | |
/// `Err(current_value)` | |
/// | |
/// Takes a success and a failure [`Ordering`] to pass on to `compare_exchange_weak`, | |
/// and also uses the failure ordering for the initial load. |
Few suggestions:
- Split the long sentence and make the CEW call part of the main flow.
- Mention the
Err
condition right next toOk
. - Split the ordering discussion to a new paragraph.
compare_exchange_weak
needs backticks
Also please make sure to rewrap doc comments to 100 chars before merge, or as far as is possible with the scattered #[doc]
attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Split the long sentence and make the CEW call part of the main flow.
Done.
* Mention the `Err` condition right next to `Ok`.
Done. I think I was afraid of the repetition, but it's probably better this way.
* Split the ordering discussion to a new paragraph.
Done.
* `compare_exchange_weak` needs backticks
Definitely.
Also please make sure to rewrap doc comments to 100 chars before merge, or as far as is possible with the scattered
#[doc]
attributes.
Once this is nearing completion, I'll do that, if you really insist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On rereading the new text, the ambiguity of "This will repeat" seems unescapable; even knowing the form of the answer, I cannot answer the question "what will repeat" with a quote from the text, and so "This" seems to be a dangling pointer. Probably because the previous sentence does not contain the act of storing anymore. This is probably why I wrote such a long sentence originally.
Going back to longer sentence, but with mention of CEW also parenthesized, so the main structure is clearer.
library/core/src/sync/atomic.rs
Outdated
/// Note: susceptible to the [ABA Problem](https://en.wikipedia.org/wiki/ABA_problem). | ||
/// | ||
/// **Note**: This method is only available on platforms that support atomic operations on | ||
#[doc = concat!("[`", $s_int_type, "`].")] | ||
/// | ||
/// # Considerations | ||
/// | ||
/// This method is not magic; it is not provided by the hardware. | ||
/// It is implemented in terms of | ||
#[doc = concat!("[`", stringify!($atomic_type), "::compare_exchange_weak`],")] | ||
/// and suffers from the same drawbacks. | ||
/// In particular, this method will not circumvent the [ABA Problem]. | ||
/// | ||
/// [ABA Problem]: https://en.wikipedia.org/wiki/ABA_problem | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with @Sky9x, this section should remain. It's one thing to explain that this uses compare_exchange_weak
, another thing to confirm that it doesn't somehow do some tricks to avoid compare_exchange_weak
's limitations.
Plus I have definitely seen this specific section get linked around.
library/core/src/sync/atomic.rs
Outdated
while let Some(next) = f(prev) { | ||
match self.compare_exchange_weak(prev, next, set_order, fetch_order) { | ||
match self.compare_exchange_weak(prev, next, success, failure) { | ||
x @ Ok(_) => return x, | ||
Err(next_prev) => prev = next_prev | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure to update update
and try_update
as well once this is closer to ready #135894
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely!
library/core/src/sync/atomic.rs
Outdated
mut f: F) -> Result<$int_type, $int_type> | ||
where F: FnMut($int_type) -> Option<$int_type> { | ||
let mut prev = self.load(fetch_order); | ||
let mut prev = self.load(failure); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing set_order
and fetch_order
are probably fine. It would indeed seem weird to immediately load a value using failure
.
@tgross35, thanks for reviewing! I'll look at each of your points soon. |
1a0d867
to
e0a5c9a
Compare
This comment has been minimized.
This comment has been minimized.
e0a5c9a
to
00468e3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I think this is definitely clearer than the current docs. I have a few suggestions. Also note that once we're happy with the wording, this needs to be duplicated to the AtomicPtr
docs separately as well, since they're different than all the atomic ints implemented thru the macro.
library/core/src/sync/atomic.rs
Outdated
/// Note: This may call the function multiple times if the value has been changed from other threads in | ||
/// the meantime, as long as the function returns `Some(_)`, but the function will have been applied | ||
/// only once to the stored value. | ||
/// If the closure ever returns None, this method will immediately return `Err(current value)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the order of these two sentences should be swapped, putting the success case first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally ordered these sentences this way, because the None
case is so much simpler than the Some
case, which requires clarification for the fail-to-store scenario.
library/core/src/sync/atomic.rs
Outdated
/// the meantime, as long as the function returns `Some(_)`, but the function will have been applied | ||
/// only once to the stored value. | ||
/// If the closure ever returns None, this method will immediately return `Err(current value)`. | ||
/// If the closure returns Some(new value), then this method calls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// If the closure returns Some(new value), then this method calls | |
/// If the closure returns `Some(updated_value)`, then this method calls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 6 uses of "new value" (including this one), which is used consistently for the value that comes from the closure and is attempted to be stored. On first glance it does not seem clear which of those should be changed. But if I assume all should be changed, for consistency, some of these substitutions seem awkward.
library/core/src/sync/atomic.rs
Outdated
/// Note: This may call the function multiple times if the value has been changed from other threads in | ||
/// the meantime, as long as the function returns `Some(_)`, but the function will have been applied | ||
/// only once to the stored value. | ||
/// If the closure ever returns None, this method will immediately return `Err(current value)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// If the closure ever returns None, this method will immediately return `Err(current value)`. | |
/// If the closure ever returns `None`, this method will immediately return `Err(loaded_value)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, the issue you're identifying is that you can never really be sure whether the value you have is current or not, only that you loaded it.
Current usage is "Loads the current value ..." and "... return Err(current value)
" plus 3 more. Your suggestion taken literally is "Loads the current value ..." and "... return Err(loaded_value)
" in which loaded_value seems to not have an antecedent any more. And if I take it more consistently, it only seems to become worse: "Loads the loaded_value ..." and "... return Err(loaded_value)
".
Your critique is valid, but I don't think this solution works.
library/core/src/sync/atomic.rs
Outdated
/// If storing a new value fails (due to another thread changing the current value), | ||
/// then the given closure will be called again on the new current value | ||
/// (returned by `compare_exchange_weak`), until either the closure returns None, | ||
/// or `compare_exchange_weak` succeeds in storing a new value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should try to talk about the semantics a bit more rather than directly to the implementation, something like
If the value of the atomic observed when attempting to store the
updated_value
is not equal to the value from the initial load, then the store will not happen. Instead, the closure will be invoked again, this time with the new value observed during the store attempt. This pattern will continue in a loop until either the closure returnsNone
or the store succeeds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that seems very attractive. I will spend some time exploring this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After trying, unfortunately I'm not sure how to use this.
library/core/src/sync/atomic.rs
Outdated
/// If the closure returns Some(new value), then this method calls | ||
#[doc = concat!("[`", stringify!($atomic_type), "::compare_exchange_weak`]")] | ||
/// to try to store the new value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps explain the semantics first, I do think a callout to the implementation is appropriate in parens here though.
If the closure returns
Some(updated_value)
, the implementation will attempt to store theupdated_value
to the atomic. This store will only occur if the value of the atomic is still equal to the value of the initial load (seecompare_exchange_weak
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do like this, but I'm not sure how to explain the two ordering arguments that are essentially for passing on to compare_exchange_weak, when compare_exchange_weak's involvement is not crystal clear.
I will think about it some more.
00468e3
to
9b60854
Compare
@fu5ha Thanks for reviewing. I apologize for not being good enough to make much use of your suggestions. I hope you won't hold it against me. |
@tgross35 Are there any egregious line wrapping problems that I should fix, before copying this? |
In particular also uses the same names for the orderings as compare_exchange does to reduce confusion as per #89116.