-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
remove @fence
#21585
Merged
Merged
remove @fence
#21585
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kprotty
approved these changes
Oct 4, 2024
kprotty
added
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
standard library
This issue involves writing Zig code for the standard library.
release notes
This PR should be mentioned in the release notes.
labels
Oct 4, 2024
I believe the |
Good catch, I totally missed that! |
andrewrk
reviewed
Oct 4, 2024
@@ -4218,11 +4218,10 @@ pub fn print(self: *Writer, arg0: []const u8, arg1: i32) !void { | |||
{#header_close#} | |||
|
|||
{#header_open|Atomics#} | |||
<p>TODO: @fence()</p> |
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.
Good thing I never bothered to write these docs 🙃
Ultra-Code
pushed a commit
to Ultra-Code/zig
that referenced
this pull request
Oct 8, 2024
1 task
This was referenced Oct 16, 2024
43 tasks
richerfu
pushed a commit
to richerfu/zig
that referenced
this pull request
Oct 28, 2024
leroycep
added a commit
to leroycep/libxev
that referenced
this pull request
Jan 26, 2025
Relevant changes - [remove @Fence()][#21585] - `tv_` prefix removed from `linux.timespec` in [std.c reorganization][2] - `linux.timerfd_create` now accepts clockid_t enum in [std.c reorganization][3] - `linux.clockid_t` is now a non-exhaustive enum in [std.c reorganization][4] [#21585]: ziglang/zig#21585 [2]: ziglang/zig@e8c4e79#diff-37593602824d84b26d4345c606faf7be68c398f3bd74016afdaa6ba5dc6acaafL6224-L6233 [3]: ziglang/zig@e8c4e79#diff-37593602824d84b26d4345c606faf7be68c398f3bd74016afdaa6ba5dc6acaafL1965-R1974 [4]: ziglang/zig@e8c4e79#diff-37593602824d84b26d4345c606faf7be68c398f3bd74016afdaa6ba5dc6acaafL4032-R4056
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
release notes
This PR should be mentioned in the release notes.
standard library
This issue involves writing Zig code for the standard library.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #11650
Release Notes:
In Zig 0.14,
@fence
has been removed.@fence
was provided to be consistent with the C11 memory model, however, it complicates semantics by modifying the memory orderings of all previous and future atomic operations. This creates unforeseen constraints that are hard to model in a sanitizer. Fences can be substituted by either upgrading atomic memory orderings or adding new atomic operations.The most common use cases for
@fence
can be replaced by utilizing stronger memory orderings or by introducing a new atomic variable.StoreLoad Barriers
The most common use case is
@fence(.seq_cst)
. This is primarily used to ensure a consistent order between multiple operations on different atomic variables.For example:
The goal is to ensure either
load X
(D) seesstore X
(A), orload Y
(B) seesstore Y
(C). The pair of Sequentially Consistent fences guarantees this via two invariance.Now that
@fence
is removed, there are other ways of achieving this relationship:SeqCst
, including them all in the total order.Acquire
and its matching load (D/B)Release
. Semantically, this would mean upgrading them to read-modify-write operations, which could be such ordering. Loads can be replaced with a non-mutating RMW, i.e.fetchAdd(0)
orfetchOr(0)
.Optimizers like LLVM may reduce this into a
@fence(.seq_cst) + load
internally.Conditional Barriers
Another use case for fences is conditionally creating a synchronizes-with relationship with previous or future atomic operations, using
Acquire
orRelease
respectively. A simple example of this in the real world is an atomic reference counter:The load in the
fetchSub(1)
only needs to beAcquire
for the last ref-count decrement to ensure previous decrementshappen-before the
deinit()
. The@fence(.acquire)
here creates this relationship using the load part of thefetchSub(1)
.Without
@fence
, there are two approaches here:For example:
For example:
The
Acquire
will synchronize-with the longest release-sequence inrc
's modification order, making all previous decrements happen-before thedeinit()
.Synchronize External Operations
The least common usage of
@fence
is providing additional synchronization to atomic operations the programmer has no control over (i.e. external function calls). Using a@fence
in this situation relies on the "hidden" functions having atomic operations with undesirably weak orderings.Ideally, the "hidden" functions would be accessible to the user and they could simply increase the order in the source code. But if this isn't possible, a last resort is introducing an atomic variable to simulate the fence's barriers. For example: