Skip to content

Atomic*::as_ptr unnecessarily restricts operations on the pointer to atomic operations #138246

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
briansmith opened this issue Mar 8, 2025 · 1 comment
Labels
A-atomic Area: Atomics, barriers, and sync primitives A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-opsem Relevant to the opsem team

Comments

@briansmith
Copy link
Contributor

briansmith commented Mar 8, 2025

Location

https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html#method.as_ptr

[...] Any use of the returned raw pointer requires an unsafe block and still has to uphold the same restriction: operations on it must be atomic.

Summary

PR #136669 and PR #138000 make it clear that we should be able to safely do a non-atomic read of an atomic in certain circumstances. The most natural way to do the non-atomic read is to use as_ptr() and then do a read through the pointer.

However, the documentation for as_ptr() disallow any non-atomic operation on the pointer it returns, at odds with the aforementioned PRs. I believe the statement "operations on it must be atomic" should be replaced with a reference to the documentation in https://doc.rust-lang.org/std/sync/atomic/index.html#memory-model-for-atomic-accesses.

(NIT: I don't think "Any use of the returned raw pointer requires an unsafe block" is correct or particularly useful. That seems to be justifying the addition of the function, not helping people use it. Presumably pointer comparison or other operations can be done that don't require an unsafe block.)

/cc @RalfJung

@briansmith briansmith added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Mar 8, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 8, 2025
@lolbinarycat lolbinarycat added T-opsem Relevant to the opsem team A-atomic Area: Atomics, barriers, and sync primitives labels Mar 9, 2025
@RalfJung
Copy link
Member

Yeah good point, the wording should be updated to reflect that non-atomic accesses are UB if they cause a data race -- the point is to clarify that this method does not somehow grant an exception to that rule.

If someone makes a PR for this, please tag me and I'll take a look. :)

@briansmith briansmith changed the title Atomic*::as_ptr unnecessarily restrictions operations on the pointer to atomic operations Atomic*::as_ptr unnecessarily restricts operations on the pointer to atomic operations Mar 10, 2025
@lolbinarycat lolbinarycat added the E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. label Apr 9, 2025
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-atomic Area: Atomics, barriers, and sync primitives A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-opsem Relevant to the opsem team
Projects
None yet
Development

No branches or pull requests

5 participants