feat(channel): impl peek()
This commit is contained in:
parent
c1388c7318
commit
72349459ec
10
src/lib.zig
10
src/lib.zig
|
@ -78,6 +78,16 @@ fn Channel(comptime T: type) type {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn peek(self: *const Self) ?T {
|
||||||
|
const read_idx = self.read.load(.Acquire);
|
||||||
|
const write_idx = self.write.load(.Acquire);
|
||||||
|
|
||||||
|
if (read_idx == write_idx) return null;
|
||||||
|
|
||||||
|
std.atomic.fence(.Acquire);
|
||||||
|
return self.buf[self.mask(read_idx)];
|
||||||
|
}
|
||||||
|
|
||||||
pub fn len(self: *const Self) Index {
|
pub fn len(self: *const Self) Index {
|
||||||
const read_idx = self.read.load(.Acquire);
|
const read_idx = self.read.load(.Acquire);
|
||||||
const write_idx = self.write.load(.Acquire);
|
const write_idx = self.write.load(.Acquire);
|
||||||
|
|
Loading…
Reference in New Issue