From 360ae33e13e46f2cc505d258e3af2a848dc61b84 Mon Sep 17 00:00:00 2001 From: Hanaasagi Date: Mon, 21 Oct 2024 23:46:07 +0900 Subject: [PATCH] chore: align MPSC queue with standard head/tail naming Rename head and tail pointer operations in the MPSC queue to align with standard queue semantics. Producers now push to the tail, and the consumer pops from the head, ensuring correct behavior and improving code clarity. --- src/queue_mpsc.zig | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/queue_mpsc.zig b/src/queue_mpsc.zig index 68f4e8f..27f93ed 100644 --- a/src/queue_mpsc.zig +++ b/src/queue_mpsc.zig @@ -39,37 +39,37 @@ pub fn Intrusive(comptime T: type) type { /// of producers. pub fn push(self: *Self, v: *T) void { @atomicStore(?*T, &v.next, null, .unordered); - const prev = @atomicRmw(*T, &self.head, .Xchg, v, .acq_rel); + const prev = @atomicRmw(*T, &self.tail, .Xchg, v, .acq_rel); @atomicStore(?*T, &prev.next, v, .release); } /// Pop the first in element from the queue. This must be called /// by only a single consumer at any given time. pub fn pop(self: *Self) ?*T { - var tail = @atomicLoad(*T, &self.tail, .unordered); - var next_ = @atomicLoad(?*T, &tail.next, .acquire); - if (tail == &self.stub) { + var head = @atomicLoad(*T, &self.head, .unordered); + var next_ = @atomicLoad(?*T, &head.next, .acquire); + if (head == &self.stub) { const next = next_ orelse return null; - @atomicStore(*T, &self.tail, next, .unordered); - tail = next; - next_ = @atomicLoad(?*T, &tail.next, .acquire); + @atomicStore(*T, &self.head, next, .unordered); + head = next; + next_ = @atomicLoad(?*T, &head.next, .acquire); } if (next_) |next| { - @atomicStore(*T, &self.tail, next, .release); - tail.next = null; - return tail; + @atomicStore(*T, &self.head, next, .release); + head.next = null; + return head; } - const head = @atomicLoad(*T, &self.head, .unordered); - if (tail != head) return null; + const tail = @atomicLoad(*T, &self.tail, .unordered); + if (head != tail) return null; self.push(&self.stub); - next_ = @atomicLoad(?*T, &tail.next, .acquire); + next_ = @atomicLoad(?*T, &head.next, .acquire); if (next_) |next| { - @atomicStore(*T, &self.tail, next, .unordered); - tail.next = null; - return tail; + @atomicStore(*T, &self.head, next, .unordered); + head.next = null; + return head; } return null;