this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

queue: add tryPush method, a nonblocking push attempt

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+20 -5
+20 -5
src/queue.zig
··· 42 42 return self.buf[i]; 43 43 } 44 44 45 + /// push an item into the queue. Blocks until the message has been put 46 + /// in the queue 45 47 pub fn push(self: *Self, item: T) void { 46 48 self.mutex.lock(); 47 49 defer self.mutex.unlock(); ··· 59 61 self.buf[i] = item; 60 62 } 61 63 64 + /// push an item into the queue. If the queue is full, this returns 65 + /// immediately and the item has not been place in the queue 66 + pub fn tryPush(self: *Self, item: T) bool { 67 + self.mutex.lock(); 68 + if (self.isFull()) { 69 + self.mutex.unlock(); 70 + return false; 71 + } 72 + self.mutex.unlock(); 73 + self.push(item); 74 + return true; 75 + } 76 + 62 77 /// Returns `true` if the ring buffer is empty and `false` otherwise. 63 - pub fn isEmpty(self: Self) bool { 78 + fn isEmpty(self: Self) bool { 64 79 return self.write_index == self.read_index; 65 80 } 66 81 67 82 /// Returns `true` if the ring buffer is full and `false` otherwise. 68 - pub fn isFull(self: Self) bool { 83 + fn isFull(self: Self) bool { 69 84 return self.mask2(self.write_index + self.buf.len) == self.read_index; 70 85 } 71 86 72 87 /// Returns the length 73 - pub fn len(self: Self) usize { 88 + fn len(self: Self) usize { 74 89 const wrap_offset = 2 * self.buf.len * @intFromBool(self.write_index < self.read_index); 75 90 const adjusted_write_index = self.write_index + wrap_offset; 76 91 return adjusted_write_index - self.read_index; 77 92 } 78 93 79 94 /// Returns `index` modulo the length of the backing slice. 80 - pub fn mask(self: Self, index: usize) usize { 95 + fn mask(self: Self, index: usize) usize { 81 96 return index % self.buf.len; 82 97 } 83 98 84 99 /// Returns `index` modulo twice the length of the backing slice. 85 - pub fn mask2(self: Self, index: usize) usize { 100 + fn mask2(self: Self, index: usize) usize { 86 101 return index % (2 * self.buf.len); 87 102 } 88 103 };