Skip to content

Update documentation of as_ptr function of Atomic$Int to clarify circumstances of usage #139637

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

madhav-madhusoodanan
Copy link
Contributor

Context

The documentation of as_ptr mentions that "operations on it must be atomic". This may confuse developers, since non-atomic reads may be safely performed in certain circumstances (eg: when they do not lead to a data race).

The core message is that non-atomic accesses are UB if they cause a data race.

This update ensures such clarity on the circumstances of usage of the as_ptr function.

Associated Issue

cc: @RalfJung @briansmith

@rustbot
Copy link
Collaborator

rustbot commented Apr 10, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 10, 2025
@madhav-madhusoodanan madhav-madhusoodanan changed the title Update documentation of Atomic$Int types' as_ptr to reflect that its usage must not lead to data races Update documentation of as_ptr function of Atomic$Int to reflect that its usage must not lead to data races Apr 10, 2025
@madhav-madhusoodanan madhav-madhusoodanan changed the title Update documentation of as_ptr function of Atomic$Int to reflect that its usage must not lead to data races Update documentation of as_ptr function of Atomic$Int to clarify circumstances of usage Apr 10, 2025
Copy link
Contributor

@briansmith briansmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on.

@rust-log-analyzer

This comment has been minimized.

@madhav-madhusoodanan
Copy link
Contributor Author

I've added an example where there is no unsafe block being used.
Do let me know if its a good example.

@RalfJung
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 17, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 17, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@madhav-madhusoodanan
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 18, 2025
/// use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
/// restriction: operations on it must be atomic.
/// All modifications of an atomic change the value through a shared reference, and can do so safely
/// as long as they use atomic operations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the first part of this sentence... what else would they do?

Copy link
Member

@RalfJung RalfJung Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github is being silly and doesn't let me make this a suggestion, so let me post this manually -- how about:

            /// Note that doing non-atomic reads or writes on the resulting integer can be
            /// Undefined Behavior due to a data race; see the [memory model section] for further information.
            ///
            /// This method is mostly useful for FFI, where the function signature may use
            #[doc = concat!("`*mut ", stringify!($int_type), "` instead of `&", stringify!($atomic_type), "`.")]

As I said before, this should link to the module-level memory model docs. You can probably use an intra-doc link like that... crate::sync::atomic#memory-model-for-atomic-accesses should work.

@RalfJung
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 22, 2025
///
/// Returning an `*mut` pointer from a shared reference to this atomic is safe because the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to clarify one thing, would it be a good idea to note about interior mutability too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I would say interior mutability is an implementation detail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

@madhav-madhusoodanan
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 22, 2025
/// This method is mostly useful for FFI, where the function signature may use
/// `*mut bool` instead of `&AtomicBool`.
/// Note that doing non-atomic reads or writes on the resulting integer can be
/// Undefined Behavior due to a data race; see the [Memory model section] for further information.
Copy link
Member

@RalfJung RalfJung Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Undefined Behavior due to a data race; see the [Memory model section] for further information.
/// Undefined Behavior due to a data race; see the [memory model section] for further information.

Why did you not just copy-paste my suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies.

I take it as an exercise in learning and understanding the nuances of what my changes do, especially when I'm dealing with comments. Hence i usually avoid directly copy pasting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask if intradoc links are case sensitive? I'm slightly new to rustdoc and I wasn't able to notice this detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just standard markdown, not rustdoc: if you write [Memory], it will be shown as "Memory" in the rendered HTML, but we do not want this to be capitalized.

I take it as an exercise in learning and understanding the nuances of what my changes do, especially when I'm dealing with comments. Hence i usually avoid directly copy pasting.

Feel free to do this and then diff the result, but as a reviewer it takes a lot of extra attention if I can't even rely on suggestions being realized correctly. You can of course have a good reason to diverge from my suggestion, but then you should explain why.

Comment on lines +3404 to +3405
/// All modifications of an atomic change the value through a shared reference, and can do so safely
/// as long as they use atomic operations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of this last sentence? It did not exist in my suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was not added. That is the consequence of removing the sentences surrounding it in this commit:

  1. The "interior mutability" detail
  2. The "unsafe" block detail

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I wasn't clear enough then when posting my suggestion -- please remove this sentence. I've already told you a week ago that by removing the sentence before this, you made this sentence here entirely void of context and confusing.

@RalfJung
Copy link
Member

@madhav-madhusoodanan if you prefer I can just post a PR myself for this, that has a lot less friction than me trying to explain to you my suggested edits, in particular since we seem to need 2 round-trips for each suggestion. ;)

@RalfJung
Copy link
Member

That odd sentence "All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations." is still around. I don't know how to make it any more clear that you should remove it everywhere. It made sense before your PR due to the surrounding context, but you removed that context, and now it's entirely unclear what the point of that sentence is.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants