authorgravatar for charles.saternos@gmail.comnc <charles.saternos@gmail.com> 2022-11-26 13:41:12-05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-05 17:01:56+02:00
loga7cb5027ae797182b5327d32b91027a883cc03d0
tree1310a29b8e173a0e4234ed38f9b8b451da64352b
parent8796da028320b97a4b67a401109dce1137ee2bbf

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


1 files changed, 18 insertions(+), 2 deletions(-)

lib/std/atomic/queue.zig+18-2
......@@ -64,6 +64,8 @@ pub fn Queue(comptime T: type) type {
6464 return head;
6565 }
6666
67 /// Prepends `node` to the front of the queue.
68 /// The lifetime of `node` must be longer than the lifetime of the queue.
6769 pub fn unget(self: *Self, node: *Node) void {
6870 node.prev = null;
6971
......@@ -72,8 +74,8 @@ pub fn Queue(comptime T: type) type {
7274
7375 const opt_head = self.head;
7476 self.head = node;
75 if (opt_head) |head| {
76 head.next = node;
77 if (opt_head) |old_head| {
78 node.next = old_head;
7779 } else {
7880 assert(self.tail == null);
7981 self.tail = node;
......@@ -330,11 +332,25 @@ test "std.atomic.Queue single-threaded" {
330332 node_3.next = null;
331333 try expect(!queue.isEmpty());
332334
335 queue.unget(&node_3);
336 try expect(queue.get().?.data == 3);
337 try expect(!queue.isEmpty());
338
333339 try expect(queue.get().?.data == 4);
334340 try expect(queue.isEmpty());
335341
336342 try expect(queue.get() == null);
337343 try expect(queue.isEmpty());
344
345 // unget an empty queue
346 queue.unget(&node_4);
347 try expect(queue.tail == &node_4);
348 try expect(queue.head == &node_4);
349
350 try expect(queue.get().?.data == 4);
351
352 try expect(queue.get() == null);
353 try expect(queue.isEmpty());
338354}
339355
340356test "std.atomic.Queue dump" {