Skip to main content

pi_async_runtime/
driving.rs

1//! 拥有者服务推进与共享句柄(shared handle)的根专用推进。
2use crate::{
3    access::{self, BindingExitGuard},
4    instance::*,
5    lifecycle::RuntimeBinding,
6    mode::RuntimeMode,
7};
8use pi_async_executor::{DriveLease, ExecutorClock, ExecutorHooks, IdlePolicy, TurnResult};
9use pi_async_runtime_core::{
10    block_on::{BlockOnDriver, RootWakeState},
11    RuntimeBlockOn, RuntimeQuery, RuntimeStatus,
12};
13use pi_clock_manual::global::GlobalManualInstant;
14use std::future::Future;
15
16struct RuntimeDriver<'a, M, C, P, H>
17where
18    M: RuntimeMode,
19    C: ExecutorClock<Instant = GlobalManualInstant>,
20    P: IdlePolicy,
21    H: ExecutorHooks,
22{
23    data: &'a mut RuntimeData<M, C, P, H>,
24    lease: DriveLease,
25    last: Option<TurnResult>,
26    binding: &'a RuntimeBinding,
27    handle: &'a RuntimeHandle<C>,
28}
29impl<M, C, P, H> ConfiguredRuntime<M, C, P, H>
30where
31    M: RuntimeMode,
32    C: ExecutorClock<Instant = GlobalManualInstant>,
33    P: IdlePolicy,
34    H: ExecutorHooks,
35{
36    /// 原线程一次有限事件循环(event loop),返回后保留本地权限;不自行停放线程。
37    /// 其它线程可继续通过Handle提交;同步控制hook和原F仍可能阻塞本次调用。
38    pub fn run_once(&mut self) -> pi_result::Result<TurnResult> {
39        runtime_once_inner(self)
40    }
41    /// 原线程持续推动同服务,直到关闭或首个推进错误;没有隐式工作线程。
42    pub fn run(&mut self) -> pi_result::Result<()> {
43        runtime_run_inner(self)
44    }
45}
46impl<C: ExecutorClock<Instant = GlobalManualInstant>> RuntimeBlockOn for RuntimeHandle<C> {
47    /// 只阻塞调用线程推进这份根Future;任务服务必须由拥有者另行推动。
48    /// 无Unpin/Send/Sync/'static约束,不等根派发的其它任务全部结束。
49    fn block_on<F: Future>(&self, future: F) -> F::Output {
50        handle_block_on_inner(self, future)
51    }
52}
53impl<M, C, P, H> RuntimeBlockOn for ConfiguredRuntime<M, C, P, H>
54where
55    M: RuntimeMode,
56    C: ExecutorClock<Instant = GlobalManualInstant>,
57    P: IdlePolicy,
58    H: ExecutorHooks,
59{
60    /// 原线程独占服务并推进根,根完成即返回;没有&mut self或F额外约束。
61    /// 非法嵌套/不同当前目标在首次poll前panic;关闭后只推进独立根,不重开服务。
62    /// 服务推进失败以原pi_result::Error为panic载荷,保留报告链;根自身panic原样传播。
63    fn block_on<F: Future>(&self, future: F) -> F::Output {
64        runtime_block_on_inner(self, future)
65    }
66}
67impl<M, C, P, H> BlockOnDriver for RuntimeDriver<'_, M, C, P, H>
68where
69    M: RuntimeMode,
70    C: ExecutorClock<Instant = GlobalManualInstant>,
71    P: IdlePolicy,
72    H: ExecutorHooks,
73{
74    fn turn(&mut self) -> bool {
75        runtime_driver_turn_inner(self)
76    }
77    fn park(&mut self, root: &RootWakeState) {
78        runtime_driver_park_inner(self, root)
79    }
80}
81include!("driving_inner.rs");