zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit a7cb5027ae797182b5327d32b91027a883cc03d0 (tree)
parent 8796da028320b97a4b67a401109dce1137ee2bbf
Author: nc <charles.saternos@gmail.com>
Date:   Sat, 26 Nov 2022 13:41:12 -0500

std.atomic.Queue: fix unget implementation and add doc

Diffstat:
Mlib/std/atomic/queue.zig | 20++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/lib/std/atomic/queue.zig b/lib/std/atomic/queue.zig @@ -64,6 +64,8 @@ pub fn Queue(comptime T: type) type { return head; } + /// Prepends `node` to the front of the queue. + /// The lifetime of `node` must be longer than the lifetime of the queue. pub fn unget(self: *Self, node: *Node) void { node.prev = null; @@ -72,8 +74,8 @@ pub fn Queue(comptime T: type) type { const opt_head = self.head; self.head = node; - if (opt_head) |head| { - head.next = node; + if (opt_head) |old_head| { + node.next = old_head; } else { assert(self.tail == null); self.tail = node; @@ -330,11 +332,25 @@ test "std.atomic.Queue single-threaded" { node_3.next = null; try expect(!queue.isEmpty()); + queue.unget(&node_3); + try expect(queue.get().?.data == 3); + try expect(!queue.isEmpty()); + try expect(queue.get().?.data == 4); try expect(queue.isEmpty()); try expect(queue.get() == null); try expect(queue.isEmpty()); + + // unget an empty queue + queue.unget(&node_4); + try expect(queue.tail == &node_4); + try expect(queue.head == &node_4); + + try expect(queue.get().?.data == 4); + + try expect(queue.get() == null); + try expect(queue.isEmpty()); } test "std.atomic.Queue dump" {