-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add extra access methods for atomic types #1649
Conversation
All the other methods for loading the values of atomic types take an argument to specify the ordering. Why not these? |
What is required for code to be sound while calling the unsafe |
Because they are accessing the value non-atomically.
The pointer needs to be valid, and you need to make sure that the other thread is not accessing the integer non-atomically while you are performing atomic operations on it. The reason a raw pointer is used here is because the value is a shared and mutable integer, which can't be expressed using a reference. |
The |
@alexcrichton I think simply documenting that |
Aren't there platforms where we'd have to use AtomicUsize to emulate smaller atomics? |
@Amanieu yeah I might prefer to go that route (a function from @ubsan yeah but |
@alexcrichton ah, 'k. |
@alexcrichton Hmm wouldn't that misbehave if you have like |
@glaebhoerl It isn't a problem since a modification to one |
OK, after giving this RFC some though, I'm in favor of the general idea. In particular, I've wanted something like I personally would prefer to start with just |
@aturon The reason for adding into_inner was to be able to consume an atomic that wasn't declared with a mutable binding. In practice I expect 99% of uses of |
🔔 This RFC is now entering its week-long final comment period 🔔 The libs team is leaning towards merging this as is (with |
Discussed during libs triage the other day, the conclusion was to merge Thanks again for the RFC @Amanieu! |
Implement RFC 1649 cc #35603 rust-lang/rfcs#1649 r? @alexcrichton
I still get an improper_ctypes warning when I try to use an atomic for FFI. Is this expected? (Relative Rust noob here; apologies if this is a dumb question or an inappropriate place/way to comment.) |
What this RFC is proposing is ability to extract plain |
Actually, it has already been implemented: rust-lang/rust#35719 |
@nagisa, it seems to me that with this RFC the layout of
But the compiler has no way of knowing this. Apologies that this wasn't clear from my earlier example, but as in the example of Linux futexes, I need to have Rust atomically manipulate a value shared with C. So it seems like the correct solution is a pointer cast, and the effect of this RFC is that this is now defined/correct? (That's another contrived example, but closer to what I need to do.) But this is not nearly as nice as being able to use I can't even use |
My opinion is that structs with only a single member should be defined as having the same representation as that element. As a workaround, I guess we could just mark the atomic types as |
This allows them to be used in #[repr(C)] structs without warnings. Since rust-lang/rfcs#1649 and rust-lang#35603 they are already documented to have "the same in-memory representation as" their corresponding primitive types. This just makes that explicit.
Add #[repr(transparent)] to Atomic* types This allows them to be used in `#[repr(C)]` structs without warnings. Since rust-lang/rfcs#1649 and rust-lang#35603 they are already documented to have "the same in-memory representation as" their corresponding primitive types. This just makes that explicit. This was briefly part of rust-lang#51395, but was controversial and therefore dropped. But it turns out that it's essentially already documented (which I had forgotten).
Add #[repr(transparent)] to Atomic* types This allows them to be used in `#[repr(C)]` structs without warnings. Since rust-lang/rfcs#1649 and rust-lang#35603 they are already documented to have "the same in-memory representation as" their corresponding primitive types. This just makes that explicit. This was briefly part of rust-lang#51395, but was controversial and therefore dropped. But it turns out that it's essentially already documented (which I had forgotten).
This RFC adds the following methods to atomic types:
get_mut
into_inner
as_raw
from_raw
Rendered