komora_sync/
mpmc.rs

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