authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2020-04-04 15:45:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-04 13:47:07-04:00
log12cdea452524e4b852cb433728c834a7b0203b67
tree3eb22db080525a13112501c5c7cc32a87502ebb8
parentcf8728aabd4ed7190a912579fcca657ee5f037e8

Adds some documentation to std.atomic.Queue.


1 files changed, 19 insertions(+), 1 deletions(-)

lib/std/atomic/queue.zig+19-1
......@@ -5,6 +5,8 @@ const expect = std.testing.expect;
55
66/// Many producer, many consumer, non-allocating, thread-safe.
77/// Uses a mutex to protect access.
8/// The queue does not manage ownership and the user is responsible to
9/// manage the storage of the nodes.
810pub fn Queue(comptime T: type) type {
911 return struct {
1012 head: ?*Node,
......@@ -14,6 +16,8 @@ pub fn Queue(comptime T: type) type {
1416 pub const Self = @This();
1517 pub const Node = std.TailQueue(T).Node;
1618
19 /// Initializes a new queue. The queue does not provide a `deinit()`
20 /// function, so the user must take care of cleaning up the queue elements.
1721 pub fn init() Self {
1822 return Self{
1923 .head = null,
......@@ -22,6 +26,8 @@ pub fn Queue(comptime T: type) type {
2226 };
2327 }
2428
29 /// Appends `node` to the queue.
30 /// The lifetime of `node` must be longer than lifetime of queue.
2531 pub fn put(self: *Self, node: *Node) void {
2632 node.next = null;
2733
......@@ -38,6 +44,9 @@ pub fn Queue(comptime T: type) type {
3844 }
3945 }
4046
47 /// Gets a previously inserted node or returns `null` if there is none.
48 /// It is safe to `get()` a node from the queue while another thread tries
49 /// to `remove()` the same node at the same time.
4150 pub fn get(self: *Self) ?*Node {
4251 const held = self.mutex.acquire();
4352 defer held.release();
......@@ -71,7 +80,9 @@ pub fn Queue(comptime T: type) type {
7180 }
7281 }
7382
74 /// Thread-safe with get() and remove(). Returns whether node was actually removed.
83 /// Removes a node from the queue, returns whether node was actually removed.
84 /// It is safe to `remove()` a node from the queue while another thread tries
85 /// to `get()` the same node at the same time.
7586 pub fn remove(self: *Self, node: *Node) bool {
7687 const held = self.mutex.acquire();
7788 defer held.release();
......@@ -95,16 +106,23 @@ pub fn Queue(comptime T: type) type {
95106 return true;
96107 }
97108
109 /// Returns `true` if the queue is currently empty.
110 /// Note that in a multi-consumer environment a return value of `false`
111 /// does not mean that `get` will yield a non-`null` value!
98112 pub fn isEmpty(self: *Self) bool {
99113 const held = self.mutex.acquire();
100114 defer held.release();
101115 return self.head == null;
102116 }
103117
118 /// Dumps the contents of the queue to `stderr`.
104119 pub fn dump(self: *Self) void {
105120 self.dumpToStream(std.io.getStdErr().outStream()) catch return;
106121 }
107122
123 /// Dumps the contents of the queue to `stream`.
124 /// Up to 4 elements from the head are dumped and the tail of the queue is
125 /// dumped as well.
108126 pub fn dumpToStream(self: *Self, stream: var) !void {
109127 const S = struct {
110128 fn dumpRecursive(