Skip to content

fix: 删除 fence 函数的引用 #189

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

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion course/code/14/atomic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ pub fn main() !void {
const RefCount = @This();

fn ref(rc: *RefCount) void {
// no synchronization necessary; just updating a counter.
_ = rc.count.fetchAdd(1, .monotonic);
}

fn unref(rc: *RefCount) void {
// release ensures code before unref() happens-before the
// count is decremented as dropFn could be called by then.
if (rc.count.fetchSub(1, .release) == 1) {
rc.count.fence(.acquire);
// seeing 1 in the counter means that other unref()s have happened,
// but it doesn't mean that uses before each unref() are visible.
// The load acquires the release-sequence created by previous unref()s
// in order to ensure visibility of uses before dropping.
_ = rc.count.load(.acquire);
(rc.dropFn)(rc);
}
}
Expand Down