oximedia-core 0.1.1

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
//! Core synchronisation primitives for `OxiMedia`.
//!
//! This module offers lightweight, single-threaded simulation types for
//! synchronisation concepts, useful in non-`async` multimedia pipelines:
//!
//! - [`AtomicCounter`] – simple monotonic u64 counter
//! - [`Semaphore`] – counting semaphore
//! - [`SimRwLock`] – readers-writer lock simulation
//! - [`Barrier`] – cyclic barrier
//!
//! # Note
//!
//! These are **logical** (non-OS) implementations intended for testing and
//! single-threaded pipeline coordination.  For real multi-threaded use,
//! prefer `std::sync`.
//!
//! # Example
//!
//! ```
//! use oximedia_core::sync::{AtomicCounter, Semaphore, Barrier};
//!
//! let mut counter = AtomicCounter::new(0);
//! counter.increment();
//! assert_eq!(counter.get(), 1);
//!
//! let mut sem = Semaphore::new(3);
//! assert!(sem.acquire());
//! assert_eq!(sem.available(), 2);
//!
//! let mut barrier = Barrier::new(2);
//! assert!(!barrier.wait()); // first thread
//! assert!(barrier.wait());  // last thread – barrier released
//! ```

#![allow(dead_code)]

/// A simple, non-atomic counter backed by a plain `u64`.
///
/// Useful as a logical counter in single-threaded or test contexts.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtomicCounter {
    value: u64,
}

impl AtomicCounter {
    /// Creates a new counter initialised to `v`.
    #[must_use]
    pub fn new(v: u64) -> Self {
        Self { value: v }
    }

    /// Increments the counter by 1 and returns the **new** value.
    pub fn increment(&mut self) -> u64 {
        self.value = self.value.saturating_add(1);
        self.value
    }

    /// Decrements the counter by 1 (saturating at 0) and returns the **new** value.
    pub fn decrement(&mut self) -> u64 {
        self.value = self.value.saturating_sub(1);
        self.value
    }

    /// Returns the current value without modifying it.
    #[must_use]
    pub fn get(&self) -> u64 {
        self.value
    }

    /// Overwrites the counter value.
    pub fn set(&mut self, v: u64) {
        self.value = v;
    }
}

impl Default for AtomicCounter {
    fn default() -> Self {
        Self::new(0)
    }
}

/// A counting semaphore that tracks the number of available permits.
#[derive(Debug, Clone)]
pub struct Semaphore {
    count: i64,
    max: i64,
}

impl Semaphore {
    /// Creates a semaphore with `max` permits, all initially available.
    #[must_use]
    pub fn new(max: i64) -> Self {
        Self { count: max, max }
    }

    /// Attempts to acquire one permit.
    ///
    /// Returns `true` on success, `false` when no permits are available.
    pub fn acquire(&mut self) -> bool {
        if self.count > 0 {
            self.count -= 1;
            true
        } else {
            false
        }
    }

    /// Releases one permit back to the semaphore (up to `max`).
    ///
    /// Returns `true` if the permit was accepted, `false` when already full.
    pub fn release(&mut self) -> bool {
        if self.count < self.max {
            self.count += 1;
            true
        } else {
            false
        }
    }

    /// Number of permits currently available.
    #[must_use]
    pub fn available(&self) -> i64 {
        self.count
    }

    /// Maximum capacity of the semaphore.
    #[must_use]
    pub fn max(&self) -> i64 {
        self.max
    }
}

/// A simple readers-writer lock simulation.
///
/// Multiple concurrent readers are permitted; at most one writer is allowed,
/// and only when there are no active readers.
///
/// This is a **single-threaded** simulation (no OS primitives).
#[derive(Debug)]
pub struct SimRwLock<T> {
    data: T,
    readers: u32,
    writer: bool,
}

impl<T> SimRwLock<T> {
    /// Wraps `data` in a new `SimRwLock`.
    #[must_use]
    pub fn new(data: T) -> Self {
        Self {
            data,
            readers: 0,
            writer: false,
        }
    }

    /// Acquires a shared read reference.
    ///
    /// Returns `None` when a writer is active.
    pub fn read(&mut self) -> Option<&T> {
        if self.writer {
            return None;
        }
        self.readers += 1;
        Some(&self.data)
    }

    /// Releases one reader.
    pub fn release_read(&mut self) {
        if self.readers > 0 {
            self.readers -= 1;
        }
    }

    /// Acquires an exclusive write reference.
    ///
    /// Returns `None` when readers are active or a writer already holds the lock.
    pub fn write(&mut self) -> Option<&mut T> {
        if self.writer || self.readers > 0 {
            return None;
        }
        self.writer = true;
        Some(&mut self.data)
    }

    /// Alias for [`write`](Self::write) – tries to acquire write access without blocking.
    pub fn try_write(&mut self) -> Option<&mut T> {
        self.write()
    }

    /// Releases the write lock.
    pub fn release_write(&mut self) {
        self.writer = false;
    }

    /// Returns the number of active readers.
    #[must_use]
    pub fn reader_count(&self) -> u32 {
        self.readers
    }

    /// Returns `true` when a writer holds the lock.
    #[must_use]
    pub fn is_writing(&self) -> bool {
        self.writer
    }
}

/// A cyclic barrier that releases all waiters once `count` threads have called
/// [`wait`](Barrier::wait).
///
/// After the barrier fires, it automatically resets for the next cycle.
#[derive(Debug, Clone)]
pub struct Barrier {
    count: usize,
    waiting: usize,
}

impl Barrier {
    /// Creates a new barrier that triggers after `count` calls to [`wait`](Self::wait).
    #[must_use]
    pub fn new(count: usize) -> Self {
        Self { count, waiting: 0 }
    }

    /// Records one arrival at the barrier.
    ///
    /// Returns `true` for the **last** thread to arrive (the one that
    /// triggers the release), and `false` for all earlier arrivals.
    /// After the last arrival, the barrier is automatically reset.
    pub fn wait(&mut self) -> bool {
        self.waiting += 1;
        if self.waiting >= self.count {
            self.reset();
            true
        } else {
            false
        }
    }

    /// Resets the barrier so it can be used for the next cycle.
    pub fn reset(&mut self) {
        self.waiting = 0;
    }

    /// Number of threads currently waiting.
    #[must_use]
    pub fn waiting_count(&self) -> usize {
        self.waiting
    }

    /// The total count required to release the barrier.
    #[must_use]
    pub fn target_count(&self) -> usize {
        self.count
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    // 1. AtomicCounter::increment
    #[test]
    fn test_counter_increment() {
        let mut c = AtomicCounter::new(0);
        assert_eq!(c.increment(), 1);
        assert_eq!(c.increment(), 2);
    }

    // 2. AtomicCounter::decrement
    #[test]
    fn test_counter_decrement() {
        let mut c = AtomicCounter::new(5);
        assert_eq!(c.decrement(), 4);
        assert_eq!(c.decrement(), 3);
    }

    // 3. AtomicCounter::decrement saturates at 0
    #[test]
    fn test_counter_decrement_saturates() {
        let mut c = AtomicCounter::new(0);
        assert_eq!(c.decrement(), 0);
    }

    // 4. AtomicCounter::get and set
    #[test]
    fn test_counter_get_set() {
        let mut c = AtomicCounter::new(10);
        assert_eq!(c.get(), 10);
        c.set(42);
        assert_eq!(c.get(), 42);
    }

    // 5. Semaphore::acquire / release
    #[test]
    fn test_semaphore_acquire_release() {
        let mut sem = Semaphore::new(3);
        assert_eq!(sem.available(), 3);
        assert!(sem.acquire());
        assert_eq!(sem.available(), 2);
        assert!(sem.release());
        assert_eq!(sem.available(), 3);
    }

    // 6. Semaphore::acquire – blocks when exhausted
    #[test]
    fn test_semaphore_exhausted() {
        let mut sem = Semaphore::new(1);
        assert!(sem.acquire());
        assert!(!sem.acquire()); // No permits left
    }

    // 7. Semaphore::release – does not exceed max
    #[test]
    fn test_semaphore_release_at_max() {
        let mut sem = Semaphore::new(2);
        assert!(!sem.release()); // Already at max
        assert_eq!(sem.available(), 2);
    }

    // 8. Semaphore::max
    #[test]
    fn test_semaphore_max() {
        let sem = Semaphore::new(5);
        assert_eq!(sem.max(), 5);
    }

    // 9. SimRwLock – read while no writer
    #[test]
    fn test_rwlock_read_success() {
        let mut lock = SimRwLock::new(42u32);
        let val = lock.read().copied();
        assert_eq!(val, Some(42));
        assert_eq!(lock.reader_count(), 1);
        lock.release_read();
        assert_eq!(lock.reader_count(), 0);
    }

    // 10. SimRwLock – write while readers blocked
    #[test]
    fn test_rwlock_write_blocked_by_readers() {
        let mut lock = SimRwLock::new(0u32);
        let _ = lock.read();
        assert!(lock.write().is_none());
        lock.release_read();
        assert!(lock.write().is_some());
    }

    // 11. SimRwLock – read blocked by writer
    #[test]
    fn test_rwlock_read_blocked_by_writer() {
        let mut lock = SimRwLock::new(0u32);
        let _ = lock.write();
        assert!(lock.read().is_none());
        lock.release_write();
        assert!(lock.read().is_some());
    }

    // 12. SimRwLock – try_write
    #[test]
    fn test_rwlock_try_write() {
        let mut lock = SimRwLock::new(99u32);
        {
            let w = lock.try_write().expect("try_write should succeed");
            *w = 100;
        }
        lock.release_write();
        let v = lock.read().copied();
        assert_eq!(v, Some(100));
    }

    // 13. Barrier – fires on last thread
    #[test]
    fn test_barrier_fires() {
        let mut b = Barrier::new(3);
        assert!(!b.wait()); // 1st
        assert!(!b.wait()); // 2nd
        assert!(b.wait()); // 3rd – fires
                           // After firing, barrier is reset
        assert_eq!(b.waiting_count(), 0);
    }

    // 14. Barrier – cyclic reuse
    #[test]
    fn test_barrier_cyclic() {
        let mut b = Barrier::new(2);
        assert!(!b.wait());
        assert!(b.wait()); // fires & resets
                           // Second cycle
        assert!(!b.wait());
        assert!(b.wait()); // fires again
    }

    // 15. Barrier – target_count
    #[test]
    fn test_barrier_target_count() {
        let b = Barrier::new(7);
        assert_eq!(b.target_count(), 7);
    }
}