Skip to main content

SegQueue

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>

pub fn new() -> SegQueue<T>

Creates a new unbounded queue.

pub fn push(&self, value: T)

Pushes an element into the queue.

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>

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

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

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.

Trait Implementations§

§

impl<T> Default for SegQueue<T>

§

fn default() -> SegQueue<T>

Returns the “default value” for a type. Read more
§

impl<T> Drop for SegQueue<T>

§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
§

impl<T> Send for SegQueue<T>
where T: Send,

§

impl<T> Sync for SegQueue<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> !Freeze for SegQueue<T>

§

impl<T> !RefUnwindSafe for SegQueue<T>

§

impl<T> !UnwindSafe for SegQueue<T>

§

impl<T> Unpin for SegQueue<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SegQueue<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.