-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: BitwiseAtomic<T> #105054
Comments
Tagging subscribers to this area: @mangod9 |
What does that do to types like the following -- perhaps zero them entirely? [StructLayout(LayoutKind.Explicit, Size = 8)]
struct Mystery
{
// Fields are accessed using unsafe code and are not declared to the CLR.
} IIRC, the C++/CLI compiler can generate types like that. |
Yeah, I don't think that will work with this API. This API is meant to be "safe", such that any padded bytes that could be random won't affect the equality of the declared fields. You would need to declare a field (or fields) of that size to make it work, or union the type in another struct. |
This type could possibly need to be a ref type with special GC support since 16B cmpxchg requires 16B alignment on all platforms. |
Is that not enforceable for this struct type? It would be unfortunate to add extra object overhead if it can be avoided. Or do you mean this type would be specially treated the same as object references, such that it must always be aligned? If so, I can definitely get behind that. |
I explained in the proposal how this is different than that. Actually, that one could probably be built on top of this. |
Another boon to this type is, for architectures that don't support small atomics (like |
Background and motivation
To make it possible to use atomic
(Compare)Exchange
on any type, including custom structs of varying size. It also makes it possible to use 128-bit atomics which the runtime currently doesn't support (like x64'scmpxchg16b
), and atomic exchanges of 2 references at once (which can't even be done in 32-bit runtimes currently).If the type's size is <= the target architecture's largest atomic
(Compare)Exchange
instruction (16 bytes for 64-bit, 8 bytes for 32-bit), it will use native atomic instructions. If a type's size is larger than that, the runtime will fallback to a spinlock.In the case of a padded type, the runtime must ensure that all padded bits are zeroed before performing the atomic operation.
For optimal performance, the runtime should also align the field of the atomic properly according to its size (if possible). I.E. a
BitwiseAtomic<ObjectPair>
should be aligned on a 16-byte boundary, as opposed to the normal 8-byte boundary in 64-bit processes.This proposal is different than #17975, which uses
IEquatable<T>
for equality comparisons, while this proposal uses bitwise equality. Float -0.0 != 0.0. This can result in single instruction operations compared to that proposal which requires CompareExchange loops. And without requiring theIEquatable<T>
interface, it will work with any type.This could also supersede #31911 since we only need 1 new type instead of several.
API Proposal
API Usage
Alternative Designs
No response
Risks
It's a struct, and exposes non-atomic methods, so it could be torn if used improperly. It also has the same risks as the
SpinLock
struct.The text was updated successfully, but these errors were encountered: