1use crate::{
3 local::ExecutorLocal,
4 parking::ThreadNotifier,
5 registry::TaskRegistryHandle,
6 routing::{ExecutorSchedule, LocalReadySubmit},
7 state::ExecutorState,
8 timer::ExecutorTimer,
9};
10use pi_async_runtime_core::{
11 control::RuntimeControl,
12 definitions::*,
13 query::RuntimeQuery,
14 task::*,
15 time::{TimedTaskNotifier, TimerAccess},
16};
17use pi_async_task::{definitions::TaskOwnerId, LocalOutput};
18use pi_async_task_queue::{idle::IdleTaskQueue, timer::TimerQueueHandle, ExternalTaskQueue};
19use std::{future::Future, marker::PhantomData, sync::Arc, time::Duration};
20
21pub struct ExecutorHandle<
26 E: ExternalTaskQueue + Clone,
27 I: IdleTaskQueue + Clone,
28 T: ExecutorTimer + 'static,
29 L: LocalReadySubmit,
30> {
31 pub(crate) factory: TaskFactory<ExecutorSchedule<L>, ThreadNotifier>,
32 pub(crate) external: E,
33 pub(crate) idle: I,
34 pub(crate) timer: T::Access,
35 pub(crate) timer_query: T::Handle,
36 pub(crate) state: Arc<ExecutorState>,
37 pub(crate) registry: TaskRegistryHandle,
38 pub(crate) notifier: ThreadNotifier,
39 pub(crate) owner: TaskOwnerId,
40 pub(crate) types: PhantomData<fn() -> T>,
41}
42struct SourceNotifyGuard<'a> {
43 notifier: &'a ThreadNotifier,
44 armed: bool,
45}
46impl Drop for SourceNotifyGuard<'_> {
47 fn drop(&mut self) {
48 source_notify_drop_inner(self)
49 }
50}
51impl<
52 E: ExternalTaskQueue + Clone,
53 I: IdleTaskQueue + Clone,
54 T: ExecutorTimer + 'static,
55 L: LocalReadySubmit,
56 > Clone for ExecutorHandle<E, I, T, L>
57{
58 fn clone(&self) -> Self {
59 handle_clone_inner(self)
60 }
61}
62impl<
63 E: ExternalTaskQueue + Clone,
64 I: IdleTaskQueue + Clone,
65 T: ExecutorTimer + 'static,
66 L: LocalReadySubmit,
67 > ExecutorHandle<E, I, T, L>
68{
69 pub fn timer_access(&self) -> T::Access {
71 self.timer.clone()
72 }
73 pub fn create<F, M>(
76 &self,
77 future: F,
78 config: TaskConfig<M>,
79 ) -> pi_result::RawResult<
80 (UnsubmittedTask, TaskHandle<ThreadNotifier, F::Output, M>),
81 CreateError<F, M>,
82 >
83 where
84 F: Future + Send + 'static,
85 F::Output: Send + 'static,
86 M: Send + Sync + 'static,
87 {
88 handle_create_inner(self, future, config)
89 }
90 pub fn submit(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
94 handle_submit_inner(self, task, false)
95 }
96 pub fn submit_idle(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
99 handle_submit_inner(self, task, true)
100 }
101 pub fn create_local<F, M>(
103 &self,
104 local: &ExecutorLocal<T>,
105 future: F,
106 config: TaskConfig<M>,
107 ) -> pi_result::RawResult<
108 (
109 UnsubmittedTask,
110 TaskHandle<ThreadNotifier, F::Output, M, LocalOutput>,
111 ),
112 CreateError<F, M>,
113 >
114 where
115 F: Future + 'static,
116 F::Output: 'static,
117 M: Send + Sync + 'static,
118 {
119 handle_create_local_inner(self, local, future, config)
120 }
121 pub fn submit_local(
125 &self,
126 local: &ExecutorLocal<T>,
127 task: UnsubmittedTask,
128 ) -> pi_result::RawResult<(), SubmitError> {
129 handle_submit_local_inner(self, local, task, false)
130 }
131 pub fn submit_local_front(
133 &self,
134 local: &ExecutorLocal<T>,
135 task: UnsubmittedTask,
136 ) -> pi_result::RawResult<(), SubmitError> {
137 handle_submit_local_inner(self, local, task, true)
138 }
139 pub fn spawn_timing<F, M>(
144 &self,
145 future: F,
146 delay: Duration,
147 config: TaskConfig<M>,
148 ) -> pi_result::RawResult<
149 TaskHandle<TimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M>,
150 SpawnError<F, TimedTaskNotifier<ThreadNotifier, T::Access>, M>,
151 >
152 where
153 F: Future + Send + 'static,
154 F::Output: Send + 'static,
155 M: Send + Sync + 'static,
156 {
157 handle_timed_inner(self, future, delay, config)
158 }
159 pub fn spawn_timing_local<F, M>(
164 &self,
165 local: &ExecutorLocal<T>,
166 future: F,
167 delay: Duration,
168 config: TaskConfig<M>,
169 ) -> pi_result::RawResult<
170 TaskHandle<TimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M, LocalOutput>,
171 SpawnError<F, TimedTaskNotifier<ThreadNotifier, T::Access>, M, LocalOutput>,
172 >
173 where
174 F: Future + 'static,
175 F::Output: 'static,
176 M: Send + Sync + 'static,
177 {
178 handle_timed_local_inner(self, local, future, delay, config)
179 }
180}
181impl<
182 E: ExternalTaskQueue + Clone,
183 I: IdleTaskQueue + Clone,
184 T: ExecutorTimer + 'static,
185 L: LocalReadySubmit,
186 > RuntimeQuery for ExecutorHandle<E, I, T, L>
187{
188 fn id(&self) -> RuntimeId {
189 self.factory.runtime_id()
190 }
191 fn status(&self) -> RuntimeStatus {
192 self.state.status()
193 }
194 fn is_driving(&self) -> bool {
195 self.state.is_driving()
196 }
197 fn last_idle_state(&self) -> IdleState {
198 self.state.last_idle_state()
199 }
200 fn close_state(&self) -> CloseState {
201 self.state.close_state()
202 }
203 fn external_task_count(&self) -> usize {
204 self.external.len()
205 }
206 fn internal_task_count(&self) -> usize {
207 handle_internal_count_inner(self)
208 }
209 fn timer_registration_count(&self) -> usize {
210 self.timer_query.len()
211 }
212 fn idle_task_count(&self) -> usize {
213 self.idle.len()
214 }
215 fn in_flight_task_count(&self) -> usize {
216 handle_in_flight_inner(self)
217 }
218 fn waiting_for_wake_count(&self) -> usize {
219 self.registry.waiting_for_wake_count()
220 }
221 fn pending_cancel_task_count(&self) -> usize {
222 self.registry.pending_cancel_count()
223 }
224}
225impl<
226 E: ExternalTaskQueue + Clone,
227 I: IdleTaskQueue + Clone,
228 T: ExecutorTimer + 'static,
229 L: LocalReadySubmit,
230 > RuntimeControl for ExecutorHandle<E, I, T, L>
231{
232 fn request_close(&self) -> CloseRequestStatus {
233 self.state.request_close(&self.notifier)
234 }
235}
236include!("handle_inner.rs");