1use std::collections::VecDeque;
2use std::sync::{Condvar, Mutex};
3
4#[derive(Debug)]
5pub struct Mpmc<T> {
6 q: Mutex<VecDeque<T>>,
7 cv: Condvar,
8}
9
10impl<T> Default for Mpmc<T> {
11 fn default() -> Self {
12 Self::new()
13 }
14}
15
16impl<T> Mpmc<T> {
17 pub fn new() -> Mpmc<T> {
18 Mpmc {
19 q: Mutex::default(),
20 cv: Condvar::new(),
21 }
22 }
23
24 pub fn send(&self, t: T) {
25 let mut q = self.q.lock().unwrap();
26 q.push_back(t);
27 drop(q);
28 self.cv.notify_one();
29 }
30
31 pub fn recv(&self) -> T {
32 let mut q = self.q.lock().unwrap();
33
34 while q.is_empty() {
35 q = self.cv.wait(q).unwrap();
36 }
37
38 q.pop_front().unwrap()
39 }
40}