|
1 | | -// Defines a high-performance, priority-aware, work-stealing deque for |
2 | | -// distributing tasks among scheduler workers. |
| 1 | +// @module StealingQueue |
| 2 | +// @description A generic, priority-aware, work-stealing queue implementation. |
| 3 | +// This module is self-contained and can be used by any scheduler or application |
| 4 | +// to manage and distribute tasks of any type `T`. |
3 | 5 |
|
4 | | -use crossbeam_deque::{Injector, Stealer, Worker}; |
| 6 | +#![allow(non_snake_case, non_camel_case_types)] |
| 7 | + |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use crossbeam_deque::{Injector, Stealer, Worker as WorkerDeque}; |
5 | 11 | use rand::seq::SliceRandom; |
6 | 12 |
|
7 | | -use crate::Task::{Priority::Enum, Task::Struct}; |
| 13 | +// The task must have a way to specify its priority. We define a trait for this. |
| 14 | +pub trait Prioritized { |
| 15 | + type P: PartialEq + Eq + Copy; |
8 | 16 |
|
9 | | -/// A container for a set of queues for a single priority level. |
10 | | -struct PriorityQueueSet { |
11 | | - GlobalInjector:Injector<Struct>, |
12 | | - WorkerQueue:Vec<Worker<Struct>>, |
13 | | - Stealer:Vec<Stealer<Struct>>, |
| 17 | + fn GetPriority(&self) -> Self::P; |
14 | 18 | } |
15 | 19 |
|
16 | | -impl PriorityQueueSet { |
17 | | - /// Creates a new set of queues for a given number of workers. |
18 | | - fn New(NumberOfWorker:usize) -> Self { |
19 | | - let WorkerQueue:Vec<Worker<Struct>> = (0..NumberOfWorker).map(|_| Worker::new_fifo()).collect(); |
20 | | - Self { |
21 | | - GlobalInjector:Injector::new(), |
22 | | - Stealer:WorkerQueue.iter().map(|w| w.stealer()).collect(), |
23 | | - WorkerQueue, |
24 | | - } |
25 | | - } |
| 20 | +// A simple enum for the library to use. The consumer's task must map to this. |
| 21 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 22 | +pub enum Priority { |
| 23 | + High, |
| 24 | + |
| 25 | + Normal, |
| 26 | + |
| 27 | + Low, |
26 | 28 | } |
27 | 29 |
|
28 | | -/// A collection of worker deques that supports priority-aware work-stealing. |
29 | | -/// |
30 | | -/// This struct holds three distinct sets of queues, one for each priority level |
31 | | -/// (`High`, `Normal`, `Low`). When a worker needs a task, it always checks for |
32 | | -/// higher-priority work before considering lower-priority work. |
33 | | -pub(crate) struct StealingQueue { |
34 | | - High:PriorityQueueSet, |
35 | | - Normal:PriorityQueueSet, |
36 | | - Low:PriorityQueueSet, |
| 30 | +// The parts of the queue that can be safely shared across all threads. |
| 31 | +struct Shared<T> { |
| 32 | + // High, Normal, Low |
| 33 | + Injector:(Injector<T>, Injector<T>, Injector<T>), |
| 34 | + |
| 35 | + // High, Normal, Low |
| 36 | + Stealer:(Vec<Stealer<T>>, Vec<Stealer<T>>, Vec<Stealer<T>>), |
37 | 37 | } |
38 | 38 |
|
39 | | -impl StealingQueue { |
40 | | - /// Creates a new `StealingQueue` with a dedicated set of queues for each |
41 | | - /// priority level. |
42 | | - pub fn New(NumberOfWorker:usize) -> Self { |
43 | | - Self { |
44 | | - High:PriorityQueueSet::New(NumberOfWorker), |
45 | | - Normal:PriorityQueueSet::New(NumberOfWorker), |
46 | | - Low:PriorityQueueSet::New(NumberOfWorker), |
47 | | - } |
48 | | - } |
| 39 | +/// The public-facing work-stealing queue. It is generic over the task type `T`. |
| 40 | +/// This object is held by the task submitter. |
| 41 | +pub struct StealingQueue<T:Prioritized<P = Priority>> { |
| 42 | + Shared:Arc<Shared<T>>, |
| 43 | +} |
| 44 | + |
| 45 | +/// A context object that contains everything a single worker thread needs to |
| 46 | +/// operate. This includes its own private, thread-local deques. |
| 47 | +pub struct WorkerContext<T> { |
| 48 | + Id:usize, |
| 49 | + |
| 50 | + // High, Normal, Low |
| 51 | + Local:(WorkerDeque<T>, WorkerDeque<T>, WorkerDeque<T>), |
| 52 | + |
| 53 | + Shared:Arc<Shared<T>>, |
| 54 | +} |
| 55 | + |
| 56 | +impl<T:Prioritized<P = Priority>> StealingQueue<T> { |
| 57 | + /// Creates a new work-stealing queue system. |
| 58 | + /// |
| 59 | + /// Returns a tuple containing: |
| 60 | + /// 1. The `StealingQueue` for submitting tasks. |
| 61 | + /// 2. A `Vec` of `WorkerContext`s, one for each worker to be spawned. |
| 62 | + pub fn New(Count:usize) -> (Self, Vec<WorkerContext<T>>) { |
| 63 | + let mut High:Vec<WorkerDeque<T>> = Vec::with_capacity(Count); |
| 64 | + |
| 65 | + let mut Normal:Vec<WorkerDeque<T>> = Vec::with_capacity(Count); |
| 66 | + |
| 67 | + let mut Low:Vec<WorkerDeque<T>> = Vec::with_capacity(Count); |
| 68 | + |
| 69 | + // --- FIX: Use the documented API for creating Worker/Stealer pairs --- |
| 70 | + let StealerHigh:Vec<Stealer<T>> = (0..Count) |
| 71 | + .map(|_| { |
| 72 | + // 1. Create the Worker. |
| 73 | + let Worker = WorkerDeque::new_fifo(); |
| 74 | + |
| 75 | + // 2. Get its Stealer. |
| 76 | + let Stealer = Worker.stealer(); |
| 77 | + |
| 78 | + // 3. Store the Worker part. |
| 79 | + High.push(Worker); |
| 80 | + |
| 81 | + // 4. Return the Stealer part. |
| 82 | + Stealer |
| 83 | + }) |
| 84 | + .collect(); |
49 | 85 |
|
50 | | - /// Submits a task to the appropriate global injection queue based on its |
51 | | - /// priority. |
52 | | - pub fn Push(&self, Task:Struct) { |
53 | | - match Task.Priority { |
54 | | - Enum::High => self.High.GlobalInjector.push(Task), |
55 | | - Enum::Normal => self.Normal.GlobalInjector.push(Task), |
56 | | - Enum::Low => self.Low.GlobalInjector.push(Task), |
| 86 | + let StealerNormal:Vec<Stealer<T>> = (0..Count) |
| 87 | + .map(|_| { |
| 88 | + let Worker = WorkerDeque::new_fifo(); |
| 89 | + |
| 90 | + let Stealer = Worker.stealer(); |
| 91 | + |
| 92 | + Normal.push(Worker); |
| 93 | + |
| 94 | + Stealer |
| 95 | + }) |
| 96 | + .collect(); |
| 97 | + |
| 98 | + let StealerLow:Vec<Stealer<T>> = (0..Count) |
| 99 | + .map(|_| { |
| 100 | + let Worker = WorkerDeque::new_fifo(); |
| 101 | + |
| 102 | + let Stealer = Worker.stealer(); |
| 103 | + |
| 104 | + Low.push(Worker); |
| 105 | + |
| 106 | + Stealer |
| 107 | + }) |
| 108 | + .collect(); |
| 109 | + |
| 110 | + let Shared = Arc::new(Shared { |
| 111 | + Injector:(Injector::new(), Injector::new(), Injector::new()), |
| 112 | + |
| 113 | + Stealer:(StealerHigh, StealerNormal, StealerLow), |
| 114 | + }); |
| 115 | + |
| 116 | + let mut Context = Vec::with_capacity(Count); |
| 117 | + |
| 118 | + for Id in 0..Count { |
| 119 | + // We use remove(0) because we built the Vecs in order and need to consume them. |
| 120 | + Context.push(WorkerContext { |
| 121 | + Id, |
| 122 | + |
| 123 | + Local:(High.remove(0), Normal.remove(0), Low.remove(0)), |
| 124 | + |
| 125 | + Shared:Shared.clone(), |
| 126 | + }); |
57 | 127 | } |
| 128 | + |
| 129 | + let Queue = Self { Shared }; |
| 130 | + |
| 131 | + (Queue, Context) |
58 | 132 | } |
59 | 133 |
|
60 | | - /// Attempts to find a task for a given worker, always prioritizing |
61 | | - /// `High` > `Normal` > `Low`. |
62 | | - pub fn StealForWorker(&self, WorkerId:usize) -> Option<Struct> { |
63 | | - self.FindTaskInSet(&self.High, WorkerId) |
64 | | - .or_else(|| self.FindTaskInSet(&self.Normal, WorkerId)) |
65 | | - .or_else(|| self.FindTaskInSet(&self.Low, WorkerId)) |
| 134 | + /// Submits a new task to the queue. |
| 135 | + /// This is thread-safe and can be called from anywhere. |
| 136 | + pub fn Submit(&self, Task:T) { |
| 137 | + match Task.GetPriority() { |
| 138 | + Priority::High => self.Shared.Injector.0.push(Task), |
| 139 | + |
| 140 | + Priority::Normal => self.Shared.Injector.1.push(Task), |
| 141 | + |
| 142 | + Priority::Low => self.Shared.Injector.2.push(Task), |
| 143 | + } |
66 | 144 | } |
| 145 | +} |
67 | 146 |
|
68 | | - /// Implements the core work-finding logic for a specific priority level. |
69 | | - /// It first checks the worker's local queue, then attempts to steal. |
70 | | - fn FindTaskInSet(&self, Set:&PriorityQueueSet, WorkerId:usize) -> Option<Struct> { |
71 | | - Set.WorkerQueue[WorkerId] |
72 | | - .pop() |
73 | | - .or_else(|| self.StealFromSetGlobalOrPeer(Set, WorkerId)) |
| 147 | +impl<T> WorkerContext<T> { |
| 148 | + /// Finds the next available task for this worker. |
| 149 | + /// Implements the full priority-aware, work-stealing logic. |
| 150 | + pub fn NextTask(&self) -> Option<T> { |
| 151 | + // Pop from local High |
| 152 | + self.Local.0.pop() |
| 153 | + // Pop from local Normal |
| 154 | + .or_else(|| self.Local.1.pop()) |
| 155 | + // Pop from local Low |
| 156 | + .or_else(|| self.Local.2.pop()) |
| 157 | + // Steal High |
| 158 | + .or_else(|| self.Steal(&self.Shared.Injector.0, &self.Shared.Stealer.0, &self.Local.0)) |
| 159 | + // Steal Normal |
| 160 | + .or_else(|| self.Steal(&self.Shared.Injector.1, &self.Shared.Stealer.1, &self.Local.1)) |
| 161 | + // Steal Low |
| 162 | + .or_else(|| self.Steal(&self.Shared.Injector.2, &self.Shared.Stealer.2, &self.Local.2)) |
74 | 163 | } |
75 | 164 |
|
76 | | - /// Attempts to steal work for a specific priority level, first from the |
77 | | - /// global queue, then from peer workers. |
78 | | - fn StealFromSetGlobalOrPeer(&self, Set:&PriorityQueueSet, WorkerId:usize) -> Option<Struct> { |
79 | | - // Try stealing from the global injector for this priority set. |
80 | | - if Set.GlobalInjector.steal_batch_and_pop(&Set.WorkerQueue[WorkerId]).is_success() { |
81 | | - return Set.WorkerQueue[WorkerId].pop(); |
| 165 | + fn Steal<'a>(&self, Injector:&'a Injector<T>, Stealer:&'a [Stealer<T>], Local:&'a WorkerDeque<T>) -> Option<T> { |
| 166 | + if Injector.steal_batch_and_pop(Local).is_success() { |
| 167 | + return Local.pop(); |
82 | 168 | } |
83 | 169 |
|
84 | | - // Try stealing from peers for this priority set. We shuffle the indices |
85 | | - // to ensure fairness and avoid contention hotspots. |
86 | | - let mut ShuffledIndex:Vec<usize> = (0..Set.Stealer.len()).collect(); |
87 | | - ShuffledIndex.shuffle(&mut rand::rng()); |
| 170 | + let mut Index:Vec<usize> = (0..Stealer.len()).collect(); |
88 | 171 |
|
89 | | - for Index in ShuffledIndex { |
90 | | - if Index == WorkerId { |
91 | | - continue; // Don't steal from ourselves. |
| 172 | + Index.shuffle(&mut rand::rng()); |
| 173 | + |
| 174 | + for i in Index { |
| 175 | + if i == self.Id { |
| 176 | + continue; |
92 | 177 | } |
93 | | - if Set.Stealer[Index].steal_batch_and_pop(&Set.WorkerQueue[WorkerId]).is_success() { |
94 | | - return Set.WorkerQueue[WorkerId].pop(); |
| 178 | + |
| 179 | + if Stealer[i].steal_batch_and_pop(Local).is_success() { |
| 180 | + return Local.pop(); |
95 | 181 | } |
96 | 182 | } |
97 | 183 |
|
|
0 commit comments