queue: update comments, add tryPop
Update comments on queue, and add a tryPop function which is nonblocking Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
parent
6461e6719c
commit
0d730aba80
1 changed files with 16 additions and 4 deletions
|
@ -22,6 +22,7 @@ pub fn Queue(
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
/// pop an item from the queue. Blocks until an item is available
|
||||||
pub fn pop(self: *Self) T {
|
pub fn pop(self: *Self) T {
|
||||||
self.mutex.lock();
|
self.mutex.lock();
|
||||||
defer self.mutex.unlock();
|
defer self.mutex.unlock();
|
||||||
|
@ -42,8 +43,8 @@ pub fn Queue(
|
||||||
return self.buf[i];
|
return self.buf[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// push an item into the queue. Blocks until the message has been put
|
/// push an item into the queue. Blocks until the item has been put in
|
||||||
/// in the queue
|
/// the queue
|
||||||
pub fn push(self: *Self, item: T) void {
|
pub fn push(self: *Self, item: T) void {
|
||||||
self.mutex.lock();
|
self.mutex.lock();
|
||||||
defer self.mutex.unlock();
|
defer self.mutex.unlock();
|
||||||
|
@ -61,8 +62,8 @@ pub fn Queue(
|
||||||
self.buf[i] = item;
|
self.buf[i] = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// push an item into the queue. If the queue is full, this returns
|
/// push an item into the queue. Returns true when the item was
|
||||||
/// immediately and the item has not been place in the queue
|
/// successfully placed in the queue
|
||||||
pub fn tryPush(self: *Self, item: T) bool {
|
pub fn tryPush(self: *Self, item: T) bool {
|
||||||
self.mutex.lock();
|
self.mutex.lock();
|
||||||
if (self.isFull()) {
|
if (self.isFull()) {
|
||||||
|
@ -74,6 +75,17 @@ pub fn Queue(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// pop an item from the queue. Returns null when no item is available
|
||||||
|
pub fn tryPop(self: *Self) ?T {
|
||||||
|
self.mutex.lock();
|
||||||
|
if (self.isEmpty()) {
|
||||||
|
self.mutex.unlock();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
self.mutex.unlock();
|
||||||
|
return self.pop();
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `true` if the ring buffer is empty and `false` otherwise.
|
/// Returns `true` if the ring buffer is empty and `false` otherwise.
|
||||||
fn isEmpty(self: Self) bool {
|
fn isEmpty(self: Self) bool {
|
||||||
return self.write_index == self.read_index;
|
return self.write_index == self.read_index;
|
||||||
|
|
Loading…
Reference in a new issue