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
//! Lock-free queues for high-performance message passing.
//!
//! This module provides both unbounded and bounded lock-free queue
//! implementations using crossbeam's efficient queue structures.
use std::sync::Arc;
use crossbeam::queue::{ArrayQueue, SegQueue};
/// Lock-free queue for high-performance message passing.
#[derive(Debug, Clone)]
pub struct LockFreeQueue<T> {
inner: Arc<SegQueue<T>>,
}
impl<T> LockFreeQueue<T> {
/// Creates a new lock-free queue.
pub fn new() -> Self {
Self {
inner: Arc::new(SegQueue::new()),
}
}
/// Pushes a value to the queue.
pub fn push(&self, value: T) {
self.inner.push(value);
}
/// Pops a value from the queue.
pub fn pop(&self) -> Option<T> {
self.inner.pop()
}
/// Checks if the queue is empty.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Gets the approximate length of the queue.
pub fn len(&self) -> usize {
self.inner.len()
}
}
impl<T> Default for LockFreeQueue<T> {
fn default() -> Self {
Self::new()
}
}
/// Lock-free bounded queue with fixed capacity.
#[derive(Debug, Clone)]
pub struct BoundedLockFreeQueue<T> {
inner: Arc<ArrayQueue<T>>,
}
impl<T> BoundedLockFreeQueue<T> {
/// Creates a new bounded lock-free queue.
pub fn new(capacity: usize) -> Self {
Self {
inner: Arc::new(ArrayQueue::new(capacity)),
}
}
/// Pushes a value to the queue.
pub fn push(&self, value: T) -> std::result::Result<(), T> {
self.inner.push(value)
}
/// Pops a value from the queue.
pub fn pop(&self) -> Option<T> {
self.inner.pop()
}
/// Checks if the queue is empty.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Checks if the queue is full.
pub fn is_full(&self) -> bool {
self.inner.is_full()
}
/// Gets the current length of the queue.
pub fn len(&self) -> usize {
self.inner.len()
}
/// Gets the capacity of the queue.
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
}