euv-core 0.51.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Wait-free telemetry snapshot describing `UiDispatcher` queue depth and backpressure behavior.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct UiDispatchMetrics {
    /// Number of queued tasks currently awaiting UI-thread execution.
    #[get(type(copy))]
    pub(crate) pending_count: usize,
    /// Configured maximum buffered task capacity.
    #[get(type(copy))]
    pub(crate) capacity: usize,
    /// High-water mark of simultaneously buffered tasks.
    #[get(type(copy))]
    pub(crate) peak_depth: usize,
    /// Cumulative number of tasks successfully enqueued.
    #[get(type(copy))]
    pub(crate) enqueued_total: u64,
    /// Cumulative number of tasks executed on the UI thread.
    #[get(type(copy))]
    pub(crate) drained_total: u64,
    /// Cumulative number of tasks rejected due to `RejectNew` backpressure or closed state.
    #[get(type(copy))]
    pub(crate) rejected_total: u64,
    /// Cumulative number of older tasks evicted under `DropOldest` backpressure.
    #[get(type(copy))]
    pub(crate) dropped_total: u64,
    /// Cumulative number of coalesced event-loop wakeup callback invocations.
    #[get(type(copy))]
    pub(crate) wakeup_invocations: u64,
    /// Whether the dispatcher has been closed to new submissions.
    #[get(type(copy))]
    pub(crate) closed: bool,
}

/// Execution summary returned by one UI-thread `UiDispatcher` drain pass.
#[derive(Clone, Copy, Data, Debug, Default, Eq, New, PartialEq)]
pub struct UiDrainSummary {
    /// Number of queued tasks executed during this drain pass.
    #[get(type(copy))]
    pub(crate) executed: usize,
    /// Number of queued tasks still pending after this drain pass.
    #[get(type(copy))]
    pub(crate) remaining: usize,
    /// Number of task panics intercepted when draining through an `ErrorBoundary`.
    #[get(type(copy))]
    pub(crate) panics_caught: usize,
}

/// Internal queued UI mutation envelope paired with its monotonic submission sequence.
pub(crate) struct UiDispatchTask {
    /// Monotonic submission sequence number.
    pub(crate) sequence: u64,
    /// Assigned scheduling lane.
    pub(crate) priority: DispatchPriority,
    /// Sendable closure executed on the UI thread.
    pub(crate) callback: Box<dyn FnOnce() + Send + 'static>,
}

/// Priority-laned FIFO storage and optional OS event-loop wakeup hook guarded by the queue mutex.
#[derive(Data, Default)]
pub(crate) struct UiDispatcherQueues {
    /// Highest-priority lane drained ahead of normal and background work.
    pub(crate) critical: VecDeque<UiDispatchTask>,
    /// Default application task lane.
    pub(crate) normal: VecDeque<UiDispatchTask>,
    /// Lowest-priority deferred work lane.
    pub(crate) background: VecDeque<UiDispatchTask>,
    /// Monotonic sequence counter for enqueued tasks.
    #[get(type(copy))]
    pub(crate) next_sequence: u64,
    /// Optional event-loop wakeup callback invoked once per idle-to-pending transition.
    #[get(skip)]
    pub(crate) wakeup_hook: Option<Arc<dyn Fn() + Send + Sync>>,
}

/// Shared atomic telemetry and synchronized priority queue backing cloned `UiDispatcher` handles.
#[derive(Getter)]
pub(crate) struct UiDispatcherShared {
    /// Maximum number of queued tasks permitted across all priority lanes.
    #[get(type(copy))]
    pub(crate) capacity: usize,
    /// Overflow backpressure policy applied when `pending_count >= capacity`.
    #[get(type(copy))]
    pub(crate) policy: DispatchBackpressurePolicy,
    /// Synchronized priority lanes and wakeup callback slot.
    pub(crate) queues: Mutex<UiDispatcherQueues>,
    /// Atomic mirror of total queued task count enabling lock-free `has_pending()` checks.
    pub(crate) pending_count: AtomicUsize,
    /// Atomic peak queue depth observed across the dispatcher lifetime.
    pub(crate) peak_depth: AtomicUsize,
    /// Cumulative accepted task count.
    pub(crate) enqueued_total: AtomicU64,
    /// Cumulative drained task count.
    pub(crate) drained_total: AtomicU64,
    /// Cumulative rejected task count.
    pub(crate) rejected_total: AtomicU64,
    /// Cumulative evicted task count under `DropOldest`.
    pub(crate) dropped_total: AtomicU64,
    /// Cumulative coalesced wakeup callback invocations.
    pub(crate) wakeup_invocations: AtomicU64,
    /// Coalescing flag preventing redundant OS wakeups while a drain is already pending.
    pub(crate) wakeup_pending: AtomicBool,
    /// Atomic closure gate preventing further submissions once closed.
    pub(crate) closed: AtomicBool,
}

/// Cloneable thread-safe handle for posting UI and `REACTIVE_RUNTIME` mutations from any background thread.
#[derive(Clone, Getter)]
pub struct UiDispatcher {
    /// Shared dispatcher queue, atomic metrics, and wakeup hook state.
    #[get(pub(crate))]
    pub(crate) shared: Arc<UiDispatcherShared>,
}