summaryrefslogtreecommitdiff
path: root/src/trace/Ring.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/Ring.zig')
-rw-r--r--src/trace/Ring.zig9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/trace/Ring.zig b/src/trace/Ring.zig
index 0bde33a..93b6314 100644
--- a/src/trace/Ring.zig
+++ b/src/trace/Ring.zig
@@ -126,8 +126,11 @@ pub fn emit(r: *Ring, ev: Event, payload: []const u8) void {
}
var at = h.head.load(.monotonic);
while (true) {
- if (at + need - r.seen_tail.load(.monotonic) > h.capacity) r.seen_tail.store(h.tail.load(.acquire), .monotonic);
- if (at + need - r.seen_tail.load(.monotonic) > h.capacity) {
+ // A tail past `at` means `at` is stale (others reserved, and were
+ // consumed, since it was loaded): not full, and the exchange below
+ // fails and loads the head again. Hence the saturating subtractions.
+ if (at + need -| r.seen_tail.load(.monotonic) > h.capacity) r.seen_tail.store(h.tail.load(.acquire), .monotonic);
+ if (at + need -| r.seen_tail.load(.monotonic) > h.capacity) {
if (h.lossy.load(.monotonic) != 0) {
_ = h.dropped.fetchAdd(1, .monotonic);
return;
@@ -136,7 +139,7 @@ pub fn emit(r: *Ring, ev: Event, payload: []const u8) void {
// between the check and the wait.
const seq = h.space_seq.load(.acquire);
_ = h.space_waiters.fetchAdd(1, .seq_cst);
- if (at + need - h.tail.load(.seq_cst) > h.capacity) futexWait(&h.space_seq, seq, std.time.ns_per_ms);
+ if (at + need -| h.tail.load(.seq_cst) > h.capacity) futexWait(&h.space_seq, seq, std.time.ns_per_ms);
_ = h.space_waiters.fetchSub(1, .monotonic);
at = h.head.load(.monotonic);
continue;