Skip to main content

WorkQueue

Struct WorkQueue 

Source
pub struct WorkQueue<T: Send + 'static> { /* private fields */ }
Expand description

A work-stealing work queue for distributing tasks across multiple workers.

WorkQueue<T> is Clone — all clones share the same underlying state, so tasks pushed from one clone are visible to all others.

§Thread safety

WorkQueue<T> is Send + Sync when T: Send. Multiple threads may call push and steal concurrently without external synchronisation.

§Examples

use oximedia_core::work_queue_ws::WorkQueue;
use std::thread;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

let wq = WorkQueue::<u32>::new(4);
for i in 0..100_u32 {
    wq.push(i);
}

let total = Arc::new(AtomicUsize::new(0));
let mut handles = Vec::new();

for _ in 0..4 {
    let wq2 = wq.clone();
    let count = Arc::clone(&total);
    handles.push(thread::spawn(move || {
        while let Some(_task) = wq2.steal() {
            count.fetch_add(1, Ordering::Relaxed);
        }
    }));
}
for h in handles { h.join().expect("thread panicked"); }
assert_eq!(total.load(Ordering::Relaxed), 100);

Implementations§

Source§

impl<T: Send + 'static> WorkQueue<T>

Source

pub fn new(workers: usize) -> Self

Creates a new WorkQueue with workers local deques.

workers controls the number of distinct steal handles. A value of 0 is clamped to 1.

§Examples
use oximedia_core::work_queue_ws::WorkQueue;

let wq = WorkQueue::<i32>::new(4);
assert_eq!(wq.len(), 0);
Source

pub fn push(&self, task: T)

Pushes a task into the global injection queue.

§Examples
use oximedia_core::work_queue_ws::WorkQueue;

let wq = WorkQueue::<u32>::new(2);
wq.push(42_u32);
assert_eq!(wq.len(), 1);
Source

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

Attempts to steal a task from any available source.

The implementation first drains the global injector into a local worker deque (slot 0), then tries to pop from each worker in round-robin order, retrying on contention.

Returns None when all queues appear empty.

§Examples
use oximedia_core::work_queue_ws::WorkQueue;

let wq = WorkQueue::<u32>::new(2);
wq.push(1_u32);
wq.push(2_u32);
let t1 = wq.steal();
let t2 = wq.steal();
assert!(t1.is_some());
assert!(t2.is_some());
Source

pub fn len(&self) -> usize

Returns the approximate number of tasks currently in the queue.

This value may be slightly stale due to concurrent operations. It saturates at zero rather than going negative.

§Examples
use oximedia_core::work_queue_ws::WorkQueue;

let wq = WorkQueue::<u32>::new(2);
wq.push(1_u32);
wq.push(2_u32);
assert_eq!(wq.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the queue appears empty.

Trait Implementations§

Source§

impl<T: Clone + Send + 'static> Clone for WorkQueue<T>

Source§

fn clone(&self) -> WorkQueue<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for WorkQueue<T>

§

impl<T> RefUnwindSafe for WorkQueue<T>
where T: RefUnwindSafe,

§

impl<T> Send for WorkQueue<T>

§

impl<T> Sync for WorkQueue<T>

§

impl<T> Unpin for WorkQueue<T>

§

impl<T> UnsafeUnpin for WorkQueue<T>

§

impl<T> UnwindSafe for WorkQueue<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.