Skip to main content

pi_async_runtime_core/
dispatch.rs

1//! 标准派发(dispatch)接口;来源接收与执行器接纳分别成立。
2use crate::{
3    task::*,
4    time::{RuntimeTime, TimedTaskNotifier},
5};
6use pi_async_task::join::LocalOutput;
7use pi_result::RawResult;
8use std::{future::Future, time::Duration};
9
10pub trait TaskDispatch {
11    type Notifier: TaskNotifier;
12    /// 创建原服务的可跨线程任务(Task)并保留配置;不提交、不接纳、不轮询(poll)。失败返还原Future和配置。
13    fn create_with_config<F, M>(
14        &self,
15        future: F,
16        config: TaskConfig<M>,
17    ) -> RawResult<(UnsubmittedTask, TaskHandle<Self::Notifier, F::Output, M>), CreateError<F, M>>
18    where
19        F: Future + Send + 'static,
20        F::Output: Send + 'static,
21        M: Send + Sync + 'static;
22    /// 向创建它的原服务提交唯一原值;明确拒绝返还原持有及原因,不取消、不改线程归属。
23    fn submit(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError>;
24    /// 使用默认配置创建;返回唯一待提交值与结果句柄(handle),不隐式派发。
25    fn create<F>(
26        &self,
27        future: F,
28    ) -> RawResult<(UnsubmittedTask, TaskHandle<Self::Notifier, F::Output>), CreateError<F, ()>>
29    where
30        F: Future + Send + 'static,
31        F::Output: Send + 'static,
32    {
33        self.create_with_config(future, TaskConfig::default())
34    }
35    /// 组合一次创建与提交;错误按创建/提交阶段保留对应原值,成功不表示已经执行。
36    fn spawn_with_config<F, M>(
37        &self,
38        future: F,
39        config: TaskConfig<M>,
40    ) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
41    where
42        F: Future + Send + 'static,
43        F::Output: Send + 'static,
44        M: Send + Sync + 'static,
45    {
46        let (task, handle) = self
47            .create_with_config(future, config)
48            .map_err(SpawnError::Create)?;
49        match self.submit(task) {
50            Ok(()) => Ok(handle),
51            Err(error) => Err(SpawnError::Submit { error, handle }),
52        }
53    }
54    /// 使用默认配置组合创建与提交;输入及输出只要求可跨线程转交(Send),不要求Sync或Unpin。
55    fn spawn<F>(
56        &self,
57        future: F,
58    ) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
59    where
60        F: Future + Send + 'static,
61        F::Output: Send + 'static,
62    {
63        self.spawn_with_config(future, TaskConfig::default())
64    }
65}
66
67/// 静态本地入口;真实当前权限由实现核对,不要求Future/Output为Send或Sync。
68pub trait LocalTaskDispatch: TaskDispatch {
69    /// 通过当前有效本地权限创建;原Future与输出保持原线程清理,不要求Send/Sync/Unpin。失败返还原输入。
70    fn create_local_with_config<F, M>(
71        future: F,
72        config: TaskConfig<M>,
73    ) -> RawResult<
74        (
75            UnsubmittedTask,
76            TaskHandle<Self::Notifier, F::Output, M, LocalOutput>,
77        ),
78        CreateError<F, M>,
79    >
80    where
81        F: Future + 'static,
82        F::Output: 'static,
83        M: Send + Sync + 'static;
84    /// 仅在原所属线程(owner thread)的有效本地范围首次提交;错误返还同一待提交值。
85    fn submit_local(task: UnsubmittedTask) -> RawResult<(), SubmitError>;
86    /// 使用默认配置进行本地创建;无合法当前环境时明确失败,不猜测目标。
87    fn create_local<F>(
88        future: F,
89    ) -> RawResult<
90        (
91            UnsubmittedTask,
92            TaskHandle<Self::Notifier, F::Output, (), LocalOutput>,
93        ),
94        CreateError<F, ()>,
95    >
96    where
97        F: Future + 'static,
98        F::Output: 'static,
99    {
100        Self::create_local_with_config(future, TaskConfig::default())
101    }
102    /// 组合本地创建与本地提交;不把结果变为可跨线程,不将拒绝当成取消。
103    fn spawn_local_with_config<F, M>(
104        future: F,
105        config: TaskConfig<M>,
106    ) -> RawResult<
107        TaskHandle<Self::Notifier, F::Output, M, LocalOutput>,
108        SpawnError<F, Self::Notifier, M, LocalOutput>,
109    >
110    where
111        F: Future + 'static,
112        F::Output: 'static,
113        M: Send + Sync + 'static,
114    {
115        let (task, handle) =
116            Self::create_local_with_config(future, config).map_err(SpawnError::Create)?;
117        match Self::submit_local(task) {
118            Ok(()) => Ok(handle),
119            Err(error) => Err(SpawnError::Submit { error, handle }),
120        }
121    }
122    /// 默认配置的本地快速派发(local dispatch);不创建新线程,不内联轮询(poll)。
123    fn spawn_local<F>(
124        future: F,
125    ) -> RawResult<
126        TaskHandle<Self::Notifier, F::Output, (), LocalOutput>,
127        SpawnError<F, Self::Notifier, (), LocalOutput>,
128    >
129    where
130        F: Future + 'static,
131        F::Output: 'static,
132    {
133        Self::spawn_local_with_config(future, TaskConfig::default())
134    }
135}
136
137/// 空闲来源不判断是否空闲;提交成功不承诺一定被接纳或执行。
138pub trait IdleDispatch: TaskDispatch {
139    /// 向同一服务的空闲来源提交普通Send任务(Task);不检查空闲、不保证未来接纳。
140    fn submit_idle(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError>;
141    /// 创建并提交空闲时任务(idle task);成功只证明来源接受,不保证执行。
142    fn idle_with_config<F, M>(
143        &self,
144        future: F,
145        config: TaskConfig<M>,
146    ) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
147    where
148        F: Future + Send + 'static,
149        F::Output: Send + 'static,
150        M: Send + Sync + 'static,
151    {
152        let (task, handle) = self
153            .create_with_config(future, config)
154            .map_err(SpawnError::Create)?;
155        match self.submit_idle(task) {
156            Ok(()) => Ok(handle),
157            Err(error) => Err(SpawnError::Submit { error, handle }),
158        }
159    }
160    /// 默认配置的空闲提交;任务(Task)未接纳前仍可按原控制能力取消。
161    fn idle<F>(
162        &self,
163        future: F,
164    ) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
165    where
166        F: Future + Send + 'static,
167        F::Output: Send + 'static,
168    {
169        self.idle_with_config(future, TaskConfig::default())
170    }
171    /// 从当前环境定位原服务后提交共享空闲来源;Future与输出要求Send,不要求Sync,不预先绑定执行线程。
172    fn idle_local_with_config<F, M>(
173        future: F,
174        config: TaskConfig<M>,
175    ) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
176    where
177        F: Future + Send + 'static,
178        F::Output: Send + 'static,
179        M: Send + Sync + 'static;
180    /// 默认配置的当前空闲提交;输入保持Send约束,不等同spawn_local的本地非Send能力。
181    fn idle_local<F>(
182        future: F,
183    ) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
184    where
185        F: Future + Send + 'static,
186        F::Output: Send + 'static,
187    {
188        Self::idle_local_with_config(future, TaskConfig::default())
189    }
190}
191
192/// 同步提交原业务任务(Task)到定时来源;不直接poll,不经普通来源绕行。
193pub trait TimedTaskDispatch: TaskDispatch + RuntimeTime {
194    /// 同步固定本次截止时间并提交定时来源;准备失败保原Future,明确拒绝保原任务(Task)和结果句柄(handle)。
195    fn spawn_timing_with_config<F, M>(
196        &self,
197        future: F,
198        delay: Duration,
199        config: TaskConfig<M>,
200    ) -> RawResult<
201        TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, M>,
202        SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, M>,
203    >
204    where
205        F: Future + Send + 'static,
206        F::Output: Send + 'static,
207        M: Send + Sync + 'static;
208    /// 默认配置的同步定时派发(timed dispatch);零时长仍入定时来源,不直接执行。
209    fn spawn_timing<F>(
210        &self,
211        future: F,
212        delay: Duration,
213    ) -> RawResult<
214        TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output>,
215        SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>>,
216    >
217    where
218        F: Future + Send + 'static,
219        F::Output: Send + 'static,
220    {
221        self.spawn_timing_with_config(future, delay, TaskConfig::default())
222    }
223    /// 当前原线程内同步定时派发(timed dispatch);原Future/输出不要求Send/Sync,准备和拒绝均保留原值。
224    fn spawn_timing_local_with_config<F, M>(
225        future: F,
226        delay: Duration,
227        config: TaskConfig<M>,
228    ) -> RawResult<
229        TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, M, LocalOutput>,
230        SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, M, LocalOutput>,
231    >
232    where
233        F: Future + 'static,
234        F::Output: 'static,
235        M: Send + Sync + 'static;
236    /// 默认配置的本地定时派发(timed dispatch);不跳过原所属域或定时来源。
237    fn spawn_timing_local<F>(
238        future: F,
239        delay: Duration,
240    ) -> RawResult<
241        TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, (), LocalOutput>,
242        SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, (), LocalOutput>,
243    >
244    where
245        F: Future + 'static,
246        F::Output: 'static,
247    {
248        Self::spawn_timing_local_with_config(future, delay, TaskConfig::default())
249    }
250}