euv_core/parallel/struct.rs
1use super::*;
2
3/// Cache-line-isolated 128-byte aligned atomic counter eliminating false sharing across Apple Silicon (128B) and x86 (64B) cores.
4#[repr(align(128))]
5#[derive(Debug, Default)]
6pub struct AlignedAtomicUsize {
7 /// Underlying atomic counter padded to a dedicated 128-byte cache line.
8 pub(crate) value: AtomicUsize,
9}
10
11/// Per-worker cache-line-isolated wake slot preventing N-way mutex convoys and cross-core invalidations.
12#[cfg(not(target_arch = "wasm32"))]
13#[repr(align(128))]
14#[derive(Debug, Default)]
15pub(crate) struct EngineWorkerSlot {
16 /// Monotonically incremented dispatch epoch signaling new work to this worker.
17 pub(crate) epoch: AtomicUsize,
18 /// Non-zero while this worker thread is parked inside its private condvar wait.
19 pub(crate) parked: AtomicUsize,
20 /// Private mutex synchronizing only this worker's park and wake transitions.
21 pub(crate) lock: Mutex<()>,
22 /// Private condition variable waking only this worker on new work or pool shutdown.
23 pub(crate) wake: Condvar,
24}
25
26/// Shared 128-byte aligned job dispatch header coordinating the calling thread and persistent background workers.
27#[cfg(not(target_arch = "wasm32"))]
28#[repr(align(128))]
29#[derive(Debug, Default)]
30pub(crate) struct EnginePoolShared {
31 /// Raw pointer address of the active stack-pinned `EngineDispatchJob` (`0` while idle).
32 pub(crate) active_job: AtomicUsize,
33 /// Count of background workers woken for the active dispatch epoch.
34 pub(crate) active_workers: AtomicUsize,
35 /// Adaptive spin iteration limit (`ENGINE_WORKER_SPIN_LIMIT_ACTIVE` vs `ENGINE_WORKER_SPIN_LIMIT_IDLE`).
36 pub(crate) spin_limit: AtomicUsize,
37 /// Non-zero once pool shutdown has been initiated by `Drop`.
38 pub(crate) shutdown: AtomicUsize,
39 /// Cache-line-isolated flag set while the caller thread is parked awaiting worker completion.
40 pub(crate) caller_parked: AlignedAtomicUsize,
41 /// Mutex guarding caller parking transitions (never touched on the spin fast-path).
42 pub(crate) caller_lock: Mutex<()>,
43 /// Condition variable waking the caller thread when the final worker finishes an epoch.
44 pub(crate) wake_caller: Condvar,
45 /// Contention-free per-worker wake slots indexed by worker ID `0..worker_count`.
46 pub(crate) slots: Vec<EngineWorkerSlot>,
47}
48
49/// Type-erased stack-pinned dispatch record executed lock-free by every worker participating in an epoch.
50#[cfg(not(target_arch = "wasm32"))]
51#[repr(align(128))]
52pub(crate) struct EngineDispatchJob {
53 /// Raw pointer to the caller's stack-pinned closure or task state (`0` invalid).
54 pub(crate) payload_ptr: usize,
55 /// Monomorphized trampoline invoking work for `(payload_ptr, worker_slot_index)`.
56 pub(crate) invoke: unsafe fn(usize, usize),
57 /// Cache-line-isolated count of background workers that have completed this epoch.
58 pub(crate) workers_done: AlignedAtomicUsize,
59}
60
61/// Stack-pinned shared state for `EngineWorkerPool::parallel_for_chunks` atomic work-stealing.
62#[cfg(not(target_arch = "wasm32"))]
63pub(crate) struct ParallelChunksState<'a, WorkFn>
64where
65 WorkFn: Fn(usize, usize, usize) + Sync,
66{
67 /// Total logical items partitioned into `[0..total_items)`.
68 pub(crate) total_items: usize,
69 /// Number of items processed per claimed chunk.
70 pub(crate) grain_size: usize,
71 /// Total number of chunks (`total_items.div_ceil(grain_size)`).
72 pub(crate) chunk_count: usize,
73 /// Cache-line-isolated atomic next-chunk cursor stolen lock-free by workers and caller.
74 pub(crate) next_chunk: AlignedAtomicUsize,
75 /// Caller-supplied stateless parallel work closure `(chunk_start, chunk_end, worker_index)`.
76 pub(crate) work_fn: &'a WorkFn,
77}
78
79/// Stack-pinned shared state for `EngineWorkerPool::parallel_work_steal` single-item atomic stealing.
80#[cfg(not(target_arch = "wasm32"))]
81pub(crate) struct ParallelStealState<'a, ItemType, WorkFn>
82where
83 ItemType: Sync,
84 WorkFn: Fn(usize, &ItemType, usize) + Sync,
85{
86 /// Read-only slice of work items claimed one by one across workers.
87 pub(crate) items: &'a [ItemType],
88 /// Cache-line-isolated atomic next-item cursor stolen lock-free by workers and caller.
89 pub(crate) next_index: AlignedAtomicUsize,
90 /// Caller-supplied per-item closure `(item_index, item_ref, worker_index)`.
91 pub(crate) work_fn: &'a WorkFn,
92}
93
94/// Persistent zero-dependency work-stealing thread pool shared by parallel CPU layout, paint packing, and software rasterization.
95#[derive(Debug)]
96pub struct EngineWorkerPool {
97 /// Count of background worker threads (excluding the calling thread; `0` on `wasm32`).
98 pub(crate) worker_count: usize,
99 /// Non-blocking re-entrancy guard ensuring nested dispatches run serially without deadlock.
100 #[cfg(not(target_arch = "wasm32"))]
101 pub(crate) dispatch_guard: Mutex<()>,
102 /// Shared atomic epoch headers and per-worker wake slots.
103 #[cfg(not(target_arch = "wasm32"))]
104 pub(crate) shared: Arc<EnginePoolShared>,
105 /// Owned background worker thread handles joined cleanly on `Drop`.
106 #[cfg(not(target_arch = "wasm32"))]
107 pub(crate) handles: Vec<JoinHandle<()>>,
108}