wasmer_wasix::runtime::task_manager::tokio

Struct ThreadPool

Source
pub struct ThreadPool { /* private fields */ }
Available on crate feature sys-thread only.

Methods from Deref<Target = ThreadPool>§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn spawn(&self, future: impl Future<Output = ()> + Send + 'static)

Available on crate feature async only.

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.

Source

pub fn try_spawn( &self, future: impl Future<Output = ()> + Send + 'static, ) -> Result<(), SendError<Box<dyn FnOnce() + Send>>>

Available on crate feature async only.

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.

Source

pub fn spawn_await<R>( &self, future: impl Future<Output = R> + Send + 'static, ) -> JoinHandle<R>
where R: Send + 'static,

Available on crate feature async only.

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.

Source

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,

Available on crate feature async only.

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.

Source

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

Source

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

Source

pub fn get_name(&self) -> &str

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

Source

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

Source§

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

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

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

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§

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§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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

impl<T> LayoutRaw for T

Source§

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

Returns the layout of the type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Pointee for T

Source§

type Metadata = ()

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

Source§

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

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

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

upcast ref
Source§

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

upcast mut ref
Source§

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

upcast boxed dyn
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,

Source§

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

Source§

impl<T> MaybeSendSync for T