rust_rsm/common/
tsqueue.rs1#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(non_upper_case_globals)]
5#![allow(dead_code)]
6
7use std::collections::vec_deque::{Iter, IterMut};
8use std::collections::VecDeque;
9use std::sync::{Mutex,Arc};
10use std::sync::Condvar;
11pub struct TsDequeue<T> {
14 inner: Mutex<VecDeque<T>>,
15 cond:Condvar,
16 has_data:Arc<Mutex<bool>>,
17}
18impl<T> TsDequeue<T> {
20 pub fn new(capacity:usize) -> Self {
21 let queue: VecDeque<T> = VecDeque::with_capacity(capacity);
22 return Self {
23 inner: Mutex::new(queue),
24 cond:Condvar::new(),
25 has_data:Arc::new(Mutex::new(false)),
26 };
27 }
28
29 pub fn push_back(&mut self, v: T) {
30 let mut inner = self.inner.lock().unwrap();
31 inner.push_back(v);
32 }
36 pub fn push_front(&mut self, v: T) {
37 let mut inner = self.inner.lock().unwrap();
38 inner.push_front(v);
39 }
40
41 pub fn pop_back(&mut self) -> Option<T> {
42 self.inner.lock().unwrap().pop_back()
43
44 }
45 pub fn pop_front(&mut self) -> Option<T> {
46 self.inner.lock().unwrap().pop_front()
47 }
48
49 pub fn iter(&self) -> Iter<T> {
50 let l = self.inner.lock().unwrap();
51 let inner = &*l as *const VecDeque<T>;
52 return unsafe { (*inner).iter() };
53
54 }
55
56 pub fn iter_mut(&mut self) -> IterMut<T> {
57 let mut l = self.inner.lock().unwrap();
58 let inner = &mut *l as *mut VecDeque<T>;
59 return unsafe { (*inner).iter_mut() };
60 }
61
62 pub fn len(&self)->usize {
63 return self.inner.lock().unwrap().len();
64 }
65
66 pub fn capacity(&self)->usize {
67 self.inner.lock().unwrap().capacity()
68 }
69 pub fn notify(&self) {
70
71 let mut has_data = self.has_data.lock().unwrap();
72 *has_data = true;
73 self.cond.notify_all();
74 }
75 pub fn wait(&self) {
76 let mut l = self.has_data.lock().unwrap();
77 while !(*l) {
78 l = self.cond.wait(l).unwrap();
79 }
80 *l=false;
81 }
82}