komora_sync/
priority_queue.rs1use std::collections::BinaryHeap;
2use std::sync::{Condvar, Mutex};
3
4use crate::Prioritized;
5
6#[derive(Debug)]
7pub struct PriorityQueue<T> {
8 q: Mutex<BinaryHeap<Prioritized<T>>>,
9 cv: Condvar,
10}
11
12impl<T> Default for PriorityQueue<T> {
13 fn default() -> Self {
14 Self::new()
15 }
16}
17
18impl<T> PriorityQueue<T> {
19 pub fn new() -> PriorityQueue<T> {
20 PriorityQueue {
21 q: Mutex::default(),
22 cv: Condvar::new(),
23 }
24 }
25
26 pub fn push(&self, t: T, priority: u64) {
41 let mut q = self.q.lock().unwrap();
42 q.push(Prioritized { t, priority });
43 drop(q);
44 self.cv.notify_one();
45 }
46
47 pub fn pop(&self) -> T {
48 let mut q = self.q.lock().unwrap();
49
50 while q.is_empty() {
51 q = self.cv.wait(q).unwrap();
52 }
53
54 q.pop().unwrap().t
55 }
56}