Executor

Struct Executor 

Source
pub struct Executor<const P: usize = 10, const NUM_SEGS_P2: usize = 8> { /* private fields */ }
Expand description

Cooperative executor executor backed by MmapExecutorArena and worker threads.

The executor provides a high-performance async executor that uses:

  • Memory-mapped task arenas for efficient task storage
  • Worker threads for parallel task execution
  • Cooperative scheduling for optimal throughput

§Type Parameters

  • P: Priority levels (default: 10)
  • NUM_SEGS_P2: Number of segments as a power of 2 (default: 6, meaning 2^6 = 64 segments)

§Example

use maniac::runtime::Executor;
use maniac::runtime::task::{TaskArenaConfig, TaskArenaOptions};

let executor = Executor::new(
    TaskArenaConfig::new(1, 8).unwrap(),
    TaskArenaOptions::default(),
    4, // worker_count
).unwrap();

let handle = executor.spawn(async {
    // Your async code here
    42
}).unwrap();

// Later, await the result
// let result = handle.await;

Implementations§

Source§

impl<const P: usize, const NUM_SEGS_P2: usize> Executor<P, NUM_SEGS_P2>

Source

pub fn new_single_threaded() -> Self

Source§

impl<const P: usize, const NUM_SEGS_P2: usize> Executor<P, NUM_SEGS_P2>

Source

pub fn new( config: TaskArenaConfig, options: TaskArenaOptions, worker_count: usize, max_worker_count: usize, ) -> Result<Self>

Creates a new executor instance with the specified configuration.

This method initializes:

  1. A task arena with the given configuration and options
  2. A worker service with the specified number of worker threads
  3. Internal executor state for task management
§Arguments
  • config: Configuration for the task arena (capacity, layout, etc.)
  • options: Options for arena initialization (memory mapping, etc.)
  • worker_count: Number of worker threads to spawn for task execution
§Returns
  • Ok(Executor): Successfully created executor instance
  • Err(io::Error): Error if arena initialization fails (e.g., memory mapping issues)
§Example
use maniac::runtime::Executor;
use maniac::runtime::task::{TaskArenaConfig, TaskArenaOptions};

let executor = Executor::new(
    TaskArenaConfig::new(1, 8).unwrap(),
    TaskArenaOptions::default(),
    4, // Use 4 worker threads
)?;
Source

pub fn new_with_tick_service( config: TaskArenaConfig, options: TaskArenaOptions, worker_count: usize, max_worker_count: usize, tick_service: Arc<TickService>, ) -> Result<Self>

Creates a new executor instance with the specified configuration.

This method initializes:

  1. A task arena with the given configuration and options
  2. A worker service with the specified number of worker threads
  3. Internal executor state for task management
§Arguments
  • config: Configuration for the task arena (capacity, layout, etc.)
  • options: Options for arena initialization (memory mapping, etc.)
  • worker_count: Number of worker threads to spawn for task execution
§Returns
  • Ok(Executor): Successfully created executor instance
  • Err(io::Error): Error if arena initialization fails (e.g., memory mapping issues)
§Example
use maniac::runtime::Executor;
use maniac::runtime::task::{TaskArenaConfig, TaskArenaOptions};

let executor = Executor::new(
    TaskArenaConfig::new(1, 8).unwrap(),
    TaskArenaOptions::default(),
    4, // Use 4 worker threads
)?;
Source

pub fn spawn<F, T>(&self, future: F) -> Result<JoinHandle<T>, SpawnError>
where F: IntoFuture<Output = T> + Send + 'static, F::IntoFuture: Future<Output = T> + Send + 'static, T: Send + 'static,

Spawns an asynchronous task on the executor, returning an awaitable join handle.

This is the primary method for scheduling async work on the executor. The spawned task will be executed by one of the executor’s worker threads.

§Arguments
  • future: The future to execute. Can be any type that implements IntoFuture.
§Returns
  • Ok(JoinHandle<T>): A join handle that implements Future<Output = T>
  • Err(SpawnError): Error if spawning fails
§Example
let handle = executor.spawn(async {
    // Your async code here
    42
})?;

// Await the result
// let result = handle.await;
Source

pub fn stats(&self) -> TaskArenaStats

Returns current arena statistics.

This provides insight into the executor’s current state, including:

  • Number of active tasks
  • Task capacity and utilization
  • Other arena-specific metrics
§Returns

TaskArenaStats containing current executor statistics

Source

pub fn service(&self) -> &Arc<WorkerService<P, NUM_SEGS_P2>>

Returns a reference to the underlying worker service.

This allows direct access to worker service operations such as:

  • Interrupting workers for preemptive scheduling
  • Accessing worker statistics and health information
  • Managing worker threads dynamically
§Returns

An Arc to the WorkerService managing this executor’s workers

Trait Implementations§

Source§

impl<const P: usize, const NUM_SEGS_P2: usize> Clone for Executor<P, NUM_SEGS_P2>

Source§

fn clone(&self) -> Executor<P, NUM_SEGS_P2>

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<const P: usize, const NUM_SEGS_P2: usize> Drop for Executor<P, NUM_SEGS_P2>

Source§

fn drop(&mut self)

Cleans up the executor when it goes out of scope.

This implementation:

  1. Initiates shutdown of the executor (closes arena, signals workers)
  2. If this executor owns a tick service, shuts it down as well
  3. Unparks any worker threads that might be waiting
  4. Joins all worker threads to ensure clean shutdown

Note: Currently, worker threads are managed by WorkerService, so the workers vector is typically empty. This code is prepared for future scenarios where we might manage worker threads directly.

Auto Trait Implementations§

§

impl<const P: usize, const NUM_SEGS_P2: usize> Freeze for Executor<P, NUM_SEGS_P2>

§

impl<const P: usize = 10, const NUM_SEGS_P2: usize = 8> !RefUnwindSafe for Executor<P, NUM_SEGS_P2>

§

impl<const P: usize, const NUM_SEGS_P2: usize> Send for Executor<P, NUM_SEGS_P2>

§

impl<const P: usize, const NUM_SEGS_P2: usize> Sync for Executor<P, NUM_SEGS_P2>

§

impl<const P: usize, const NUM_SEGS_P2: usize> Unpin for Executor<P, NUM_SEGS_P2>

§

impl<const P: usize = 10, const NUM_SEGS_P2: usize = 8> !UnwindSafe for Executor<P, NUM_SEGS_P2>

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> 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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V