Skip to main content

euv_core/runtime/streaming/queue/
struct.rs

1use super::*;
2
3/// Lock-free atomic cancellation token shared between streaming requesters and background decoders.
4#[derive(Clone, Debug, Default)]
5pub struct CancelToken {
6    /// Atomic cancellation flag (`0 = active`, `1 = cancelled`).
7    pub(crate) state: Arc<AtomicU8>,
8}
9
10/// Handle referencing an active or coalesced asynchronous asset streaming request.
11#[derive(Clone, Data, Debug, Default, New)]
12pub struct StreamRequestHandle {
13    /// Deterministic 64-bit asset key hash.
14    #[get(type(copy))]
15    pub(crate) key_hash: u64,
16    /// Priority lane assigned to this request.
17    #[get(type(copy))]
18    pub(crate) priority: StreamPriority,
19    /// Shared lock-free cancellation token for this request.
20    pub(crate) cancel_token: CancelToken,
21}
22
23/// Single queued asynchronous asset decode work item stored in one of the 4 priority lanes.
24#[derive(Clone, Data, Debug)]
25pub struct StreamWorkItem {
26    /// Deterministic 64-bit FNV-1a asset key hash.
27    #[get(type(copy))]
28    pub(crate) key_hash: u64,
29    /// Priority lane from which this work item was scheduled.
30    #[get(type(copy))]
31    pub(crate) priority: StreamPriority,
32    /// Semantic asset decode category.
33    #[get(type(copy))]
34    pub(crate) kind: StreamJobKind,
35    /// Target progressive texture residency tier when decoding a texture.
36    #[get(type(copy))]
37    pub(crate) target_tier: TextureResidencyTier,
38    /// Target GPU bindless texture slot index.
39    #[get(type(copy))]
40    pub(crate) texture_slot: u32,
41    /// Monotonic generation stamped on enqueue or reprioritization to discard stale lane duplicates in O(1).
42    #[get(type(copy))]
43    pub(crate) generation: u64,
44    /// Shared lock-free cancellation token checked before and during chunked decoding.
45    pub(crate) cancel_token: CancelToken,
46    /// True when texture texels use sRGB color encoding.
47    #[get(type(copy))]
48    pub(crate) is_srgb: bool,
49}
50
51/// Coalescing table entry tracking the latest active priority, tier, and generation for one in-flight asset key.
52#[derive(Clone, Debug)]
53pub(crate) struct ActiveStreamEntry {
54    /// Current highest-urgency priority assigned to this key.
55    pub(crate) priority: StreamPriority,
56    /// Latest generation value (supersedes older queued items in lower lanes).
57    pub(crate) generation: u64,
58    /// Shared atomic cancellation token.
59    pub(crate) cancel_token: CancelToken,
60    /// Semantic decode job category.
61    pub(crate) kind: StreamJobKind,
62    /// Highest requested texture residency tier.
63    pub(crate) target_tier: TextureResidencyTier,
64    /// Target GPU bindless texture slot index.
65    pub(crate) texture_slot: u32,
66    /// True when texture texels use sRGB color encoding.
67    pub(crate) is_srgb: bool,
68}
69
70/// Mutex-protected 4-lane priority queues and O(1) active coalescing registry.
71#[derive(Debug, Default)]
72pub(crate) struct PriorityQueueInner {
73    /// Four FIFO work-stealing lanes ordered `0=Critical .. 3=BackgroundLow`.
74    pub(crate) lanes: [VecDeque<StreamWorkItem>; STREAM_PRIORITY_LANE_COUNT],
75    /// O(1) active request coalescing table keyed by 64-bit asset hash.
76    pub(crate) active_entries: HashMap<u64, ActiveStreamEntry>,
77}
78
79/// Lock-light 4-lane priority work-stealing streaming queue with request coalescing, dynamic reprioritization, and atomic cancellation.
80#[derive(Debug)]
81pub struct PriorityStreamQueue {
82    /// Synchronized 4-lane deques and coalescing map.
83    pub(crate) inner: Mutex<PriorityQueueInner>,
84    /// Lock-free 4-bit occupancy bitmask avoiding mutex acquisition on empty queues.
85    pub(crate) non_empty_mask: AtomicU8,
86    /// Approximate count of queued lane entries for fast telemetry.
87    pub(crate) queued_count: AtomicUsize,
88    /// Monotonic generation counter for O(1) stale reprioritized item elision.
89    pub(crate) next_generation: AtomicU64,
90    /// Atomic shutdown signal waking all parked background worker threads.
91    pub(crate) shutdown: AtomicBool,
92    /// Native condition variable waking background workers when work arrives.
93    #[cfg(not(target_arch = "wasm32"))]
94    pub(crate) wake_signal: Condvar,
95}