Skip to main content

Executor

Struct Executor 

Source
pub struct Executor { /* private fields */ }
Expand description

A thread pool-based executor for running asynchronous tasks.

The Executor manages a pool of worker threads that execute submitted tasks. It provides both synchronous and asynchronous draining capabilities, allowing you to wait for all tasks to complete before shutdown.

§Platform Support

This executor automatically adapts to the target platform:

  • On standard platforms, it uses OS threads via crossbeam-channel
  • On WASM targets, it uses web workers for parallelism

§Examples

use some_global_executor::Executor;

// Create an executor named "worker-pool" with 4 threads
let executor = Executor::new("worker-pool".to_string(), 4);

// Get the executor's name
assert_eq!(executor.name(), "worker-pool");

// Clean up when done
executor.drain();

Implementations§

Source§

impl Executor

Source

pub fn new(name: String, threads: usize) -> Self

Creates a new executor with the specified name and thread count.

§Arguments
  • name - A descriptive name for the executor, used for logging and debugging
  • threads - The number of worker threads to create in the thread pool
§Examples
use some_global_executor::Executor;

// Create an executor with 8 worker threads
let executor = Executor::new("high-performance".to_string(), 8);

// Clean up
executor.drain();
§Logging

This method logs the creation of the executor using the logwise framework, including the name and thread count for debugging purposes.

Source

pub fn name(&self) -> &str

Returns the name of this executor.

§Examples
use some_global_executor::Executor;

let executor = Executor::new("my-executor".to_string(), 2);
assert_eq!(executor.name(), "my-executor");
executor.drain();
Source

pub fn resize(&mut self, threads: usize)

Resizes the thread pool to the specified number of threads.

This method adjusts the number of worker threads in the executor’s thread pool. The exact behavior depends on the underlying platform implementation.

§Arguments
  • threads - The new number of worker threads
§Examples
use some_global_executor::Executor;

let mut executor = Executor::new("dynamic".to_string(), 2);

// Increase thread count for heavy workload
executor.resize(8);

// Later, reduce thread count
executor.resize(4);

executor.drain();
Source§

impl Executor

Source

pub fn new_default() -> Self

Creates a new executor with default settings.

This creates an executor named “default” with a platform-appropriate number of threads (determined by the underlying system implementation).

§Examples
use some_global_executor::Executor;

let executor = Executor::new_default();
assert_eq!(executor.name(), "default");
executor.drain();
Source

pub fn drain(self)

Synchronously waits for all tasks in the executor to complete.

This method blocks the current thread until all spawned tasks have finished execution. It uses a busy-wait loop with thread yielding to check for task completion.

§Performance

This method logs performance warnings if the drain operation takes an unexpectedly long time, which may indicate stuck or long-running tasks.

§Examples
use some_global_executor::Executor;
use some_executor::SomeExecutor;
use some_executor::task::{Task, Configuration};

let mut executor = Executor::new("sync-drain".to_string(), 2);

// Spawn a task
let task = Task::without_notifications(
    "quick-task".to_string(),
    Configuration::default(),
    async { 1 + 1 }
);
executor.spawn(task);

// Block until all tasks complete
executor.drain();
Source

pub fn drain_async(self) -> ExecutorDrain

Returns a future that completes when all tasks have finished.

Unlike drain(), this method returns immediately with an ExecutorDrain future that can be awaited. This allows for non-blocking waiting in async contexts.

§Examples
use some_global_executor::Executor;
use some_executor::SomeExecutor;
use some_executor::task::{Task, Configuration};

let mut executor = Executor::new("async-example".to_string(), 4);

// Spawn multiple tasks
for i in 0..5 {
    let task = Task::without_notifications(
        format!("task-{}", i),
        Configuration::default(),
        async move { i * 2 }
    );
    executor.spawn(task);
}

// Get the drain future (would be awaited in async context)
let drain_future = executor.drain_async();
// In async context: drain_future.await;
// For this example, convert back to executor and drain synchronously
let executor: Executor = drain_future.into();
executor.drain();
Source

pub fn set_as_global_executor(&self)

Sets this executor as the global executor.

After calling this method, this executor will be used as the default for global task spawning operations throughout the application. This is useful for libraries and applications that need a shared executor instance.

§Thread Safety

The executor is cloned when set as global, so the original instance remains independent and can still be used directly.

§Examples
use some_global_executor::Executor;

let executor = Executor::new("global".to_string(), 4);
executor.set_as_global_executor();

// The global executor is now available for use by any code
// that calls some_executor::global_executor::spawn()
Source

pub fn set_as_thread_executor(&self)

Sets this executor as the thread-local executor.

After calling this method, this executor will be used as the default for thread-local task spawning operations on the current thread. This is useful when different threads need different executor configurations.

§Thread Safety

The thread-local executor is only accessible from the thread that set it. Each thread can have its own thread-local executor.

§Examples
use some_global_executor::Executor;

let executor = Executor::new("thread-local".to_string(), 2);
executor.set_as_thread_executor();

// This thread now uses this executor for thread-local spawning
// via some_executor::thread_executor::spawn()

Trait Implementations§

Source§

impl AsMut<Executor> for ExecutorDrain

Provides mutable access to the underlying Executor from an ExecutorDrain.

This allows operations like resizing the thread pool even while draining.

§Examples

use some_global_executor::{Executor, ExecutorDrain};

let executor = Executor::new("test".to_string(), 2);
let mut drain = executor.drain_async();
drain.as_mut().resize(4);  // Resize while draining
Source§

fn as_mut(&mut self) -> &mut Executor

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<Executor> for ExecutorDrain

Provides immutable access to the underlying Executor from an ExecutorDrain.

§Examples

use some_global_executor::{Executor, ExecutorDrain};

let executor = Executor::new("test".to_string(), 2);
let drain = executor.drain_async();
let name = drain.as_ref().name();
assert_eq!(name, "test");
Source§

fn as_ref(&self) -> &Executor

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Executor

Source§

fn clone(&self) -> Executor

Returns a duplicate 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 Executor

Source§

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

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

impl From<Executor> for ExecutorDrain

Converts an Executor into an ExecutorDrain future.

This conversion is equivalent to calling Executor::drain_async().

§Examples

use some_global_executor::{Executor, ExecutorDrain};

let executor = Executor::new("test".to_string(), 2);
let drain: ExecutorDrain = executor.into();
// In async context: drain.await;
// For this example, convert back and drain:
let executor: Executor = drain.into();
executor.drain();
Source§

fn from(executor: Executor) -> Self

Converts to this type from the input type.
Source§

impl From<ExecutorDrain> for Executor

Converts an ExecutorDrain back to its underlying Executor.

This is useful when you need to access the executor after initiating a drain operation but before it completes.

§Examples

use some_global_executor::{Executor, ExecutorDrain};

let executor = Executor::new("test".to_string(), 2);
let drain: ExecutorDrain = executor.into();
let executor: Executor = drain.into();
executor.drain();
Source§

fn from(drain: ExecutorDrain) -> Self

Converts to this type from the input type.
Source§

impl Hash for Executor

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Executor

Source§

fn eq(&self, other: &Executor) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl SomeExecutor for Executor

Implementation of the SomeExecutor trait from the some_executor framework.

This implementation provides the core task spawning functionality, supporting both synchronous and asynchronous task submission with type-safe observers for monitoring task execution.

Source§

type ExecutorNotifier = Infallible

Design notes Read more
Source§

fn spawn<F: Future + Send + 'static, Notifier: ObserverNotified<F::Output> + Send>( &mut self, task: Task<F, Notifier>, ) -> impl Observer<Value = F::Output>
where Self: Sized, F::Output: Send,

Spawns a future onto the runtime. Read more
Source§

async fn spawn_async<F: Future + Send + 'static, Notifier: ObserverNotified<F::Output> + Send>( &mut self, task: Task<F, Notifier>, ) -> impl Observer<Value = F::Output>
where Self: Sized, F::Output: Send + Unpin,

Spawns a future onto the runtime. Read more
Source§

fn spawn_objsafe( &mut self, task: Task<Pin<Box<dyn Future<Output = Box<dyn Any + Send + 'static>> + Send + 'static>>, Box<dyn ObserverNotified<dyn Any + Send> + Send>>, ) -> Box<dyn Observer<Value = Box<dyn Any + Send>, Output = FinishedObservation<Box<dyn Any + Send>>> + Send>

Spawns a future onto the runtime. Read more
Source§

fn spawn_objsafe_async<'s>( &'s mut self, task: Task<Pin<Box<dyn Future<Output = Box<dyn Any + Send + 'static>> + Send + 'static>>, Box<dyn ObserverNotified<dyn Any + Send> + Send>>, ) -> Box<dyn Future<Output = Box<dyn Observer<Value = Box<dyn Any + Send>, Output = FinishedObservation<Box<dyn Any + Send>>> + Send>> + 's>

Spawns a future onto the runtime. Read more
Source§

fn clone_box(&self) -> Box<DynExecutor>

Clones the executor. Read more
Source§

fn executor_notifier(&mut self) -> Option<Self::ExecutorNotifier>

Produces an executor notifier.
Source§

impl Eq for Executor

Source§

impl StructuralPartialEq for Executor

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> 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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 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> 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.