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,
impl<T, L> FlipQueue<T, L>where
L: RawRwLock,
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Create new flip queue.
Sourcepub fn try_push_sync(&self, value: T) -> Result<(), T>
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.
Sourcepub fn push_sync(&self, value: T)
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.
pub fn push(&mut self, value: T)
Sourcepub fn try_pop_sync(&self) -> Option<T>
pub fn try_pop_sync(&self) -> Option<T>
Tries to pop value from the queue.
pub fn pop_sync(&self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain_locking<R>(&self, f: impl FnOnce(Drain<'_, T>) -> R) -> R
Sourcepub fn swap_buffer(&self, ring: &mut RingBuffer<T>)
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.