pub struct CancellableBlockingPool {
    inner: Arc<Inner>,
}
Expand description

A thread pool for running work that may block.

This is a wrapper around BlockingPool with an ability to cancel queued tasks. See BlockingPool for more info.

Examples

Spawn a task to run in the CancellableBlockingPool and await on its result.

use cros_async::CancellableBlockingPool;

    let pool = CancellableBlockingPool::default();
    let CANCELLED = 0;

    let res = pool.spawn(move || {
        // Do some CPU-intensive or blocking work here.

        42
    }, move || CANCELLED).await;

    assert_eq!(res, 42);

Fields§

§inner: Arc<Inner>

Implementations§

source§

impl CancellableBlockingPool

source

const RETRY_COUNT: usize = 10usize

source

const SLEEP_DURATION: Duration = _

source

pub fn new(max_threads: usize, keepalive: Duration) -> CancellableBlockingPool

Create a new CancellableBlockingPool.

When we try to shutdown or drop CancellableBlockingPool, it may happen that a hung thread might prevent CancellableBlockingPool pool from getting dropped. On failure to shutdown in watchdog_opts.timeout duration, CancellableBlockingPool can take an action specified by watchdog_opts.action.

See also: BlockingPool::new()

source

pub fn with_capacity( max_threads: usize, keepalive: Duration ) -> CancellableBlockingPool

Like Self::new but with pre-allocating capacity for up to max_threads.

source

pub fn spawn<F, R, G>(&self, f: F, cancel: G) -> impl Future<Output = R>where F: FnOnce() -> R + Send + 'static, R: Send + 'static, G: Fn() -> R + Send + 'static,

Spawn a task to run in the CancellableBlockingPool.

Callers may await the returned Task to be notified when the work is completed. Dropping the future will not cancel the task.

cancel helps to cancel a queued or in-flight operation f. cancel may be called more than once if f doesn’t respond to cancel. cancel is not called if f completes successfully. For example,

Examples
use {cros_async::CancellableBlockingPool, std::sync::{Arc, Mutex, Condvar}};

   let pool = CancellableBlockingPool::default();
   let cancelled: i32 = 1;
   let success: i32 = 2;

   let shared = Arc::new((Mutex::new(0), Condvar::new()));
   let shared2 = shared.clone();
   let shared3 = shared.clone();

   let res = pool
       .spawn(
           move || {
               let guard = shared.0.lock().unwrap();
               let mut guard = shared.1.wait_while(guard, |state| *state == 0).unwrap();
               if *guard != cancelled {
                   *guard = success;
               }
           },
           move || {
               *shared2.0.lock().unwrap() = cancelled;
               shared2.1.notify_all();
           },
       )
       .await;
   pool.shutdown();

   assert_eq!(*shared3.0.lock().unwrap(), cancelled);
source

fn drain_cancellables(&self)

Iterates over all the queued tasks and marks them as cancelled.

source

pub fn disarm(&self)

Marks all the queued and in-flight tasks as cancelled. Any tasks queued after disarming will be cancelled. Does not wait for all the tasks to get cancelled.

source

pub fn shutdown(&self) -> Result<(), Error>

Shut down the CancellableBlockingPool.

This will block until all work that has been started by the worker threads is finished. Any work that was added to the CancellableBlockingPool but not yet picked up by a worker thread will not complete and awaiting on the Task for that work will panic.

source

fn shutdown_with_timeout(&self, timeout: Duration) -> Result<(), Error>

Trait Implementations§

source§

impl Clone for CancellableBlockingPool

source§

fn clone(&self) -> CancellableBlockingPool

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Default for CancellableBlockingPool

source§

fn default() -> CancellableBlockingPool

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

impl Drop for CancellableBlockingPool

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.