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

This commit is contained in:
nc 2022-11-26 13:41:12 -05:00 committed by Veikka Tuominen
parent 8796da0283
commit a7cb5027ae

View File

@ -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" {