euv-core 0.52.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::*;

/// Cache-line-isolated 128-byte aligned atomic counter eliminating false sharing across Apple Silicon (128B) and x86 (64B) cores.
#[repr(align(128))]
#[derive(Debug, Default)]
pub struct AlignedAtomicUsize {
    /// Underlying atomic counter padded to a dedicated 128-byte cache line.
    pub(crate) value: AtomicUsize,
}

/// Per-worker cache-line-isolated wake slot preventing N-way mutex convoys and cross-core invalidations.
#[cfg(not(target_arch = "wasm32"))]
#[repr(align(128))]
#[derive(Debug, Default)]
pub(crate) struct EngineWorkerSlot {
    /// Monotonically incremented dispatch epoch signaling new work to this worker.
    pub(crate) epoch: AtomicUsize,
    /// Non-zero while this worker thread is parked inside its private condvar wait.
    pub(crate) parked: AtomicUsize,
    /// Private mutex synchronizing only this worker's park and wake transitions.
    pub(crate) lock: Mutex<()>,
    /// Private condition variable waking only this worker on new work or pool shutdown.
    pub(crate) wake: Condvar,
}

/// Shared 128-byte aligned job dispatch header coordinating the calling thread and persistent background workers.
#[cfg(not(target_arch = "wasm32"))]
#[repr(align(128))]
#[derive(Debug, Default)]
pub(crate) struct EnginePoolShared {
    /// Raw pointer address of the active stack-pinned `EngineDispatchJob` (`0` while idle).
    pub(crate) active_job: AtomicUsize,
    /// Count of background workers woken for the active dispatch epoch.
    pub(crate) active_workers: AtomicUsize,
    /// Adaptive spin iteration limit (`ENGINE_WORKER_SPIN_LIMIT_ACTIVE` vs `ENGINE_WORKER_SPIN_LIMIT_IDLE`).
    pub(crate) spin_limit: AtomicUsize,
    /// Non-zero once pool shutdown has been initiated by `Drop`.
    pub(crate) shutdown: AtomicUsize,
    /// Cache-line-isolated flag set while the caller thread is parked awaiting worker completion.
    pub(crate) caller_parked: AlignedAtomicUsize,
    /// Mutex guarding caller parking transitions (never touched on the spin fast-path).
    pub(crate) caller_lock: Mutex<()>,
    /// Condition variable waking the caller thread when the final worker finishes an epoch.
    pub(crate) wake_caller: Condvar,
    /// Contention-free per-worker wake slots indexed by worker ID `0..worker_count`.
    pub(crate) slots: Vec<EngineWorkerSlot>,
}

/// Type-erased stack-pinned dispatch record executed lock-free by every worker participating in an epoch.
#[cfg(not(target_arch = "wasm32"))]
#[repr(align(128))]
pub(crate) struct EngineDispatchJob {
    /// Raw pointer to the caller's stack-pinned closure or task state (`0` invalid).
    pub(crate) payload_ptr: usize,
    /// Monomorphized trampoline invoking work for `(payload_ptr, worker_slot_index)`.
    pub(crate) invoke: unsafe fn(usize, usize),
    /// Cache-line-isolated count of background workers that have completed this epoch.
    pub(crate) workers_done: AlignedAtomicUsize,
}

/// Stack-pinned shared state for `EngineWorkerPool::parallel_for_chunks` atomic work-stealing.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) struct ParallelChunksState<'a, WorkFn>
where
    WorkFn: Fn(usize, usize, usize) + Sync,
{
    /// Total logical items partitioned into `[0..total_items)`.
    pub(crate) total_items: usize,
    /// Number of items processed per claimed chunk.
    pub(crate) grain_size: usize,
    /// Total number of chunks (`total_items.div_ceil(grain_size)`).
    pub(crate) chunk_count: usize,
    /// Cache-line-isolated atomic next-chunk cursor stolen lock-free by workers and caller.
    pub(crate) next_chunk: AlignedAtomicUsize,
    /// Caller-supplied stateless parallel work closure `(chunk_start, chunk_end, worker_index)`.
    pub(crate) work_fn: &'a WorkFn,
}

/// Stack-pinned shared state for `EngineWorkerPool::parallel_work_steal` single-item atomic stealing.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) struct ParallelStealState<'a, ItemType, WorkFn>
where
    ItemType: Sync,
    WorkFn: Fn(usize, &ItemType, usize) + Sync,
{
    /// Read-only slice of work items claimed one by one across workers.
    pub(crate) items: &'a [ItemType],
    /// Cache-line-isolated atomic next-item cursor stolen lock-free by workers and caller.
    pub(crate) next_index: AlignedAtomicUsize,
    /// Caller-supplied per-item closure `(item_index, item_ref, worker_index)`.
    pub(crate) work_fn: &'a WorkFn,
}

/// Persistent zero-dependency work-stealing thread pool shared by parallel CPU layout, paint packing, and software rasterization.
#[derive(Debug)]
pub struct EngineWorkerPool {
    /// Count of background worker threads (excluding the calling thread; `0` on `wasm32`).
    pub(crate) worker_count: usize,
    /// Non-blocking re-entrancy guard ensuring nested dispatches run serially without deadlock.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) dispatch_guard: Mutex<()>,
    /// Shared atomic epoch headers and per-worker wake slots.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) shared: Arc<EnginePoolShared>,
    /// Owned background worker thread handles joined cleanly on `Drop`.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) handles: Vec<JoinHandle<()>>,
}