mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
std.atomic.Queue: fix unget implementation and add doc
This commit is contained in:
parent
8796da0283
commit
a7cb5027ae
@ -64,6 +64,8 @@ pub fn Queue(comptime T: type) type {
|
|||||||
return head;
|
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 {
|
pub fn unget(self: *Self, node: *Node) void {
|
||||||
node.prev = null;
|
node.prev = null;
|
||||||
|
|
||||||
@ -72,8 +74,8 @@ pub fn Queue(comptime T: type) type {
|
|||||||
|
|
||||||
const opt_head = self.head;
|
const opt_head = self.head;
|
||||||
self.head = node;
|
self.head = node;
|
||||||
if (opt_head) |head| {
|
if (opt_head) |old_head| {
|
||||||
head.next = node;
|
node.next = old_head;
|
||||||
} else {
|
} else {
|
||||||
assert(self.tail == null);
|
assert(self.tail == null);
|
||||||
self.tail = node;
|
self.tail = node;
|
||||||
@ -330,11 +332,25 @@ test "std.atomic.Queue single-threaded" {
|
|||||||
node_3.next = null;
|
node_3.next = null;
|
||||||
try expect(!queue.isEmpty());
|
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.get().?.data == 4);
|
||||||
try expect(queue.isEmpty());
|
try expect(queue.isEmpty());
|
||||||
|
|
||||||
try expect(queue.get() == null);
|
try expect(queue.get() == null);
|
||||||
try expect(queue.isEmpty());
|
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" {
|
test "std.atomic.Queue dump" {
|
||||||
|
Loading…
Reference in New Issue
Block a user