Skip to main content

ThreadpoolWork

Struct ThreadpoolWork 

Source
pub struct ThreadpoolWork { /* private fields */ }
Expand description

An owned thread-pool work object.

Each call to ThreadpoolWork::submit queues one invocation of the callback on the process thread pool. Multiple invocations may execute concurrently.

Drop calls WaitForThreadpoolWorkCallbacks (allowing in-flight callbacks to complete) before releasing the callback context, so the captured closure remains valid for the full lifetime of every callback execution.

§Examples

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use windows_threadpool_sys::work::ThreadpoolWork;

let total = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&total);

let work = ThreadpoolWork::new(move || {
    counter.fetch_add(1, Ordering::SeqCst);
}, None)?;

// Each submission queues one independent invocation; they may run
// concurrently, so the callback must tolerate that.
for _ in 0..8 {
    work.submit();
}
work.wait();

assert_eq!(total.load(Ordering::SeqCst), 8);

Implementations§

Source§

impl ThreadpoolWork

Source

pub fn new<F>( callback: F, env: Option<&mut CallbackEnviron<'_>>, ) -> Result<Self>
where F: Fn() + Send + Sync + 'static,

Creates a new work object that invokes callback each time it is submitted.

Pass Some(env) to associate a non-default callback environment; None uses the process-default pool with default priority.

Source

pub fn submit(&self)

Queues one invocation of the callback on the thread pool.

May be called repeatedly; each call queues an independent invocation. Multiple queued invocations may execute concurrently.

Source

pub fn wait(&self)

Blocks until all queued and in-progress invocations have completed.

This type has no separately-named synchronous close: this is the drain that Drop would otherwise perform, so calling it discharges the obligation Drop reports.

Source

pub fn stop_and_drain(&self)

Stop accepting work and block until none is queued or executing.

The drain every type in this crate offers under this name, so a caller tearing down a mixed set of objects can reach for one method.

There is nothing to stop on a work object – a submission cannot be withdrawn, only waited for – so this is exactly wait, which is why that method is documented as this type’s drain. The name exists because a caller should not have to know which of this crate’s types has something to stop.

Source

pub fn cancel_pending(&self)

Cancels callbacks that have not yet started, then waits for any currently-executing invocations to finish.

Settles the drain obligation, exactly as wait does and for the same reason: on return nothing is queued and nothing is executing, which is what the obligation tracks. It used not to, so a caller who submitted, cancelled, and dropped was told at drop that it still owed a drain – and under fail-fast that report is a panic, on a caller that had done nothing wrong.

Sound here, and deliberately not done on the wait or the timer. Those two can be re-armed from inside their own callbacks (crate::wait::WaitActivation::rearm, crate::timer::TimerFiring::rearm_after), so a callback running during the cancel can leave the object live again before this returns – which is why their stop_and_drain suppresses re-arms before draining and only then settles. A work callback is a bare Fn() with no handle to its object, so nothing can re-queue it except an explicit submit, and that arms the obligation again itself.

Trait Implementations§

Source§

impl Drop for ThreadpoolWork

Source§

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
Source§

impl Send for ThreadpoolWork

Source§

impl Sync for ThreadpoolWork

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.