Skip to main content

pi_async_runtime/
dispatch.rs

1//! 一类派发入口(dispatch API);只组合原工厂与受限来源,不再定义任务生命周期。
2use crate::{access, instance::*, mode::RuntimeMode};
3use pi_async_executor::{ExecutorClock, ExecutorHooks, IdlePolicy, ThreadNotifier};
4use pi_async_runtime_core::task::{CreateError, SpawnError, SubmitError};
5use pi_async_runtime_core::{
6    IdleDispatch, LocalTaskDispatch, TaskConfig, TaskDispatch, TimedTaskDispatch, UnsubmittedTask,
7};
8use pi_async_task::LocalOutput;
9use pi_clock_manual::global::GlobalManualInstant;
10use std::{future::Future, time::Duration};
11
12pub(crate) fn create_local<C, F, V>(
13    future: F,
14    config: TaskConfig<V>,
15) -> pi_result::RawResult<(UnsubmittedTask, LocalTaskHandle<F::Output, V>), CreateError<F, V>>
16where
17    C: ExecutorClock<Instant = GlobalManualInstant>,
18    F: Future + 'static,
19    F::Output: 'static,
20    V: Send + Sync + 'static,
21{
22    local_create_inner::<C, F, V>(future, config)
23}
24pub(crate) fn submit_local<C: ExecutorClock<Instant = GlobalManualInstant>>(
25    task: UnsubmittedTask,
26    front: bool,
27) -> pi_result::RawResult<(), SubmitError> {
28    local_submit_inner::<C>(task, front)
29}
30pub(crate) fn idle_local<C, F, V>(
31    future: F,
32    config: TaskConfig<V>,
33) -> pi_result::RawResult<TaskHandle<F::Output, V>, SpawnError<F, ThreadNotifier, V>>
34where
35    C: ExecutorClock<Instant = GlobalManualInstant>,
36    F: Future + Send + 'static,
37    F::Output: Send + 'static,
38    V: Send + Sync + 'static,
39{
40    local_idle_inner::<C, F, V>(future, config)
41}
42pub(crate) fn timing_local<C, F, V>(
43    future: F,
44    delay: Duration,
45    config: TaskConfig<V>,
46) -> pi_result::RawResult<
47    pi_async_runtime_core::task::TaskHandle<TimedTaskNotifier<C>, F::Output, V, LocalOutput>,
48    SpawnError<F, TimedTaskNotifier<C>, V, LocalOutput>,
49>
50where
51    C: ExecutorClock<Instant = GlobalManualInstant>,
52    F: Future + 'static,
53    F::Output: 'static,
54    V: Send + Sync + 'static,
55{
56    local_timing_inner::<C, F, V>(future, delay, config)
57}
58include!("dispatch_inner.rs");
59impl<C> TaskDispatch for RuntimeHandle<C>
60where
61    C: ExecutorClock<Instant = GlobalManualInstant>,
62{
63    type Notifier = ThreadNotifier;
64    fn create_with_config<F, V>(
65        &self,
66        future: F,
67        config: TaskConfig<V>,
68    ) -> pi_result::RawResult<(UnsubmittedTask, TaskHandle<F::Output, V>), CreateError<F, V>>
69    where
70        F: Future + Send + 'static,
71        F::Output: Send + 'static,
72        V: Send + Sync + 'static,
73    {
74        self.shared().inner.create(future, config)
75    }
76    fn submit(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
77        self.shared().inner.submit(task)
78    }
79}
80impl<C> LocalTaskDispatch for RuntimeHandle<C>
81where
82    C: ExecutorClock<Instant = GlobalManualInstant>,
83{
84    fn create_local_with_config<F, V>(
85        future: F,
86        config: TaskConfig<V>,
87    ) -> pi_result::RawResult<(UnsubmittedTask, LocalTaskHandle<F::Output, V>), CreateError<F, V>>
88    where
89        F: Future + 'static,
90        F::Output: 'static,
91        V: Send + Sync + 'static,
92    {
93        create_local::<C, F, V>(future, config)
94    }
95    fn submit_local(task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
96        submit_local::<C>(task, false)
97    }
98}
99impl<C> IdleDispatch for RuntimeHandle<C>
100where
101    C: ExecutorClock<Instant = GlobalManualInstant>,
102{
103    fn submit_idle(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
104        self.shared().inner.submit_idle(task)
105    }
106    fn idle_local_with_config<F, V>(
107        future: F,
108        config: TaskConfig<V>,
109    ) -> pi_result::RawResult<TaskHandle<F::Output, V>, SpawnError<F, ThreadNotifier, V>>
110    where
111        F: Future + Send + 'static,
112        F::Output: Send + 'static,
113        V: Send + Sync + 'static,
114    {
115        idle_local::<C, F, V>(future, config)
116    }
117}
118impl<C> TimedTaskDispatch for RuntimeHandle<C>
119where
120    C: ExecutorClock<Instant = GlobalManualInstant>,
121{
122    fn spawn_timing_with_config<F, V>(
123        &self,
124        future: F,
125        delay: Duration,
126        config: TaskConfig<V>,
127    ) -> pi_result::RawResult<
128        TimedTaskHandle<F::Output, V, C>,
129        SpawnError<F, TimedTaskNotifier<C>, V>,
130    >
131    where
132        F: Future + Send + 'static,
133        F::Output: Send + 'static,
134        V: Send + Sync + 'static,
135    {
136        self.shared().inner.spawn_timing(future, delay, config)
137    }
138    fn spawn_timing_local_with_config<F, V>(
139        future: F,
140        delay: Duration,
141        config: TaskConfig<V>,
142    ) -> pi_result::RawResult<
143        pi_async_runtime_core::task::TaskHandle<TimedTaskNotifier<C>, F::Output, V, LocalOutput>,
144        SpawnError<F, TimedTaskNotifier<C>, V, LocalOutput>,
145    >
146    where
147        F: Future + 'static,
148        F::Output: 'static,
149        V: Send + Sync + 'static,
150    {
151        timing_local::<C, F, V>(future, delay, config)
152    }
153}
154impl<C> RuntimeHandle<C>
155where
156    C: ExecutorClock<Instant = GlobalManualInstant>,
157{
158    /// 由当前原线程把首次提交放内部头;无本地权限时返原值及原因。
159    pub fn submit_local_front(task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
160        submit_local::<C>(task, true)
161    }
162    /// 本地队首便利派发;与spawn_local相同原值及权限,只改变下一次排队优先级。
163    pub fn spawn_local_front_with_config<F, V>(
164        future: F,
165        config: TaskConfig<V>,
166    ) -> pi_result::RawResult<
167        LocalTaskHandle<F::Output, V>,
168        SpawnError<F, ThreadNotifier, V, LocalOutput>,
169    >
170    where
171        F: Future + 'static,
172        F::Output: 'static,
173        V: Send + Sync + 'static,
174    {
175        let (task, handle) = create_local::<C, F, V>(future, config).map_err(SpawnError::Create)?;
176        match submit_local::<C>(task, true) {
177            Ok(()) => Ok(handle),
178            Err(error) => Err(SpawnError::Submit { error, handle }),
179        }
180    }
181    /// 默认附加数据(metadata)的本地队首入口,不增加F的Send/Sync/Unpin。
182    pub fn spawn_local_front<F>(
183        future: F,
184    ) -> pi_result::RawResult<
185        LocalTaskHandle<F::Output>,
186        SpawnError<F, ThreadNotifier, (), LocalOutput>,
187    >
188    where
189        F: Future + 'static,
190        F::Output: 'static,
191    {
192        Self::spawn_local_front_with_config(future, TaskConfig::default())
193    }
194}
195impl<M, C, P, H> TaskDispatch for ConfiguredRuntime<M, C, P, H>
196where
197    M: RuntimeMode,
198    C: ExecutorClock<Instant = GlobalManualInstant>,
199    P: IdlePolicy,
200    H: ExecutorHooks,
201{
202    type Notifier = ThreadNotifier;
203    fn create_with_config<F, V>(
204        &self,
205        future: F,
206        config: TaskConfig<V>,
207    ) -> pi_result::RawResult<(UnsubmittedTask, TaskHandle<F::Output, V>), CreateError<F, V>>
208    where
209        F: Future + Send + 'static,
210        F::Output: Send + 'static,
211        V: Send + Sync + 'static,
212    {
213        self.shared().inner.create(future, config)
214    }
215    fn submit(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
216        self.shared().inner.submit(task)
217    }
218}
219impl<M, C, P, H> LocalTaskDispatch for ConfiguredRuntime<M, C, P, H>
220where
221    M: RuntimeMode,
222    C: ExecutorClock<Instant = GlobalManualInstant>,
223    P: IdlePolicy,
224    H: ExecutorHooks,
225{
226    fn create_local_with_config<F, V>(
227        future: F,
228        config: TaskConfig<V>,
229    ) -> pi_result::RawResult<(UnsubmittedTask, LocalTaskHandle<F::Output, V>), CreateError<F, V>>
230    where
231        F: Future + 'static,
232        F::Output: 'static,
233        V: Send + Sync + 'static,
234    {
235        create_local::<C, F, V>(future, config)
236    }
237    fn submit_local(task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
238        submit_local::<C>(task, false)
239    }
240}
241impl<M, C, P, H> IdleDispatch for ConfiguredRuntime<M, C, P, H>
242where
243    M: RuntimeMode,
244    C: ExecutorClock<Instant = GlobalManualInstant>,
245    P: IdlePolicy,
246    H: ExecutorHooks,
247{
248    fn submit_idle(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
249        self.shared().inner.submit_idle(task)
250    }
251    fn idle_local_with_config<F, V>(
252        future: F,
253        config: TaskConfig<V>,
254    ) -> pi_result::RawResult<TaskHandle<F::Output, V>, SpawnError<F, ThreadNotifier, V>>
255    where
256        F: Future + Send + 'static,
257        F::Output: Send + 'static,
258        V: Send + Sync + 'static,
259    {
260        idle_local::<C, F, V>(future, config)
261    }
262}
263impl<M, C, P, H> TimedTaskDispatch for ConfiguredRuntime<M, C, P, H>
264where
265    M: RuntimeMode,
266    C: ExecutorClock<Instant = GlobalManualInstant>,
267    P: IdlePolicy,
268    H: ExecutorHooks,
269{
270    fn spawn_timing_with_config<F, V>(
271        &self,
272        future: F,
273        delay: Duration,
274        config: TaskConfig<V>,
275    ) -> pi_result::RawResult<
276        TimedTaskHandle<F::Output, V, C>,
277        SpawnError<F, TimedTaskNotifier<C>, V>,
278    >
279    where
280        F: Future + Send + 'static,
281        F::Output: Send + 'static,
282        V: Send + Sync + 'static,
283    {
284        self.shared().inner.spawn_timing(future, delay, config)
285    }
286    fn spawn_timing_local_with_config<F, V>(
287        future: F,
288        delay: Duration,
289        config: TaskConfig<V>,
290    ) -> pi_result::RawResult<
291        pi_async_runtime_core::task::TaskHandle<TimedTaskNotifier<C>, F::Output, V, LocalOutput>,
292        SpawnError<F, TimedTaskNotifier<C>, V, LocalOutput>,
293    >
294    where
295        F: Future + 'static,
296        F::Output: 'static,
297        V: Send + Sync + 'static,
298    {
299        timing_local::<C, F, V>(future, delay, config)
300    }
301}
302impl<M, C, P, H> ConfiguredRuntime<M, C, P, H>
303where
304    M: RuntimeMode,
305    C: ExecutorClock<Instant = GlobalManualInstant>,
306    P: IdlePolicy,
307    H: ExecutorHooks,
308{
309    /// 由当前原线程把首次提交放内部头;无本地权限时返原值及原因。
310    pub fn submit_local_front(task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError> {
311        submit_local::<C>(task, true)
312    }
313    /// 本地队首便利派发;与spawn_local相同原值及权限,只改变下一次排队优先级。
314    pub fn spawn_local_front_with_config<F, V>(
315        future: F,
316        config: TaskConfig<V>,
317    ) -> pi_result::RawResult<
318        LocalTaskHandle<F::Output, V>,
319        SpawnError<F, ThreadNotifier, V, LocalOutput>,
320    >
321    where
322        F: Future + 'static,
323        F::Output: 'static,
324        V: Send + Sync + 'static,
325    {
326        let (task, handle) = create_local::<C, F, V>(future, config).map_err(SpawnError::Create)?;
327        match submit_local::<C>(task, true) {
328            Ok(()) => Ok(handle),
329            Err(error) => Err(SpawnError::Submit { error, handle }),
330        }
331    }
332    /// 默认附加数据(metadata)的本地队首入口,不增加F的Send/Sync/Unpin。
333    pub fn spawn_local_front<F>(
334        future: F,
335    ) -> pi_result::RawResult<
336        LocalTaskHandle<F::Output>,
337        SpawnError<F, ThreadNotifier, (), LocalOutput>,
338    >
339    where
340        F: Future + 'static,
341        F::Output: 'static,
342    {
343        Self::spawn_local_front_with_config(future, TaskConfig::default())
344    }
345}