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
use std::collections::VecDeque;
use std::sync::{Arc, Mutex, MutexGuard};
#[derive(Clone)]
pub struct WorkQueue<T: Send + Clone + PartialEq> {
pub inner: Arc<Mutex<VecDeque<T>>>,
}
impl<T: Send + Clone + PartialEq> PartialEq for WorkQueue<T> {
fn eq(&self, other: &Self) -> bool {
let this_inner = self.inner.lock().unwrap();
let other_inner = other.inner.lock().unwrap();
*this_inner == *other_inner
}
}
impl<T: Send + PartialEq + Clone> WorkQueue<T> {
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(VecDeque::new())),
}
}
fn aquire(&self) -> MutexGuard<'_, VecDeque<T>> {
if let Ok(q) = self.inner.lock() {
q
} else {
panic!("WorkQueue::get_work() tried to lock a poisoned mutex")
}
}
pub fn get_work(&self) -> Option<T> {
self.aquire().pop_front()
}
pub fn add_work(&self, work: T) -> usize {
let mut q = self.aquire();
if !q.contains(&work) {
q.push_back(work);
}
q.len()
}
pub fn retain<F>(&self, f: F) -> VecDeque<T>
where
F: for<'a> FnMut(&'a T) -> bool,
{
let mut q = self.aquire().clone();
q.retain(f);
q
}
}