feat(channel): impl peek()

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-05-23 01:38:15 -05:00
parent c1388c7318
commit 72349459ec
1 changed files with 10 additions and 0 deletions

View File

@ -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);