velox_engine/async_rt/
scheduler.rs1use core::future::Future;
7use core::pin::Pin;
8use core::task::{Context, Poll};
9use core::time::Duration;
10
11use crate::engine::VeloxEngine;
12use crate::platform::{AsyncPlatform, PlatformControlFlow};
13
14#[derive(Copy, Clone, PartialEq, Eq, Debug)]
16pub struct SchedulerCapabilities {
17 pub name: &'static str,
19 pub supports_background_threads: bool,
21 pub supports_dynamic_spawn: bool,
23 pub deterministic_clock: bool,
25}
26
27#[derive(Default)]
31pub struct YieldNow {
32 yielded: bool,
33}
34
35impl YieldNow {
36 #[inline(always)]
38 pub const fn new() -> Self {
39 Self { yielded: false }
40 }
41}
42
43impl Unpin for YieldNow {}
44
45impl Future for YieldNow {
46 type Output = ();
47
48 #[inline]
49 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
50 if self.yielded {
51 Poll::Ready(())
52 } else {
53 self.yielded = true;
54 cx.waker().wake_by_ref();
55 Poll::Pending
56 }
57 }
58}
59
60pub trait SchedulerClock {
66 fn now_ms(&self) -> u64;
68
69 fn sleep(&self, duration: Duration) -> impl Future<Output = ()>;
71}
72
73pub trait AsyncDriver: SchedulerClock {
79 fn capabilities(&self) -> SchedulerCapabilities;
81
82 fn wait_for_event(&self) -> impl Future<Output = ()>;
86
87 #[inline]
89 fn yield_now(&self) -> impl Future<Output = ()> {
90 YieldNow::new()
91 }
92}
93
94#[derive(Copy, Clone, PartialEq, Eq, Debug)]
96pub struct AsyncLoopConfig {
97 pub frame_interval: Duration,
99 pub max_steps: Option<u32>,
101 pub exit_on_idle: bool,
103}
104
105impl Default for AsyncLoopConfig {
106 #[inline(always)]
107 fn default() -> Self {
108 Self {
109 frame_interval: Duration::from_millis(16),
110 max_steps: None,
111 exit_on_idle: false,
112 }
113 }
114}
115
116impl AsyncLoopConfig {
117 #[inline(always)]
119 pub const fn new(frame_interval: Duration) -> Self {
120 Self {
121 frame_interval,
122 max_steps: None,
123 exit_on_idle: false,
124 }
125 }
126
127 #[inline(always)]
129 pub const fn with_max_steps(mut self, max_steps: u32) -> Self {
130 self.max_steps = Some(max_steps);
131 self
132 }
133
134 #[inline(always)]
136 pub const fn with_exit_on_idle(mut self, exit_on_idle: bool) -> Self {
137 self.exit_on_idle = exit_on_idle;
138 self
139 }
140}
141
142#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
144pub struct AsyncLoopReport {
145 pub steps: u32,
147 pub idle_waits: u32,
149 pub last_control_flow: Option<PlatformControlFlow>,
151}
152
153pub async fn run_async_event_loop<P, D>(
162 platform: &mut P,
163 engine: &mut VeloxEngine,
164 driver: &D,
165 config: AsyncLoopConfig,
166) -> AsyncLoopReport
167where
168 P: AsyncPlatform,
169 D: AsyncDriver,
170{
171 let mut report = AsyncLoopReport::default();
172 let mut last_ms = driver.now_ms();
173 let frame_ms = config.frame_interval.as_millis().min(u64::MAX as u128) as u64;
174
175 loop {
176 if let Some(limit) = config.max_steps {
177 if report.steps >= limit {
178 break;
179 }
180 }
181
182 let step_start_ms = driver.now_ms();
183 let dt_ms = step_start_ms.saturating_sub(last_ms).clamp(1, 100) as u16;
184 last_ms = step_start_ms;
185
186 let flow = platform.step_platform_async(engine, dt_ms).await;
187 report.steps = report.steps.saturating_add(1);
188 report.last_control_flow = Some(flow);
189
190 match flow {
191 PlatformControlFlow::Exit => break,
192 PlatformControlFlow::Wait => {
193 if engine.needs_redraw() {
195 driver.yield_now().await;
196 continue;
197 }
198 report.idle_waits = report.idle_waits.saturating_add(1);
199 if config.exit_on_idle {
200 break;
201 }
202 driver.wait_for_event().await;
203 last_ms = driver.now_ms();
204 }
205 PlatformControlFlow::Poll => {
206 let elapsed_ms = driver.now_ms().saturating_sub(step_start_ms);
207 if frame_ms > 0 && elapsed_ms < frame_ms {
208 driver
209 .sleep(Duration::from_millis(frame_ms - elapsed_ms))
210 .await;
211 } else {
212 driver.yield_now().await;
213 }
214 }
215 }
216 }
217
218 report
219}