Init
This commit is contained in:
commit
e70a04ac16
19 changed files with 576 additions and 0 deletions
35
src/msg_queue/queue.zig
Normal file
35
src/msg_queue/queue.zig
Normal file
|
@ -0,0 +1,35 @@
|
|||
const std = @import("std");
|
||||
|
||||
const mpsc = @import("../mpsc.zig");
|
||||
|
||||
pub fn Queue(comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
ring_buffer: mpsc.RingBuffer(T),
|
||||
|
||||
pub fn init(buffer: []mpsc.Slot(T)) Self {
|
||||
return .{
|
||||
.ring_buffer = mpsc.RingBuffer(T).init(buffer),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn blockingEnqueue(self: *Self, value: T) void {
|
||||
while (true) {
|
||||
self.ring_buffer.enqueue(value) catch |err| switch (err) {
|
||||
error.Overflow => {
|
||||
std.atomic.spinLoopHint();
|
||||
continue;
|
||||
},
|
||||
};
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dequeue(self: *Self) ?T {
|
||||
return self.ring_buffer.dequeue() catch |err| switch (err) {
|
||||
error.Underflow => return null,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue