velox-engine 0.4.0

Velox core UI engine: arena, pod state, 1D fixed-point layout solver, damage tracker, SDF scanline rasterizer, and cross-platform GPU/software backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! 内置默认标准库调度器 [`NativeStdScheduler`]、统一定时器 [`Timer`]、可插拔 [`BackgroundExecutor`] 与轻量 [`block_on`]。
//!
//! 特点:
//! - 零外部异步运行时依赖(纯标准库 `Future` / `Waker` / `Condvar` / `BinaryHeap`);
//! - 后台线程池与单常驻最小堆定时器线程均惰性启动,未调用前 0 线程、0 内存占用,空闲 0% CPU;
//! - [`BackgroundExecutor`] 持有 `Arc<dyn SchedulerBackend>`,并通过通用可取消包装自动为任意后端提供 RAII `Task::drop` 取消语义。

use core::cmp::Reverse;
use core::future::Future;
use core::pin::{pin, Pin};
use core::sync::atomic::{AtomicBool, Ordering};
use core::task::{Context, Poll, Waker};
use core::time::Duration;
use std::boxed::Box;
use std::collections::{BinaryHeap, VecDeque};
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::task::Wake;
use std::time::Instant;

use super::backend::{BoxFuture, SchedulerBackend};
use super::scheduler::SchedulerCapabilities;
use super::task::{Task, TaskShared};

/// 在当前线程阻塞驱动任意 [`Future`] 至完成(用于无头工具、CLI 演示与单元测试)。
pub fn block_on<F: Future>(fut: F) -> F::Output {
    struct ThreadWaker {
        ready: Mutex<bool>,
        cv: Condvar,
    }
    impl Wake for ThreadWaker {
        fn wake(self: Arc<Self>) {
            self.wake_by_ref();
        }
        fn wake_by_ref(self: &Arc<Self>) {
            if let Ok(mut ready) = self.ready.lock() {
                *ready = true;
                self.cv.notify_one();
            }
        }
    }

    let signal = Arc::new(ThreadWaker {
        ready: Mutex::new(false),
        cv: Condvar::new(),
    });
    let waker = Waker::from(signal.clone());
    let mut cx = Context::from_waker(&waker);
    let mut fut = pin!(fut);

    loop {
        match fut.as_mut().poll(&mut cx) {
            Poll::Ready(out) => return out,
            Poll::Pending => {
                if let Ok(mut ready) = signal.ready.lock() {
                    while !*ready {
                        ready = match signal.cv.wait(ready) {
                            Ok(g) => g,
                            Err(poisoned) => poisoned.into_inner(),
                        };
                    }
                    *ready = false;
                }
            }
        }
    }
}

// ============================================================================
// 全局单常驻线程最小堆定时器(NativeTimerDriver)
// ============================================================================

#[cfg(not(target_arch = "wasm32"))]
struct TimerEntry {
    deadline: Instant,
    seq: u64,
    waker: Waker,
}

#[cfg(not(target_arch = "wasm32"))]
impl PartialEq for TimerEntry {
    fn eq(&self, other: &Self) -> bool {
        self.deadline == other.deadline && self.seq == other.seq
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Eq for TimerEntry {}

#[cfg(not(target_arch = "wasm32"))]
impl PartialOrd for TimerEntry {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Ord for TimerEntry {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.deadline
            .cmp(&other.deadline)
            .then_with(|| self.seq.cmp(&other.seq))
    }
}

#[cfg(not(target_arch = "wasm32"))]
struct TimerDriver {
    state: Mutex<(u64, BinaryHeap<Reverse<TimerEntry>>)>,
    cv: Condvar,
}

#[cfg(not(target_arch = "wasm32"))]
static TIMER_DRIVER: OnceLock<Arc<TimerDriver>> = OnceLock::new();

#[cfg(not(target_arch = "wasm32"))]
fn timer_driver() -> &'static Arc<TimerDriver> {
    TIMER_DRIVER.get_or_init(|| {
        let driver = Arc::new(TimerDriver {
            state: Mutex::new((0, BinaryHeap::new())),
            cv: Condvar::new(),
        });
        let bg = Arc::clone(&driver);
        let _ = std::thread::Builder::new()
            .name(std::string::String::from("velox-async-timer"))
            .stack_size(128 * 1024)
            .spawn(move || loop {
                let waker_to_fire = {
                    let mut guard = bg.state.lock().unwrap_or_else(|e| e.into_inner());
                    loop {
                        let Some(Reverse(top)) = guard.1.peek() else {
                            guard = bg.cv.wait(guard).unwrap_or_else(|e| e.into_inner());
                            continue;
                        };
                        let now = Instant::now();
                        if top.deadline <= now {
                            let Reverse(entry) = guard.1.pop().unwrap();
                            break entry.waker;
                        }
                        let wait_dur = top.deadline.saturating_duration_since(now);
                        let (next_guard, _) = bg
                            .cv
                            .wait_timeout(guard, wait_dur)
                            .unwrap_or_else(|e| e.into_inner());
                        guard = next_guard;
                    }
                };
                waker_to_fire.wake();
            });
        driver
    })
}

struct NativeTimer {
    deadline: Instant,
    registered: bool,
}

impl NativeTimer {
    #[inline]
    fn after(duration: Duration) -> Self {
        Self {
            deadline: Instant::now() + duration,
            registered: false,
        }
    }

    fn poll_native(&mut self, cx: &mut Context<'_>) -> Poll<()> {
        if Instant::now() >= self.deadline {
            return Poll::Ready(());
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            if !self.registered {
                self.registered = true;
                let driver = timer_driver();
                let mut guard = driver.state.lock().unwrap_or_else(|e| e.into_inner());
                guard.0 = guard.0.wrapping_add(1);
                let seq = guard.0;
                let is_earlier = guard
                    .1
                    .peek()
                    .map(|Reverse(top)| self.deadline < top.deadline)
                    .unwrap_or(true);
                guard.1.push(Reverse(TimerEntry {
                    deadline: self.deadline,
                    seq,
                    waker: cx.waker().clone(),
                }));
                if is_earlier {
                    driver.cv.notify_one();
                }
            }
            Poll::Pending
        }
        #[cfg(target_arch = "wasm32")]
        {
            let _ = cx;
            Poll::Ready(())
        }
    }
}

enum TimerInner {
    /// 默认原生最小堆定时器(零额外 `Box` 堆分配快路径)。
    Native(NativeTimer),
    /// 可插拔第三方或确定性调度器提供的自定义定时器 Future。
    Custom(BoxFuture<'static, ()>),
}

/// 跨调度器统一非阻塞异步定时器 [`Future`]。
///
/// - 使用默认 [`NativeStdScheduler`] 时走零 `Box` 快路径,由单常驻最小堆定时器线程驱动(空闲 0% CPU);
/// - 注入自定义 [`SchedulerBackend`](如 [`ManualScheduler`](super::ManualScheduler) 或未来的 Tokio 适配器)时,
///   自动透明切换至对应调度器的时钟实现,业务层无需修改任何代码。
pub struct Timer {
    inner: TimerInner,
}

impl Timer {
    /// 创建一个使用默认内置原生时钟驱动的异步定时器。
    #[inline]
    pub fn after(duration: Duration) -> Self {
        Self {
            inner: TimerInner::Native(NativeTimer::after(duration)),
        }
    }

    /// 将自定义调度器返回的任意 `Send + 'static` 定时 Future 包装为统一 [`Timer`]。
    #[inline]
    pub fn from_future(fut: BoxFuture<'static, ()>) -> Self {
        Self {
            inner: TimerInner::Custom(fut),
        }
    }
}

impl Unpin for Timer {}

impl Future for Timer {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match &mut self.get_mut().inner {
            TimerInner::Native(native) => native.poll_native(cx),
            TimerInner::Custom(custom) => custom.as_mut().poll(cx),
        }
    }
}

// ============================================================================
// 内置原生后台工作线程池(NativeStdScheduler)
// ============================================================================

#[cfg(not(target_arch = "wasm32"))]
struct BgJob {
    future: Mutex<Option<BoxFuture<'static, ()>>>,
    scheduled: AtomicBool,
}

#[cfg(not(target_arch = "wasm32"))]
impl BgJob {
    fn enqueue(self: &Arc<Self>) {
        if !self.scheduled.swap(true, Ordering::AcqRel) {
            let pool = bg_pool();
            let mut q = pool.queue.lock().unwrap_or_else(|e| e.into_inner());
            q.push_back(Arc::clone(self));
            pool.cv.notify_one();
        }
    }

    fn run(self: &Arc<Self>) {
        self.scheduled.store(false, Ordering::Release);

        // 先移出 Future 到局部栈再 poll,避免 Future 内部同步触发 wake 造成重入锁死锁
        let mut fut = {
            let mut slot = self.future.lock().unwrap_or_else(|e| e.into_inner());
            let Some(f) = slot.take() else {
                return;
            };
            f
        };

        let waker = Waker::from(Arc::clone(self));
        let mut cx = Context::from_waker(&waker);
        match fut.as_mut().poll(&mut cx) {
            Poll::Ready(()) => {}
            Poll::Pending => {
                let mut slot = self.future.lock().unwrap_or_else(|e| e.into_inner());
                *slot = Some(fut);
            }
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Wake for BgJob {
    fn wake(self: Arc<Self>) {
        self.enqueue();
    }
    fn wake_by_ref(self: &Arc<Self>) {
        self.enqueue();
    }
}

#[cfg(not(target_arch = "wasm32"))]
struct BackgroundPool {
    queue: Mutex<VecDeque<Arc<BgJob>>>,
    cv: Condvar,
}

#[cfg(not(target_arch = "wasm32"))]
static BG_POOL: OnceLock<Arc<BackgroundPool>> = OnceLock::new();

#[cfg(not(target_arch = "wasm32"))]
fn bg_pool() -> &'static Arc<BackgroundPool> {
    BG_POOL.get_or_init(|| {
        let pool = Arc::new(BackgroundPool {
            queue: Mutex::new(VecDeque::new()),
            cv: Condvar::new(),
        });
        let worker_count = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(2)
            .clamp(2, 4);
        for idx in 0..worker_count {
            let p = Arc::clone(&pool);
            let _ = std::thread::Builder::new()
                .name(std::format!("velox-bg-{idx}"))
                .stack_size(256 * 1024)
                .spawn(move || loop {
                    let job = {
                        let mut q = p.queue.lock().unwrap_or_else(|e| e.into_inner());
                        while q.is_empty() {
                            q = p.cv.wait(q).unwrap_or_else(|e| e.into_inner());
                        }
                        q.pop_front()
                    };
                    if let Some(job) = job {
                        job.run();
                    }
                });
        }
        pool
    })
}

/// Velox 内置默认标准库调度器后端(零外部依赖:常驻多线程池 + 单常驻最小堆定时器线程)。
pub struct NativeStdScheduler {
    epoch: Instant,
}

static GLOBAL_NATIVE_SCHEDULER: OnceLock<Arc<dyn SchedulerBackend>> = OnceLock::new();

impl NativeStdScheduler {
    /// 创建一个新的原生标准库调度器实例(共享惰性后台线程池与定时器线程)。
    pub fn new() -> Self {
        Self {
            epoch: Instant::now(),
        }
    }

    /// 获取进程级共享默认 [`NativeStdScheduler`] 后端引用(零重复分配)。
    pub fn global() -> Arc<dyn SchedulerBackend> {
        Arc::clone(GLOBAL_NATIVE_SCHEDULER.get_or_init(|| Arc::new(Self::new())))
    }
}

impl Default for NativeStdScheduler {
    fn default() -> Self {
        Self::new()
    }
}

impl SchedulerBackend for NativeStdScheduler {
    #[inline]
    fn name(&self) -> &'static str {
        "velox-native-std"
    }

    #[inline]
    fn capabilities(&self) -> SchedulerCapabilities {
        SchedulerCapabilities {
            name: self.name(),
            supports_background_threads: cfg!(not(target_arch = "wasm32")),
            supports_dynamic_spawn: true,
            deterministic_clock: false,
        }
    }

    fn spawn_background(&self, fut: BoxFuture<'static, ()>) {
        #[cfg(not(target_arch = "wasm32"))]
        {
            let job = Arc::new(BgJob {
                future: Mutex::new(Some(fut)),
                scheduled: AtomicBool::new(false),
            });
            job.enqueue();
        }
        #[cfg(target_arch = "wasm32")]
        {
            block_on(fut);
        }
    }

    #[inline]
    fn timer(&self, duration: Duration) -> Timer {
        Timer::after(duration)
    }

    #[inline]
    fn now_ms(&self) -> u64 {
        self.epoch.elapsed().as_millis().min(u64::MAX as u128) as u64
    }
}

// ============================================================================
// 通用可取消后台任务包装 + 可插拔 `BackgroundExecutor`
// ============================================================================

struct CancellableFuture<T, Fut> {
    fut: Option<Pin<Box<Fut>>>,
    shared: Arc<TaskShared<T>>,
    cancel_waker: Arc<Mutex<Option<Waker>>>,
}

impl<T, Fut> Unpin for CancellableFuture<T, Fut> {}

impl<T, Fut> Future for CancellableFuture<T, Fut>
where
    T: Send + 'static,
    Fut: Future<Output = T> + Send + 'static,
{
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        if this.shared.is_cancelled() {
            this.fut = None;
            let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
            *slot = None;
            return Poll::Ready(());
        }

        {
            let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
            let should_update = slot
                .as_ref()
                .map(|prev| !prev.will_wake(cx.waker()))
                .unwrap_or(true);
            if should_update {
                *slot = Some(cx.waker().clone());
            }
        }

        let Some(fut) = this.fut.as_mut() else {
            return Poll::Ready(());
        };

        match fut.as_mut().poll(cx) {
            Poll::Ready(output) => {
                this.fut = None;
                {
                    let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
                    *slot = None;
                }
                if !this.shared.is_cancelled() {
                    this.shared.complete(output);
                }
                Poll::Ready(())
            }
            Poll::Pending => {
                if this.shared.is_cancelled() {
                    this.fut = None;
                    let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
                    *slot = None;
                    return Poll::Ready(());
                }
                Poll::Pending
            }
        }
    }
}

/// 可插拔后台异步执行器句柄(轻量 `Clone`,面向 CPU 密集型计算、阻塞 I/O 与跨调度器定时)。
///
/// 内部持有 `Arc<dyn SchedulerBackend>`:默认指向 [`NativeStdScheduler::global`],
/// 也可通过 [`BackgroundExecutor::with_backend`] 或 [`crate::engine::VeloxEngine::with_scheduler`] 切换至任意外部调度器。
#[derive(Clone)]
pub struct BackgroundExecutor {
    backend: Arc<dyn SchedulerBackend>,
}

impl Default for BackgroundExecutor {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl core::fmt::Debug for BackgroundExecutor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("BackgroundExecutor")
            .field("scheduler", &self.backend.name())
            .finish()
    }
}

impl BackgroundExecutor {
    /// 创建一个绑定默认内置 [`NativeStdScheduler`] 的后台执行器句柄。
    #[inline]
    pub fn new() -> Self {
        Self {
            backend: NativeStdScheduler::global(),
        }
    }

    /// 创建一个绑定指定 [`SchedulerBackend`] 的后台执行器句柄。
    #[inline]
    pub fn with_backend(backend: Arc<dyn SchedulerBackend>) -> Self {
        Self { backend }
    }

    /// 获取底层 [`SchedulerBackend`] 引用。
    #[inline]
    pub fn backend(&self) -> &Arc<dyn SchedulerBackend> {
        &self.backend
    }

    /// 返回当前绑定的调度器名称。
    #[inline]
    pub fn scheduler_name(&self) -> &'static str {
        self.backend.name()
    }

    /// 返回当前绑定的调度器能力元信息。
    #[inline]
    pub fn capabilities(&self) -> SchedulerCapabilities {
        self.backend.capabilities()
    }

    /// 返回当前调度器单调时钟(毫秒)。
    #[inline]
    pub fn now_ms(&self) -> u64 {
        self.backend.now_ms()
    }

    /// 在所配置的后台调度器中派生 `Send + 'static` 异步任务并返回支持 RAII Drop 自动取消的 [`Task<T>`]。
    pub fn spawn<T, Fut>(&self, fut: Fut) -> Task<T>
    where
        T: Send + 'static,
        Fut: Future<Output = T> + Send + 'static,
    {
        let cancelled = Arc::new(AtomicBool::new(false));
        let cancel_waker: Arc<Mutex<Option<Waker>>> = Arc::new(Mutex::new(None));
        let cancel_waker_cb = Arc::clone(&cancel_waker);
        let on_cancel: Arc<dyn Fn() + Send + Sync> = Arc::new(move || {
            let waker = {
                let mut slot = cancel_waker_cb.lock().unwrap_or_else(|e| e.into_inner());
                slot.take()
            };
            if let Some(w) = waker {
                w.wake();
            }
        });
        let shared = TaskShared::new(cancelled, Some(on_cancel));
        let wrapped = CancellableFuture {
            fut: Some(Box::pin(fut)),
            shared: Arc::clone(&shared),
            cancel_waker,
        };
        self.backend.spawn_background(Box::pin(wrapped));
        Task::from_shared(shared)
    }

    /// 创建由当前调度器时钟驱动的非阻塞异步定时器(与 GPUI `cx.background_executor().timer(dur)` 对齐)。
    #[inline]
    pub fn timer(&self, duration: Duration) -> Timer {
        self.backend.timer(duration)
    }
}