Struct SegQueue
pub struct SegQueue<T> { /* private fields */ }Expand description
An unbounded lock-free queue and its pop error, from the kovan-queue crate: see its docs. An unbounded, lock-free, linearizable FIFO queue for many producers and many consumers.
Values come out in one total order consistent with real time: a push that returned before another push started comes out first, so one producer’s values always come out in the order it pushed them.
use kovan_queue::seg_queue::SegQueue;
let q = SegQueue::new();
q.push(1);
q.push(2);
assert_eq!(q.pop(), Some(1));
assert_eq!(q.pop(), Some(2));
assert_eq!(q.pop(), None);Implementations§
§impl<T> SegQueue<T>
impl<T> SegQueue<T>
pub fn push(&self, value: T)
pub fn push(&self, value: T)
Pushes an element into the queue.
pub fn pop(&self) -> Option<T>
pub fn pop(&self) -> Option<T>
Pops an element from the queue.
Returns None when the queue held no value at some moment during the
call. Never waits for a producer: a cell whose producer has claimed it
but not written is taken out of play, and that producer pushes again.
pub fn try_pop(&self) -> Result<T, TryPopError>
pub fn try_pop(&self) -> Result<T, TryPopError>
Pops an element without waiting and without making any producer push again.
Returns TryPopError::Empty when the queue held no value at some
moment during the call, and TryPopError::Busy when the queue’s
next cell was claimed by a push that had not written its value yet.
Busy lets a single consumer tell “nothing to wait for” from “a value
is about to land”: it can register a waker, check once more, and park
until the producer, after its push returns, wakes it.
use kovan_queue::seg_queue::{SegQueue, TryPopError};
let q = SegQueue::new();
assert_eq!(q.try_pop(), Err(TryPopError::Empty));
q.push(7);
assert_eq!(q.try_pop(), Ok(7));pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the queue.
Counts the cells claimed by pushes and not yet by pops, so a push in progress counts and a pop in progress does not. This is an approximation in concurrent scenarios: elements may be pushed or popped by other threads between the moment the length is read and the moment the caller acts on the returned value. With no operation in progress it is exact.
pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the queue is empty.
The answer pop would give at the same moment, without
taking anything: true when no value was in the queue at some moment
during the call, false when a push had claimed the next cell (a push
in progress counts, as it does for len). An
approximation in concurrent scenarios, exact with no operation in
progress.