-
Notifications
You must be signed in to change notification settings - Fork 6
/
unlimited.rs
133 lines (124 loc) · 3.79 KB
/
unlimited.rs
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
//! Unlimited queue implementation
use std::convert::TryInto;
use std::fmt::Debug;
use std::iter::FromIterator;
use crossbeam_queue::SegQueue;
use tokio::sync::Semaphore;
use crate::atomic::Available;
use crate::{Notifier, Receiver};
/// Queue that is unlimited in size.
///
/// This queue implementation has the following characteristics:
///
/// - Based on `crossbeam_queue::SegQueue`
/// - Has unlimitied capacity and no back pressure on push
/// - Enabled via the `unlimited` feature in your `Cargo.toml`
pub struct Queue<T> {
queue: SegQueue<T>,
semaphore: Semaphore,
available: Available,
notifier_empty: Notifier,
}
impl<T> Queue<T> {
/// Create new empty queue
pub fn new() -> Self {
Self::default()
}
/// Get an item from the queue. If the queue is currently empty
/// this method blocks until an item is available.
pub async fn pop(&self) -> T {
let (txn, new_len) = self.available.sub();
let permit = self.semaphore.acquire().await.unwrap();
let item = self.queue.pop().unwrap();
txn.commit();
if new_len <= 0 {
self.notify_empty();
}
permit.forget();
item
}
/// Try to get an item from the queue. If the queue is currently
/// empty return None instead.
pub fn try_pop(&self) -> Option<T> {
let (txn, new_len) = self.available.sub();
let permit = self.semaphore.try_acquire().ok()?;
let item = self.queue.pop().unwrap();
txn.commit();
if new_len <= 0 {
self.notify_empty();
}
permit.forget();
Some(item)
}
/// Push an item into the queue
pub fn push(&self, item: T) {
self.queue.push(item);
self.semaphore.add_permits(1);
self.available.add();
}
/// Get current length of queue (number of items currently stored).
pub fn len(&self) -> usize {
self.queue.len()
}
/// Returns `true` if the queue is empty.
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
/// Get available count. This is the difference between the current
/// queue length and the number of tasks waiting for an item of the
/// queue.
pub fn available(&self) -> isize {
self.available.get()
}
/// Notify any callers awaiting empty()
fn notify_empty(&self) {
self.notifier_empty.send_replace(());
}
/// Await until the queue is empty.
pub async fn wait_empty(&self) {
if self.is_empty() {
return;
}
self.subscribe_empty().changed().await.unwrap();
}
/// Get a `Receiver` object that can repeatedly be awaited for
/// queue-empty notifications.
pub fn subscribe_empty(&self) -> Receiver {
self.notifier_empty.subscribe()
}
}
impl<T> Debug for Queue<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Queue")
.field("queue", &self.queue)
.field("semaphore", &self.semaphore)
.field("available", &self.available)
.field("empty", &self.notifier_empty)
.finish()
}
}
impl<T> Default for Queue<T> {
fn default() -> Self {
Self {
queue: SegQueue::new(),
semaphore: Semaphore::new(0),
available: Available::new(0),
notifier_empty: crate::new_notifier(),
}
}
}
impl<T> FromIterator<T> for Queue<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let queue = SegQueue::new();
for item in iter {
queue.push(item);
}
let size = queue.len();
Self {
queue,
semaphore: Semaphore::new(size),
available: Available::new(size.try_into().unwrap()),
..Self::default()
}
}
}