Skip to main content

moirai_core/executor/
builder.rs

1//! Executor builder implementation.
2
3use super::config::{CleanupConfig, ExecutorConfig, MemoryConfig, PreemptionConfig};
4use crate::platform::String;
5
6/// Builder for creating executors with custom configuration.
7pub struct ExecutorBuilder {
8    pub(crate) config: ExecutorConfig,
9}
10
11impl ExecutorBuilder {
12    /// Creates a new executor builder with default settings.
13    ///
14    /// # Returns
15    /// A new builder instance ready for configuration
16    #[must_use]
17    pub fn new() -> Self {
18        Self {
19            config: ExecutorConfig::default(),
20        }
21    }
22
23    /// Sets the number of worker threads for CPU-bound tasks.
24    ///
25    /// # Arguments
26    /// * `count` - Number of worker threads to create
27    ///
28    /// # Returns
29    /// The builder instance for method chaining
30    #[must_use]
31    pub fn worker_threads(mut self, count: usize) -> Self {
32        self.config.worker_threads = count;
33        self
34    }
35
36    /// Sets the number of threads for async task execution.
37    ///
38    /// # Arguments
39    /// * `count` - Number of async threads to create
40    ///
41    /// # Returns
42    /// The builder instance for method chaining
43    #[must_use]
44    pub fn async_threads(mut self, count: usize) -> Self {
45        self.config.async_threads = count;
46        self
47    }
48
49    /// Sets the maximum size of the global task queue.
50    ///
51    /// # Arguments
52    /// * `size` - Maximum number of tasks in the global queue
53    ///
54    /// # Returns
55    /// The builder instance for method chaining
56    #[must_use]
57    pub fn max_global_queue_size(mut self, size: usize) -> Self {
58        self.config.max_global_queue_size = size;
59        self
60    }
61
62    /// Sets the maximum size of per-worker local queues.
63    ///
64    /// # Arguments
65    /// * `size` - Maximum number of tasks in each local queue
66    ///
67    /// # Returns
68    /// The builder instance for method chaining
69    #[must_use]
70    pub fn max_local_queue_size(mut self, size: usize) -> Self {
71        self.config.max_local_queue_size = size;
72        self
73    }
74
75    /// Sets the thread name prefix for executor threads.
76    ///
77    /// # Arguments
78    /// * `prefix` - String prefix for thread names
79    ///
80    /// # Returns
81    /// The builder instance for method chaining
82    #[must_use]
83    pub fn thread_name_prefix(mut self, prefix: impl Into<String>) -> Self {
84        self.config.thread_name_prefix = prefix.into();
85        self
86    }
87
88    /// Enable or disable NUMA awareness.
89    #[cfg(feature = "numa")]
90    #[must_use]
91    pub fn numa_aware(mut self, enabled: bool) -> Self {
92        self.config.numa_aware = enabled;
93        self
94    }
95
96    /// Enable or disable metrics collection.
97    #[cfg(feature = "metrics")]
98    #[must_use]
99    pub fn enable_metrics(mut self, enabled: bool) -> Self {
100        self.config.enable_metrics = enabled;
101        self
102    }
103
104    /// Configures preemption behavior for the executor.
105    ///
106    /// # Arguments
107    /// * `config` - Preemption configuration settings
108    ///
109    /// # Returns
110    /// The builder instance for method chaining
111    #[must_use]
112    pub fn preemption_config(mut self, config: PreemptionConfig) -> Self {
113        self.config.preemption = config;
114        self
115    }
116
117    /// Configures memory management settings.
118    ///
119    /// # Arguments
120    /// * `config` - Memory configuration settings
121    ///
122    /// # Returns
123    /// The builder instance for method chaining
124    #[must_use]
125    pub fn memory_config(mut self, config: MemoryConfig) -> Self {
126        self.config.memory = config;
127        self
128    }
129
130    /// Configures cleanup and maintenance settings.
131    ///
132    /// # Arguments
133    /// * `config` - Cleanup configuration settings
134    ///
135    /// # Returns
136    /// The builder instance for method chaining
137    #[must_use]
138    pub fn cleanup_config(mut self, config: CleanupConfig) -> Self {
139        self.config.cleanup = config;
140        self
141    }
142}
143
144impl Default for ExecutorBuilder {
145    fn default() -> Self {
146        Self::new()
147    }
148}