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>
impl<const P: usize, const NUM_SEGS_P2: usize> Executor<P, NUM_SEGS_P2>
pub fn new_single_threaded() -> Self
Source§impl<const P: usize, const NUM_SEGS_P2: usize> Executor<P, NUM_SEGS_P2>
impl<const P: usize, const NUM_SEGS_P2: usize> Executor<P, NUM_SEGS_P2>
Sourcepub fn new(
config: TaskArenaConfig,
options: TaskArenaOptions,
worker_count: usize,
max_worker_count: usize,
) -> Result<Self>
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:
- A task arena with the given configuration and options
- A worker service with the specified number of worker threads
- 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 instanceErr(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
)?;Sourcepub fn new_with_tick_service(
config: TaskArenaConfig,
options: TaskArenaOptions,
worker_count: usize,
max_worker_count: usize,
tick_service: Arc<TickService>,
) -> Result<Self>
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:
- A task arena with the given configuration and options
- A worker service with the specified number of worker threads
- 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 instanceErr(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
)?;Sourcepub 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,
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 implementsIntoFuture.
§Returns
Ok(JoinHandle<T>): A join handle that implementsFuture<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;Sourcepub fn stats(&self) -> TaskArenaStats
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
Sourcepub fn service(&self) -> &Arc<WorkerService<P, NUM_SEGS_P2>>
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> Drop for Executor<P, NUM_SEGS_P2>
impl<const P: usize, const NUM_SEGS_P2: usize> Drop for Executor<P, NUM_SEGS_P2>
Source§fn drop(&mut self)
fn drop(&mut self)
Cleans up the executor when it goes out of scope.
This implementation:
- Initiates shutdown of the executor (closes arena, signals workers)
- If this executor owns a tick service, shuts it down as well
- Unparks any worker threads that might be waiting
- 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.