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

/// Lock-free atomic cancellation token shared between streaming requesters and background decoders.
#[derive(Clone, Debug, Default)]
pub struct CancelToken {
    /// Atomic cancellation flag (`0 = active`, `1 = cancelled`).
    pub(crate) state: Arc<AtomicU8>,
}

/// Handle referencing an active or coalesced asynchronous asset streaming request.
#[derive(Clone, Data, Debug, Default, New)]
pub struct StreamRequestHandle {
    /// Deterministic 64-bit asset key hash.
    #[get(type(copy))]
    pub(crate) key_hash: u64,
    /// Priority lane assigned to this request.
    #[get(type(copy))]
    pub(crate) priority: StreamPriority,
    /// Shared lock-free cancellation token for this request.
    pub(crate) cancel_token: CancelToken,
}

/// Single queued asynchronous asset decode work item stored in one of the 4 priority lanes.
#[derive(Clone, Data, Debug)]
pub struct StreamWorkItem {
    /// Deterministic 64-bit FNV-1a asset key hash.
    #[get(type(copy))]
    pub(crate) key_hash: u64,
    /// Priority lane from which this work item was scheduled.
    #[get(type(copy))]
    pub(crate) priority: StreamPriority,
    /// Semantic asset decode category.
    #[get(type(copy))]
    pub(crate) kind: StreamJobKind,
    /// Target progressive texture residency tier when decoding a texture.
    #[get(type(copy))]
    pub(crate) target_tier: TextureResidencyTier,
    /// Target GPU bindless texture slot index.
    #[get(type(copy))]
    pub(crate) texture_slot: u32,
    /// Monotonic generation stamped on enqueue or reprioritization to discard stale lane duplicates in O(1).
    #[get(type(copy))]
    pub(crate) generation: u64,
    /// Shared lock-free cancellation token checked before and during chunked decoding.
    pub(crate) cancel_token: CancelToken,
    /// True when texture texels use sRGB color encoding.
    #[get(type(copy))]
    pub(crate) is_srgb: bool,
}

/// Coalescing table entry tracking the latest active priority, tier, and generation for one in-flight asset key.
#[derive(Clone, Debug)]
pub(crate) struct ActiveStreamEntry {
    /// Current highest-urgency priority assigned to this key.
    pub(crate) priority: StreamPriority,
    /// Latest generation value (supersedes older queued items in lower lanes).
    pub(crate) generation: u64,
    /// Shared atomic cancellation token.
    pub(crate) cancel_token: CancelToken,
    /// Semantic decode job category.
    pub(crate) kind: StreamJobKind,
    /// Highest requested texture residency tier.
    pub(crate) target_tier: TextureResidencyTier,
    /// Target GPU bindless texture slot index.
    pub(crate) texture_slot: u32,
    /// True when texture texels use sRGB color encoding.
    pub(crate) is_srgb: bool,
}

/// Mutex-protected 4-lane priority queues and O(1) active coalescing registry.
#[derive(Debug, Default)]
pub(crate) struct PriorityQueueInner {
    /// Four FIFO work-stealing lanes ordered `0=Critical .. 3=BackgroundLow`.
    pub(crate) lanes: [VecDeque<StreamWorkItem>; STREAM_PRIORITY_LANE_COUNT],
    /// O(1) active request coalescing table keyed by 64-bit asset hash.
    pub(crate) active_entries: HashMap<u64, ActiveStreamEntry>,
}

/// Lock-light 4-lane priority work-stealing streaming queue with request coalescing, dynamic reprioritization, and atomic cancellation.
#[derive(Debug)]
pub struct PriorityStreamQueue {
    /// Synchronized 4-lane deques and coalescing map.
    pub(crate) inner: Mutex<PriorityQueueInner>,
    /// Lock-free 4-bit occupancy bitmask avoiding mutex acquisition on empty queues.
    pub(crate) non_empty_mask: AtomicU8,
    /// Approximate count of queued lane entries for fast telemetry.
    pub(crate) queued_count: AtomicUsize,
    /// Monotonic generation counter for O(1) stale reprioritized item elision.
    pub(crate) next_generation: AtomicU64,
    /// Atomic shutdown signal waking all parked background worker threads.
    pub(crate) shutdown: AtomicBool,
    /// Native condition variable waking background workers when work arrives.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) wake_signal: Condvar,
}