FlipQueue

Struct FlipQueue 

Source
pub struct FlipQueue<T, L = RawRwLock> { /* private fields */ }
Expand description

Implements flexible flip-queue data structure. This data structure allows either push values into the queue or pop them from it like a regular queue. The advantage FlipQueue is that it allows to push values from multiple threads in parallel. But only when no values are popped from the queue.

Pushing requires shared borrow and popping requires exclusive borrow. This guarantees that above requirement is met.

When queue is at full capacity it is grown by doubling its size. Pushing thread will have to lock the queue to grow it.

When capacity is enough to push all values, then pushing is guaranteed to be wait-free. Popping and draining is wait-free since it uses exclusive borrow.

Typical use case of flip-queue is to broadcast &FlipQueue to multiple tasks and collect values, and then after all tasks are finished drain the queue to process collected values.

Implementations§

Source§

impl<T, L> FlipQueue<T, L>
where L: RawRwLock,

Source

pub fn clear(&mut self)

Clears the queue, removing all values.

Source§

impl<T, L> FlipQueue<T, L>
where L: RawRwLock,

Source

pub fn new() -> Self

Create new flip queue.

Source

pub fn with_capacity(cap: usize) -> Self

Create new flip queue.

Source

pub fn try_push_sync(&self, value: T) -> Result<(), T>

Tries to push value to the queue.

If at full capacity this will return the value back.

This function may block if concurrent call to push_sync grows the queue.

Source

pub fn push_sync(&self, value: T)

Push value to the queue.

If at full capacity this will lock the queue and grow it. Pushes that need not to grow the queue are executed in parallel.

Source

pub fn push(&mut self, value: T)

Source

pub fn try_pop_sync(&self) -> Option<T>

Tries to pop value from the queue.

Source

pub fn pop_sync(&self) -> Option<T>

Source

pub fn pop(&mut self) -> Option<T>

Source

pub fn drain(&mut self) -> Drain<'_, T>

Source

pub fn drain_locking<R>(&self, f: impl FnOnce(Drain<'_, T>) -> R) -> R

Source

pub fn swap_buffer(&self, ring: &mut RingBuffer<T>)

Lock the queue and swap its buffer with provided one.

This is preferred to draining it with locking if iteration can take significant time.

Trait Implementations§

Source§

impl<T, L> Default for FlipQueue<T, L>
where L: RawRwLock,

Source§

fn default() -> Self

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

impl<T, L> Send for FlipQueue<T, L>
where T: Send, L: Send,

Source§

impl<T, L> Sync for FlipQueue<T, L>
where T: Send, L: Sync,

Auto Trait Implementations§

§

impl<T, L = RawRwLock> !Freeze for FlipQueue<T, L>

§

impl<T, L = RawRwLock> !RefUnwindSafe for FlipQueue<T, L>

§

impl<T, L> Unpin for FlipQueue<T, L>
where L: Unpin,

§

impl<T, L> UnwindSafe for FlipQueue<T, L>
where L: UnwindSafe, T: UnwindSafe,

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.

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.