1use crate::{
3 binding::RuntimeTag,
4 lifecycle::{RuntimeBinding, RuntimeExternalSource},
5 mode::{ConcurrentMode, RuntimeMode, SerialMode},
6 options::RuntimeOptions,
7};
8use pi_async_executor::{
9 timer::{DefaultTimerAccess, DefaultTimerDriver},
10 DefaultExecutor, DefaultIdlePolicy, ExecutorClock, ExecutorHandle, ExecutorHooks,
11 ExecutorLocal, IdlePolicy, ManualClock, NoHooks, ThreadNotifier,
12};
13use pi_async_task_queue::{
14 MpscIdleTaskQueue, MpscIdleTaskQueueHandle, MpscTaskQueue, MpscTaskQueueHandle,
15};
16use pi_clock_manual::global::GlobalManualInstant;
17use std::{cell::RefCell, rc::Rc, sync::Arc};
18
19pub(crate) type Timer<C> = DefaultTimerDriver<C, RuntimeTag<C>>;
20pub(crate) type Executor<C> =
21 DefaultExecutor<C, RuntimeExternalSource, MpscIdleTaskQueue, Timer<C>, RuntimeTag<C>>;
22pub(crate) type Shared<C> =
23 ExecutorHandle<MpscTaskQueueHandle, MpscIdleTaskQueueHandle, Timer<C>, RuntimeTag<C>>;
24pub type RuntimeTimerAccess<C = ManualClock> = DefaultTimerAccess<C, RuntimeTag<C>>;
26pub type TaskHandle<T, V = ()> = pi_async_runtime_core::task::TaskHandle<ThreadNotifier, T, V>;
27pub type LocalTaskHandle<T, V = ()> =
28 pi_async_runtime_core::task::TaskHandle<ThreadNotifier, T, V, pi_async_task::LocalOutput>;
29pub type TimedTaskHandle<T, V = (), C = ManualClock> = pi_async_runtime_core::task::TaskHandle<
30 pi_async_runtime_core::time::TimedTaskNotifier<ThreadNotifier, RuntimeTimerAccess<C>>,
31 T,
32 V,
33>;
34pub type TimedTaskNotifier<C = ManualClock> =
35 pi_async_runtime_core::time::TimedTaskNotifier<ThreadNotifier, RuntimeTimerAccess<C>>;
36
37pub struct ConfiguredRuntime<M, C, P, H>
45where
46 M: RuntimeMode,
47 C: ExecutorClock<Instant = GlobalManualInstant>,
48 P: IdlePolicy,
49 H: ExecutorHooks,
50{
51 pub(crate) shared: RuntimeHandle<C>,
52 pub(crate) data: RefCell<Option<RuntimeData<M, C, P, H>>>,
53 pub(crate) binding: Rc<RuntimeBinding>,
54}
55pub(crate) struct RuntimeData<M, C, P, H>
56where
57 M: RuntimeMode,
58 C: ExecutorClock<Instant = GlobalManualInstant>,
59 P: IdlePolicy,
60 H: ExecutorHooks,
61{
62 pub(crate) executor: Executor<C>,
63 pub(crate) mode: M,
64 pub(crate) policy: P,
65 pub(crate) hooks: H,
66}
67pub type Handle = RuntimeHandle<ManualClock>;
70pub type Runtime = ConfiguredRuntime<ConcurrentMode, ManualClock, DefaultIdlePolicy, NoHooks>;
72
73pub struct RuntimeHandle<C>
77where
78 C: ExecutorClock<Instant = GlobalManualInstant>,
79{
80 pub(crate) inner: Arc<Shared<C>>,
81}
82pub type SerialRuntime = ConfiguredRuntime<SerialMode, ManualClock, DefaultIdlePolicy, NoHooks>;
84pub type ConcurrentRuntime =
86 ConfiguredRuntime<ConcurrentMode, ManualClock, DefaultIdlePolicy, NoHooks>;
87
88impl<M: RuntimeMode> ConfiguredRuntime<M, ManualClock, DefaultIdlePolicy, NoHooks> {
89 pub fn new() -> pi_result::Result<Self> {
91 runtime_new_inner(RuntimeOptions::default())
92 }
93 pub fn with_options(options: RuntimeOptions) -> pi_result::Result<Self> {
95 runtime_new_inner(options)
96 }
97 pub fn with_components<C, P, H>(
99 clock: C,
100 policy: P,
101 hooks: H,
102 options: RuntimeOptions,
103 ) -> pi_result::Result<ConfiguredRuntime<M, C, P, H>>
104 where
105 C: ExecutorClock<Instant = GlobalManualInstant>,
106 P: IdlePolicy,
107 H: ExecutorHooks,
108 {
109 runtime_components_inner(clock, policy, hooks, options)
110 }
111}
112impl<M, C, P, H> ConfiguredRuntime<M, C, P, H>
113where
114 M: RuntimeMode,
115 C: ExecutorClock<Instant = GlobalManualInstant>,
116 P: IdlePolicy,
117 H: ExecutorHooks,
118{
119 pub fn from_components(
122 clock: C,
123 policy: P,
124 hooks: H,
125 options: RuntimeOptions,
126 ) -> pi_result::Result<Self> {
127 runtime_components_inner(clock, policy, hooks, options)
128 }
129 pub fn handle(&self) -> RuntimeHandle<C> {
131 self.shared.clone()
132 }
133 pub(crate) fn shared(&self) -> &RuntimeHandle<C> {
134 &self.shared
135 }
136}
137impl<C: ExecutorClock<Instant = GlobalManualInstant>> RuntimeHandle<C> {
138 pub(crate) fn shared(&self) -> &Self {
139 self
140 }
141}
142impl<C: ExecutorClock<Instant = GlobalManualInstant>> Clone for RuntimeHandle<C> {
143 fn clone(&self) -> Self {
144 runtime_handle_clone_inner(self)
145 }
146}
147impl<M, C, P, H> Drop for ConfiguredRuntime<M, C, P, H>
148where
149 M: RuntimeMode,
150 C: ExecutorClock<Instant = GlobalManualInstant>,
151 P: IdlePolicy,
152 H: ExecutorHooks,
153{
154 fn drop(&mut self) {
155 runtime_drop_inner(self)
156 }
157}
158include!("instance_inner.rs");