-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Atomic as_mut_ptr #66705
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Atomic as_mut_ptr #66705
Conversation
r? @KodrAus (rust_highfive has picked a reviewer for you, use r? to override) |
👍 for the basic functionality; I don't have time to review the docs I am afraid. |
Since you can produce data races using this method it may be good to mark it |
That was my first intention, and in a way I agree with you. But there is no way to cause data races with this function in safe Rust, and there is precedent in not marking a function unsafe just because it can lead to unsafety later on. Even all the similarly named |
The important difference is, that they all take a |
It was maybe good to mention that atomics are a |
Another point against making this method unsafe: it is doing nothing that can't already be done in safe (but maybe questionable) code. When should I make a tracking issue? After review? |
Quoting
Please note the difference, that |
No. The signature here matches |
Apparently the documentation should explain why this method is safe. I am no wordsmith, @jfrimmel and @hellow554 can I use you as test subjects 😄? Does this explanation make sense and is it enough?
|
The The |
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.
This looks good to me! I’ll open up a tracking issue that we can reference in here then r=me
Thank you! |
Maybe this one: @bors r=KodrAus |
@pitdicker: 🔑 Insufficient privileges: Not in reviewers |
@bors r=KodrAus |
📌 Commit d34090a has been approved by |
🌲 The tree is currently closed for pull requests below priority 1000, this pull request will be tested once the tree is reopened |
Atomic as_mut_ptr I encountered the following pattern a few times: In Rust we use some atomic type like `AtomicI32`, and an FFI interface exposes this as `*mut i32` (or some similar `libc` type). It was not obvious to me if a just transmuting a pointer to the atomic was acceptable, or if this should use a cast that goes through an `UnsafeCell`. See rust-lang#66136 (comment) Transmuting the pointer directly: ```rust let atomic = AtomicI32::new(1); let ptr = &atomic as *const AtomicI32 as *mut i32; unsafe { ffi(ptr); } ``` A dance with `UnsafeCell`: ```rust let atomic = AtomicI32::new(1); unsafe { let ptr = (&*(&atomic as *const AtomicI32 as *const UnsafeCell<i32>)).get(); ffi(ptr); } ``` Maybe in the end both ways could be valid. But why not expose a direct method to get a pointer from the standard library? An `as_mut_ptr` method on atomics can be safe, because only the use of the resulting pointer is where things can get unsafe. I documented its use for FFI, and "Doing non-atomic reads and writes on the resulting integer can be a data race." The standard library could make use this method in a few places in the WASM module. cc @RalfJung as you answered my original question.
Rollup of 9 pull requests Successful merges: - #66612 (Initial implementation of or-pattern usefulness checking) - #66705 (Atomic as_mut_ptr) - #66759 (impl TrustedLen for vec::Drain) - #66858 (Use LLVMAddAnalysisPasses instead of Rust's wrapper) - #66870 (SimplifyArmIdentity only for locals with the same type) - #66883 (rustc_typeck: gate AnonConst's generics on feature(const_generics).) - #66889 (Make python-generated source files compatible with rustfmt) - #66894 (Remove unneeded prelude imports in libcore tests) - #66895 (Feature gating *declarations* => new crate `rustc_feature`) Failed merges: - #66905 (rustc_plugin: Remove some remaining plugin features) r? @ghost
I encountered the following pattern a few times: In Rust we use some atomic type like
AtomicI32
, and an FFI interface exposes this as*mut i32
(or some similarlibc
type).It was not obvious to me if a just transmuting a pointer to the atomic was acceptable, or if this should use a cast that goes through an
UnsafeCell
. See #66136 (comment)Transmuting the pointer directly:
A dance with
UnsafeCell
:Maybe in the end both ways could be valid. But why not expose a direct method to get a pointer from the standard library?
An
as_mut_ptr
method on atomics can be safe, because only the use of the resulting pointer is where things can get unsafe. I documented its use for FFI, and "Doing non-atomic reads and writes on the resulting integer can be a data race."The standard library could make use this method in a few places in the WASM module.
cc @RalfJung as you answered my original question.