AtomicQueue

Struct AtomicQueue 

Source
pub struct AtomicQueue<'a, T> { /* private fields */ }
Expand description

A thread safe, lock-free atomic queue. Objects are added to the queue via a closure and are owned by the queue as well This means objects in the queue will only be dropped when the queue’s backing buffer is also destroyed.

Implementations§

Source§

impl<'a, T> AtomicQueue<'a, T>


use rustpool::AtomicQueue;
use rustpool::RPQueue;
fn main() {
    // create a blocking queue with 3 elements set to value 0
    let q = AtomicQueue::<isize>::new(3, || 0);

    println!("simple blocking queue size {}", q.capacity());

    // take the first element
    let v1 = q.take().unwrap();

    println!("first element is {}", v1);

    // remove the remaining items
    let v2 = q.take().unwrap();
    let v3 = q.take().unwrap();

    // how many items available now?
    println!("items in the queue {}", q.available());

    // no more items to return but the atomic queue will never block
    let empty = q.take_wait(std::time::Duration::from_secs(1));

    assert_eq!(None, empty);

    // put everything back
    q.offer(v1);
    q.offer(v2);
    q.offer(v3);

    // how many items available now?
    println!("items in the queue {}", q.available());

    let new_value: isize = 1;

    // we can't add more items than the original capacity
    q.offer(&new_value);

    // we still have the same number of available items
    println!("items in the queue {}", q.available());
}
Source

pub fn new(size: usize, f: impl FnMut() -> T) -> Self

Create a new queue with fixed size size using custom create function

Examples found in repository?
examples/atomic_queue.rs (line 7)
5fn main() {
6    // create a blocking queue with 3 elements set to value 0
7    let q = AtomicQueue::<isize>::new(3, || 0);
8
9    println!("simple blocking queue size {}", q.capacity());
10
11    // take the first element
12    let v1 = q.take().unwrap();
13
14    println!("first element is {}", v1);
15
16    // remove the remaining items
17    let v2 = q.take().unwrap();
18    let v3 = q.take().unwrap();
19
20    // how many items available now?
21    println!("items in the queue {}", q.available());
22
23    // no more items to return but the atomic queue will never block
24    let empty = q.take_wait(std::time::Duration::from_secs(1));
25
26    assert_eq!(None, empty);
27
28    // put everything back
29    q.offer(v1);
30    q.offer(v2);
31    q.offer(v3);
32
33    // how many items available now?
34    println!("items in the queue {}", q.available());
35
36    let new_value: isize = 1;
37
38    // we can't add more items than the original capacity
39    q.offer(&new_value);
40
41    // we still have the same number of available items
42    println!("items in the queue {}", q.available());
43}
Source

pub fn new_arc(size: usize, f: impl FnMut() -> T) -> Arc<Self>

Create a new queue with fixed size wrapped as an atomic reference counter using a custom create function

Trait Implementations§

Source§

impl<'a, T> RPQueue<'a, T> for AtomicQueue<'a, T>

Source§

fn capacity(&self) -> usize

Returns the size of the queue in total number objects originally allocated

Source§

fn available(&self) -> usize

Returns the number of available items in the buffer

Source§

fn take(&self) -> Option<&'a mut T>

Returns an object from the queue or None if the pool is empty

Source§

fn take_wait(&self, _timeout: Duration) -> Option<&'a mut T>

This method behaves the same as AtomicQueue::take as the atomic implementation will never block if the pool is empty

Source§

fn offer(&self, item: &'a T) -> usize

Returns an object to the pool

Source§

impl<'a, T> Send for AtomicQueue<'a, T>

Source§

impl<'a, T> Sync for AtomicQueue<'a, T>

Auto Trait Implementations§

§

impl<'a, T> !Freeze for AtomicQueue<'a, T>

§

impl<'a, T> RefUnwindSafe for AtomicQueue<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Unpin for AtomicQueue<'a, T>

§

impl<'a, T> UnwindSafe for AtomicQueue<'a, T>
where T: RefUnwindSafe,

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.