Struct wasmer_wasix::runtime::task_manager::tokio::ThreadPool
source · pub struct ThreadPool {
inner: ThreadPool,
}
Fields§
§inner: ThreadPool
Methods from Deref<Target = ThreadPool>§
pub fn get_current_worker_count(&self) -> usize
pub fn get_current_worker_count(&self) -> usize
Get the number of live workers, includes all workers waiting for work or executing tasks.
This counter is incremented when creating a new worker. The value is increment just before the worker starts executing its initial task. Incrementing the worker total might fail if the total has already reached the specified limit (either core_size or max_size) after being incremented by another thread, as of rusty_pool 0.5.0 failed attempts to create a worker no longer skews the worker total as failed attempts to increment the worker total does not increment the value at all. This counter is decremented when a worker reaches the end of its working loop, which for non-core threads might happen if it does not receive any work during its keep alive time, for core threads this only happens once the channel is disconnected.
pub fn get_idle_worker_count(&self) -> usize
pub fn get_idle_worker_count(&self) -> usize
Get the number of workers currently waiting for work. Those threads are currently
polling from the crossbeam receiver. Core threads wait indefinitely and might remain
in this state until the ThreadPool
is dropped. The remaining threads give up after
waiting for the specified keep_alive time.
pub fn execute<T>(&self, task: T)where
T: Task<()> + 'static,
pub fn execute<T>(&self, task: T)where
T: Task<()> + 'static,
Send a new task to the worker threads. This function is responsible for sending the message through the channel and creating new workers if needed. If the current worker count is lower than the core pool size this function will always create a new worker. If the current worker count is equal to or greater than the core pool size this function only creates a new worker if the worker count is below the max pool size and there are no idle threads.
When attempting to increment the total worker count before creating a worker fails due to the counter reaching the provided limit (core_size when attempting to create core thread, else max_size) after being incremented by another thread, the pool tries to create a non-core worker instead (if previously trying to create a core worker and no idle worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, either because the current value of the counter matched the expected value or because the last observed value was still below the limit, the worker starts with the provided task as initial task and spawns its thread.
§Panics
This function might panic if try_execute
returns an error when the crossbeam channel has been
closed unexpectedly.
This should never occur under normal circumstances using safe code, as shutting down the ThreadPool
consumes ownership and the crossbeam channel is never dropped unless dropping the ThreadPool
.
pub fn try_execute<T>(
&self,
task: T,
) -> Result<(), SendError<Box<dyn FnOnce() + Send>>>where
T: Task<()> + 'static,
pub fn try_execute<T>(
&self,
task: T,
) -> Result<(), SendError<Box<dyn FnOnce() + Send>>>where
T: Task<()> + 'static,
Send a new task to the worker threads. This function is responsible for sending the message through the channel and creating new workers if needed. If the current worker count is lower than the core pool size this function will always create a new worker. If the current worker count is equal to or greater than the core pool size this function only creates a new worker if the worker count is below the max pool size and there are no idle threads.
When attempting to increment the total worker count before creating a worker fails due to the counter reaching the provided limit (core_size when attempting to create core thread, else max_size) after being incremented by another thread, the pool tries to create a non-core worker instead (if previously trying to create a core worker and no idle worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, either because the current value of the counter matched the expected value or because the last observed value was still below the limit, the worker starts with the provided task as initial task and spawns its thread.
§Errors
This function might return crossbeam_channel::SendError
if the sender was dropped unexpectedly.
pub fn evaluate<R, T>(&self, task: T) -> JoinHandle<R>where
R: Send + 'static,
T: Task<R> + 'static,
pub fn evaluate<R, T>(&self, task: T) -> JoinHandle<R>where
R: Send + 'static,
T: Task<R> + 'static,
Send a new task to the worker threads and return a JoinHandle
that may be used to await
the result. This function is responsible for sending the message through the channel and creating new
workers if needed. If the current worker count is lower than the core pool size this function will always
create a new worker. If the current worker count is equal to or greater than the core pool size this
function only creates a new worker if the worker count is below the max pool size and there are no idle
threads.
When attempting to increment the total worker count before creating a worker fails due to the counter reaching the provided limit (core_size when attempting to create core thread, else max_size) after being incremented by another thread, the pool tries to create a non-core worker instead (if previously trying to create a core worker and no idle worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, either because the current value of the counter matched the expected value or because the last observed value was still below the limit, the worker starts with the provided task as initial task and spawns its thread.
§Panics
This function might panic if try_execute
returns an error when the crossbeam channel has been
closed unexpectedly.
This should never occur under normal circumstances using safe code, as shutting down the ThreadPool
consumes ownership and the crossbeam channel is never dropped unless dropping the ThreadPool
.
pub fn try_evaluate<R, T>(
&self,
task: T,
) -> Result<JoinHandle<R>, SendError<Box<dyn FnOnce() + Send>>>where
R: Send + 'static,
T: Task<R> + 'static,
pub fn try_evaluate<R, T>(
&self,
task: T,
) -> Result<JoinHandle<R>, SendError<Box<dyn FnOnce() + Send>>>where
R: Send + 'static,
T: Task<R> + 'static,
Send a new task to the worker threads and return a JoinHandle
that may be used to await
the result. This function is responsible for sending the message through the channel and creating new
workers if needed. If the current worker count is lower than the core pool size this function will always
create a new worker. If the current worker count is equal to or greater than the core pool size this
function only creates a new worker if the worker count is below the max pool size and there are no idle
threads.
When attempting to increment the total worker count before creating a worker fails due to the counter reaching the provided limit (core_size when attempting to create core thread, else max_size) after being incremented by another thread, the pool tries to create a non-core worker instead (if previously trying to create a core worker and no idle worker exists) or sends the task to the channel instead. If incrementing the counter succeeded, either because the current value of the counter matched the expected value or because the last observed value was still below the limit, the worker starts with the provided task as initial task and spawns its thread.
§Errors
This function might return crossbeam_channel::SendError
if the sender was dropped unexpectedly.
pub fn complete<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> JoinHandle<R>where
R: Send + 'static,
pub fn complete<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> JoinHandle<R>where
R: Send + 'static,
Send a task to the ThreadPool
that completes the given Future
and return a JoinHandle
that may be used to await the result. This function simply calls evaluate()
with a closure that calls block_on
with the provided future.
§Panic
This function panics if the task fails to be sent to the ThreadPool
due to the channel being broken.
pub fn try_complete<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Result<JoinHandle<R>, SendError<Box<dyn FnOnce() + Send>>>where
R: Send + 'static,
pub fn try_complete<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Result<JoinHandle<R>, SendError<Box<dyn FnOnce() + Send>>>where
R: Send + 'static,
Send a task to the ThreadPool
that completes the given Future
and return a JoinHandle
that may be used to await the result. This function simply calls try_evaluate()
with a closure that calls block_on
with the provided future.
§Errors
This function returns crossbeam_channel::SendError
if the task fails to be sent to the ThreadPool
due to the channel being broken.
pub fn spawn(&self, future: impl Future<Output = ()> + Send + 'static)
pub fn spawn(&self, future: impl Future<Output = ()> + Send + 'static)
Submit a Future
to be polled by this ThreadPool
. Unlike complete()
this does not
block a worker until the Future
has been completed but polls the Future
once at a time and creates a Waker
that re-submits the Future to this pool when awakened. Since Arc<AsyncTask>
implements the Task
trait this
function simply constructs the AsyncTask
and calls execute()
.
§Panic
This function panics if the task fails to be sent to the ThreadPool
due to the channel being broken.
pub fn try_spawn(
&self,
future: impl Future<Output = ()> + Send + 'static,
) -> Result<(), SendError<Box<dyn FnOnce() + Send>>>
pub fn try_spawn( &self, future: impl Future<Output = ()> + Send + 'static, ) -> Result<(), SendError<Box<dyn FnOnce() + Send>>>
Submit a Future
to be polled by this ThreadPool
. Unlike try_complete()
this does not
block a worker until the Future
has been completed but polls the Future
once at a time and creates a Waker
that re-submits the Future to this pool when awakened. Since Arc<AsyncTask>
implements the Task
trait this
function simply constructs the AsyncTask
and calls try_execute()
.
§Errors
This function returns crossbeam_channel::SendError
if the task fails to be sent to the ThreadPool
due to the channel being broken.
pub fn spawn_await<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> JoinHandle<R>where
R: Send + 'static,
pub fn spawn_await<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> JoinHandle<R>where
R: Send + 'static,
Create a top-level Future
that awaits the provided Future
and then sends the result to the
returned JoinHandle
. Unlike complete()
this does not
block a worker until the Future
has been completed but polls the Future
once at a time and creates a Waker
that re-submits the Future to this pool when awakened. Since Arc<AsyncTask>
implements the Task
trait this
function simply constructs the AsyncTask
and calls execute()
.
This enables awaiting the final result outside of an async context like complete()
while still
polling the future lazily instead of eagerly blocking the worker until the future is done.
§Panic
This function panics if the task fails to be sent to the ThreadPool
due to the channel being broken.
pub fn try_spawn_await<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Result<JoinHandle<R>, SendError<Box<dyn FnOnce() + Send>>>where
R: Send + 'static,
pub fn try_spawn_await<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Result<JoinHandle<R>, SendError<Box<dyn FnOnce() + Send>>>where
R: Send + 'static,
Create a top-level Future
that awaits the provided Future
and then sends the result to the
returned JoinHandle
. Unlike try_complete()
this does not
block a worker until the Future
has been completed but polls the Future
once at a time and creates a Waker
that re-submits the Future to this pool when awakened. Since Arc<AsyncTask>
implements the Task
trait this
function simply constructs the AsyncTask
and calls try_execute()
.
This enables awaiting the final result outside of an async context like complete()
while still
polling the future lazily instead of eagerly blocking the worker until the future is done.
§Errors
This function returns crossbeam_channel::SendError
if the task fails to be sent to the ThreadPool
due to the channel being broken.
pub fn join(&self)
pub fn join(&self)
Blocks the current thread until there aren’t any non-idle threads anymore.
This includes work started after calling this function.
This function blocks until the next time this ThreadPool
completes all of its work,
except if all threads are idle and the channel is empty at the time of calling this
function, in which case it will fast-return.
This utilizes a Condvar
that is notified by workers when they complete a job and notice
that the channel is currently empty and it was the last thread to finish the current
generation of work (i.e. when incrementing the idle worker counter brings the value
up to the total worker counter, meaning it’s the last thread to become idle).
pub fn join_timeout(&self, time_out: Duration)
pub fn join_timeout(&self, time_out: Duration)
Blocks the current thread until there aren’t any non-idle threads anymore or until the
specified time_out Duration passes, whichever happens first.
This includes work started after calling this function.
This function blocks until the next time this ThreadPool
completes all of its work,
(or until the time_out is reached) except if all threads are idle and the channel is
empty at the time of calling this function, in which case it will fast-return.
This utilizes a Condvar
that is notified by workers when they complete a job and notice
that the channel is currently empty and it was the last thread to finish the current
generation of work (i.e. when incrementing the idle worker counter brings the value
up to the total worker counter, meaning it’s the last thread to become idle).
pub fn start_core_threads(&self)
pub fn start_core_threads(&self)
Starts all core workers by creating core idle workers until the total worker count reaches the core count.
Returns immediately if the current worker count is already >= core size.
Trait Implementations§
source§impl Clone for ThreadPool
impl Clone for ThreadPool
source§fn clone(&self) -> ThreadPool
fn clone(&self) -> ThreadPool
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ThreadPool
impl Debug for ThreadPool
Auto Trait Implementations§
impl Freeze for ThreadPool
impl RefUnwindSafe for ThreadPool
impl Send for ThreadPool
impl Sync for ThreadPool
impl Unpin for ThreadPool
impl UnwindSafe for ThreadPool
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
)§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more