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

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

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,

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,

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,

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,

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,

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,

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)

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

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,

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,

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)

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)

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 get_name(&self) -> &str

Return the name of this pool, used as prefix for each worker thread.

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

source§

fn clone(&self) -> ThreadPool

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 Debug for ThreadPool

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for ThreadPool

§

type Target = ThreadPool

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.

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
§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

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

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> LayoutRaw for T

§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Pointee for T

§

type Metadata = ()

The metadata type for pointers and references to this type.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where 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 T
where 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 T
where 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.
§

impl<T> Upcastable for T
where T: Any + Debug + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

§

impl<T> Upcastable for T
where T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

upcast boxed dyn
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,