-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
std.sync.atomic: extended atomic helper functions #8866
Conversation
kprotty
commented
May 22, 2021
•
edited
Loading
edited
- generic Atomic(T) wrapper replacing atomic.Int and atomic.Bool
- Bitwise atomic intrinsics when Atomic(T) is an Integer
- No redundant specification of type as seen in builtins atomic functions
- fetchXX() ops instead of generic atomicRmw for C11 consistency
- contains compilerFence() and spinLoopHint()
We already have atomic wrappers for ints and bools. That approach is much safer than taking raw pointers as it prevents you from mixing atomic and non-atomic ops. |
@LemonBoy atomic int and bool dont contain cmpxchg abstractions + there aren't any such wrappers for atomic pointers/enums/floats. Not sure about improved safety as ordering misuse/ignorance can often be equally unsafe. Mixing non-atomic ops and atomic ops can sometimes be valid. Would a generic |
Should be added if needed.
Wrappers for pointers and enums are also a good idea. Floating point types can be added later (if ever) when somebody finds an use for those. Not sure about improved safety as ordering misuse/ignorance can often be equally unsafe. The wrappers try to protect the user from shooting themselves in the foot by checking the ordering parameter, avoid defining meaningless operations, and preventing the mix-up of atomic & non atomic ops. The idea of wrapper types is not novel (see Rust or C11) and strikes a good balance between ergonomics and safety.
I think separate wrapper types are simpler to define, you need quite a bit of meta programming and |
Separate wrapper types feels like repetition: They would all expose load/store/xchg/cmpxchg and ordering checks. Floats would expose that plus arithmetic rmw. Ints would expose even that plus bitwise rmw. Theres few metaprogramming conditions but it covers a lot of cases. Thats my pitch for Atomic(T) |
I'm sold! |
I prefer everything except the Actually, another issue with wrapper types is being able to store them inside |
There should be no problem if the wrapper is |
freebsd CI: atomic.zig:25: Whats up with the fence builtin? |
Change this line. |
@LemonBoy Any further comments before it gets merged? |
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.
LGTM.
The size of type T
is actually bounded by the combination of architecture and feature flags being used, an extra check can be added at a later stage though.
@compileError(@tagName(Ordering.Unordered) ++ " is only allowed on atomic loads and stores"); | ||
} | ||
|
||
comptime var success_is_stronger = switch (failure) { |
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.
comptime var success_is_stronger = switch (failure) { | |
comptime const success_is_stronger = switch (failure) { |
Since std.atomic.Int was replaced by std.atomic.Atomic at "std.sync.atomic: extended atomic helper functions by kprotty · Pull Request #8866 · ziglang/zig" ziglang/zig#8866