Struct cros_async::CancellableBlockingPool
source · 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
impl CancellableBlockingPool
const RETRY_COUNT: usize = 10usize
const SLEEP_DURATION: Duration = _
sourcepub fn new(max_threads: usize, keepalive: Duration) -> CancellableBlockingPool
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()
sourcepub fn with_capacity(
max_threads: usize,
keepalive: Duration,
) -> CancellableBlockingPool
pub fn with_capacity( max_threads: usize, keepalive: Duration, ) -> CancellableBlockingPool
Like Self::new but with pre-allocating capacity for up to max_threads
.
sourcepub fn spawn<F, R, G>(&self, f: F, cancel: G) -> impl Future<Output = R>
pub fn spawn<F, R, G>(&self, f: F, cancel: G) -> impl Future<Output = R>
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);
sourcefn drain_cancellables(&self)
fn drain_cancellables(&self)
Iterates over all the queued tasks and marks them as cancelled.
sourcepub fn disarm(&self)
pub fn disarm(&self)
Marks all the queued and in-flight tasks as cancelled. Any tasks queued after disarm
ing
will be cancelled.
Does not wait for all the tasks to get cancelled.
sourcepub fn shutdown(&self) -> Result<(), Error>
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 await
ing on the Task
for that work will panic.
fn shutdown_with_timeout(&self, timeout: Duration) -> Result<(), Error>
Trait Implementations§
source§impl Clone for CancellableBlockingPool
impl Clone for CancellableBlockingPool
source§fn clone(&self) -> CancellableBlockingPool
fn clone(&self) -> CancellableBlockingPool
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Default for CancellableBlockingPool
impl Default for CancellableBlockingPool
source§fn default() -> CancellableBlockingPool
fn default() -> CancellableBlockingPool
Auto Trait Implementations§
impl Freeze for CancellableBlockingPool
impl RefUnwindSafe for CancellableBlockingPool
impl Send for CancellableBlockingPool
impl Sync for CancellableBlockingPool
impl Unpin for CancellableBlockingPool
impl UnwindSafe for CancellableBlockingPool
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)