moirai_core/executor/mod.rs
1//! Executor trait and implementations.
2//!
3//! This module provides the core executor abstraction for the Moirai runtime.
4//! It defines traits for task spawning, management, and lifecycle control.
5
6pub mod builder;
7pub mod config;
8pub mod control;
9pub mod manager;
10pub mod parallelism;
11pub mod placement;
12pub mod spawner;
13
14pub use builder::ExecutorBuilder;
15pub use config::{CleanupConfig, ExecutorConfig, MemoryConfig, PreemptionConfig};
16pub use control::ExecutorControl;
17pub use manager::{TaskManager, TaskStats, TaskStatus};
18pub use parallelism::logical_parallelism;
19pub use placement::WorkerPlacement;
20pub use spawner::TaskSpawner;
21
22/// Combined executor trait with all capabilities.
23///
24/// This trait combines all executor capabilities into a single interface
25/// for convenience while maintaining the segregated design internally.
26///
27/// # Design Philosophy
28/// - Composition over inheritance
29/// - Single interface for complete functionality
30/// - Maintains internal separation of concerns
31/// - Enables easy mocking and testing
32pub trait Executor: TaskSpawner + TaskManager + ExecutorControl {
33 /// Get comprehensive executor statistics.
34 ///
35 /// # Behavior Guarantees
36 /// - Returns current snapshot of all executor metrics
37 /// - Statistics are eventually consistent
38 /// - Available only when metrics feature is enabled
39 /// - Includes worker, queue, memory, and task statistics
40 ///
41 /// # Performance Characteristics
42 /// - Collection overhead: < 1μs for full statistics
43 /// - Memory: ~1KB for complete statistics snapshot
44 /// - Thread safety: Atomic operations for consistency
45 #[cfg(feature = "metrics")]
46 fn stats(&self) -> ExecutorStats;
47}
48
49/// Executor statistics (basic implementation when metrics feature is disabled)
50#[cfg(not(feature = "metrics"))]
51#[derive(Debug, Clone, Default)]
52pub struct ExecutorStats;
53
54/// Executor statistics with full metrics
55#[cfg(feature = "metrics")]
56#[derive(Debug, Clone, Default)]
57pub struct ExecutorStats {
58 /// Number of tasks executed
59 pub tasks_executed: u64,
60 /// Number of tasks in queue
61 pub tasks_queued: usize,
62 /// Average task execution time
63 pub avg_execution_time_ns: u64,
64}