oximedia-core 0.1.8

Core types and traits for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Chase-Lev work-stealing work queue for multi-threaded media pipelines.
//!
//! This module exposes [`WorkQueue`] — a thread-safe, work-stealing task
//! distributor backed by [`crossbeam_deque`].  Each call to [`WorkQueue::new`]
//! creates one injector (global push point) and `workers` local steal handles.
//!
//! # Design
//!
//! ```text
//!   Producer         Injector          Worker 0 deque
//!   ────────►  push  ──────► steal ───►  pop / steal
//!//!                           Worker 1 ─────┘  (steals from Worker 0)
//! ```
//!
//! Tasks pushed via [`WorkQueue::push`] land in the global injector queue.
//! Worker threads call [`WorkQueue::steal`] which first drains the injector,
//! then falls back to stealing from sibling workers.  [`WorkQueue::len`]
//! returns an approximate total count.
//!
//! # Examples
//!
//! ```
//! use oximedia_core::work_queue_ws::WorkQueue;
//! use std::sync::Arc;
//! use std::sync::atomic::{AtomicUsize, Ordering};
//!
//! let wq: WorkQueue<u32> = WorkQueue::new(2);
//! for i in 0..10_u32 {
//!     wq.push(i);
//! }
//! // Any thread can steal.
//! let _item = wq.steal();
//! assert!(wq.len() <= 10);
//! ```

use crossbeam_deque::{Injector, Steal, Stealer, Worker};
use std::sync::{Arc, Mutex};

// ─────────────────────────────────────────────────────────────────────────────
// WorkQueue
// ─────────────────────────────────────────────────────────────────────────────

/// Inner shared state of a [`WorkQueue`].
struct Inner<T> {
    /// The global injection point; any thread may push here.
    injector: Injector<T>,
    /// One stealer handle per logical worker (cloned from the worker deques).
    stealers: Vec<Stealer<T>>,
    /// One worker deque per logical worker (protected behind a mutex so that
    /// steal() can borrow a deque without requiring the caller to own a slot).
    workers: Vec<Mutex<Worker<T>>>,
    /// Approximate item count (incremented on push, decremented on steal).
    len: std::sync::atomic::AtomicIsize,
}

/// A work-stealing work queue for distributing tasks across multiple workers.
///
/// `WorkQueue<T>` is `Clone` — all clones share the same underlying state,
/// so tasks pushed from one clone are visible to all others.
///
/// # Thread safety
///
/// `WorkQueue<T>` is `Send + Sync` when `T: Send`.  Multiple threads may
/// call [`push`](WorkQueue::push) and [`steal`](WorkQueue::steal)
/// concurrently without external synchronisation.
///
/// # Examples
///
/// ```
/// use oximedia_core::work_queue_ws::WorkQueue;
/// use std::thread;
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicUsize, Ordering};
///
/// let wq = WorkQueue::<u32>::new(4);
/// for i in 0..100_u32 {
///     wq.push(i);
/// }
///
/// let total = Arc::new(AtomicUsize::new(0));
/// let mut handles = Vec::new();
///
/// for _ in 0..4 {
///     let wq2 = wq.clone();
///     let count = Arc::clone(&total);
///     handles.push(thread::spawn(move || {
///         while let Some(_task) = wq2.steal() {
///             count.fetch_add(1, Ordering::Relaxed);
///         }
///     }));
/// }
/// for h in handles { h.join().expect("thread panicked"); }
/// assert_eq!(total.load(Ordering::Relaxed), 100);
/// ```
#[derive(Clone)]
pub struct WorkQueue<T: Send + 'static> {
    inner: Arc<Inner<T>>,
}

impl<T: Send + 'static> WorkQueue<T> {
    /// Creates a new `WorkQueue` with `workers` local deques.
    ///
    /// `workers` controls the number of distinct steal handles.  A value of
    /// `0` is clamped to `1`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::work_queue_ws::WorkQueue;
    ///
    /// let wq = WorkQueue::<i32>::new(4);
    /// assert_eq!(wq.len(), 0);
    /// ```
    #[must_use]
    pub fn new(workers: usize) -> Self {
        let num = workers.max(1);
        let injector = Injector::new();
        let mut worker_deques = Vec::with_capacity(num);
        let mut stealers = Vec::with_capacity(num);

        for _ in 0..num {
            let w: Worker<T> = Worker::new_fifo();
            stealers.push(w.stealer());
            worker_deques.push(Mutex::new(w));
        }

        Self {
            inner: Arc::new(Inner {
                injector,
                stealers,
                workers: worker_deques,
                len: std::sync::atomic::AtomicIsize::new(0),
            }),
        }
    }

    /// Pushes a task into the global injection queue.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::work_queue_ws::WorkQueue;
    ///
    /// let wq = WorkQueue::<u32>::new(2);
    /// wq.push(42_u32);
    /// assert_eq!(wq.len(), 1);
    /// ```
    pub fn push(&self, task: T) {
        self.inner.injector.push(task);
        self.inner
            .len
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }

    /// Attempts to steal a task from any available source.
    ///
    /// The implementation first drains the global injector into a local worker
    /// deque (slot 0), then tries to pop from each worker in round-robin order,
    /// retrying on contention.
    ///
    /// Returns `None` when all queues appear empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::work_queue_ws::WorkQueue;
    ///
    /// let wq = WorkQueue::<u32>::new(2);
    /// wq.push(1_u32);
    /// wq.push(2_u32);
    /// let t1 = wq.steal();
    /// let t2 = wq.steal();
    /// assert!(t1.is_some());
    /// assert!(t2.is_some());
    /// ```
    pub fn steal(&self) -> Option<T> {
        // Try draining the injector into worker 0 first.
        if let Ok(guard) = self.inner.workers[0].lock() {
            loop {
                match self.inner.injector.steal_batch_and_pop(&guard) {
                    Steal::Success(v) => {
                        self.inner
                            .len
                            .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
                        return Some(v);
                    }
                    Steal::Retry => continue,
                    Steal::Empty => break,
                }
            }
        }

        // Try popping from each worker deque in turn.
        for w_mutex in &self.inner.workers {
            if let Ok(guard) = w_mutex.lock() {
                if let Some(item) = guard.pop() {
                    self.inner
                        .len
                        .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
                    return Some(item);
                }
            }
        }

        // Fall back to stealing via stealer handles (cross-thread steal).
        for stealer in &self.inner.stealers {
            loop {
                match stealer.steal() {
                    Steal::Success(v) => {
                        self.inner
                            .len
                            .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
                        return Some(v);
                    }
                    Steal::Retry => continue,
                    Steal::Empty => break,
                }
            }
        }

        None
    }

    /// Returns the approximate number of tasks currently in the queue.
    ///
    /// This value may be slightly stale due to concurrent operations.  It
    /// saturates at zero rather than going negative.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::work_queue_ws::WorkQueue;
    ///
    /// let wq = WorkQueue::<u32>::new(2);
    /// wq.push(1_u32);
    /// wq.push(2_u32);
    /// assert_eq!(wq.len(), 2);
    /// ```
    #[must_use]
    pub fn len(&self) -> usize {
        let v = self.inner.len.load(std::sync::atomic::Ordering::Relaxed);
        v.max(0) as usize
    }

    /// Returns `true` if the queue appears empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::thread;

    // 1. Basic push and steal.
    #[test]
    fn push_and_steal_basic() {
        let wq = WorkQueue::<u32>::new(1);
        wq.push(10_u32);
        wq.push(20_u32);
        let a = wq.steal();
        let b = wq.steal();
        assert!(a.is_some());
        assert!(b.is_some());
        assert_eq!(wq.len(), 0);
    }

    // 2. Steal from empty returns None.
    #[test]
    fn steal_empty_returns_none() {
        let wq = WorkQueue::<u32>::new(2);
        assert!(wq.steal().is_none());
    }

    // 3. len tracks count.
    #[test]
    fn len_tracks_count() {
        let wq = WorkQueue::<u32>::new(2);
        assert_eq!(wq.len(), 0);
        wq.push(1_u32);
        assert_eq!(wq.len(), 1);
        wq.push(2_u32);
        assert_eq!(wq.len(), 2);
        wq.steal();
        assert_eq!(wq.len(), 1);
    }

    // 4. is_empty.
    #[test]
    fn is_empty_basic() {
        let wq = WorkQueue::<u32>::new(2);
        assert!(wq.is_empty());
        wq.push(1_u32);
        assert!(!wq.is_empty());
    }

    // 5. Clone shares state.
    #[test]
    fn clone_shares_state() {
        let wq = WorkQueue::<u32>::new(2);
        let wq2 = wq.clone();
        wq.push(99_u32);
        let stolen = wq2.steal();
        assert_eq!(stolen, Some(99_u32));
    }

    // 6. Multi-threaded stress test: 4 workers, 10 000 tasks.
    #[test]
    fn threaded_stress_10000_tasks() {
        const TASKS: u32 = 10_000;
        const WORKERS: usize = 4;

        let wq = WorkQueue::<u32>::new(WORKERS);
        for i in 0..TASKS {
            wq.push(i);
        }

        let stolen_count = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::with_capacity(WORKERS);

        for _ in 0..WORKERS {
            let wq_clone = wq.clone();
            let count = Arc::clone(&stolen_count);
            handles.push(thread::spawn(move || {
                let mut local = 0usize;
                // Keep trying until the queue is empty.
                let mut empty_streak = 0usize;
                loop {
                    match wq_clone.steal() {
                        Some(_) => {
                            local += 1;
                            empty_streak = 0;
                        }
                        None => {
                            empty_streak += 1;
                            // After many consecutive misses, assume queue is drained.
                            if empty_streak > 200 {
                                break;
                            }
                            std::hint::spin_loop();
                        }
                    }
                }
                count.fetch_add(local, Ordering::Relaxed);
            }));
        }

        for h in handles {
            h.join().expect("worker thread panicked");
        }

        let total = stolen_count.load(Ordering::Relaxed);
        assert_eq!(
            total, TASKS as usize,
            "expected all {TASKS} tasks to be consumed, got {total}"
        );
    }

    // 7. Push from multiple producers, steal from multiple consumers.
    #[test]
    fn multi_producer_multi_consumer() {
        const PER_PRODUCER: usize = 1_000;
        const PRODUCERS: usize = 4;
        const CONSUMERS: usize = 4;
        const TOTAL: usize = PER_PRODUCER * PRODUCERS;

        let wq = WorkQueue::<usize>::new(CONSUMERS);
        let consumed = Arc::new(AtomicUsize::new(0));

        // Spawn producers.
        let mut handles = Vec::new();
        for p in 0..PRODUCERS {
            let wq_p = wq.clone();
            handles.push(thread::spawn(move || {
                for i in 0..PER_PRODUCER {
                    wq_p.push(p * PER_PRODUCER + i);
                }
            }));
        }
        for h in handles {
            h.join().expect("producer panicked");
        }

        // Spawn consumers.
        let mut handles = Vec::new();
        for _ in 0..CONSUMERS {
            let wq_c = wq.clone();
            let cnt = Arc::clone(&consumed);
            handles.push(thread::spawn(move || {
                let mut miss = 0;
                loop {
                    match wq_c.steal() {
                        Some(_) => {
                            cnt.fetch_add(1, Ordering::Relaxed);
                            miss = 0;
                        }
                        None => {
                            miss += 1;
                            if miss > 500 {
                                break;
                            }
                        }
                    }
                }
            }));
        }
        for h in handles {
            h.join().expect("consumer panicked");
        }

        assert_eq!(consumed.load(Ordering::Relaxed), TOTAL);
    }
}