Skip to content

Commit

Permalink
fix: 删除 fence 函数的引用 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia authored Oct 7, 2024
1 parent 8d8b49e commit ac7a577
Showing 1 changed file with 8 additions and 1 deletion.
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

0 comments on commit ac7a577

Please sign in to comment.