use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use bitvec::vec::BitVec;
use tokio::sync::Notify;
use tokio::sync::futures::Notified;
const NOTIFY_BUCKETS: usize = 32;
#[derive(Debug)]
pub struct Notifications {
buckets: Vec<Notify>,
}
impl Default for Notifications {
fn default() -> Self {
Self {
buckets: (0..NOTIFY_BUCKETS).map(|_| Notify::new()).collect(),
}
}
}
fn slot_index_for_hash(hash_value: u64) -> usize {
(hash_value % (NOTIFY_BUCKETS as u64)) as usize
}
fn slot_index_for_key<K: Hash>(key: K) -> usize {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
let hash_value = hasher.finish();
slot_index_for_hash(hash_value)
}
impl Notifications {
pub fn new() -> Self {
Self::default()
}
pub fn register<K>(&'_ self, key: K) -> Notified<'_>
where
K: Hash,
{
self.buckets[slot_index_for_key(key)].notified()
}
pub fn notify<K>(&self, key: K)
where
K: Hash,
{
self.buckets[slot_index_for_key(key)].notify_waiters();
}
pub fn submit_queue(&self, queue: &NotifyQueue) {
for bucket in queue.buckets.iter_ones() {
self.buckets[bucket].notify_waiters();
}
}
}
#[derive(Debug)]
pub struct NotifyQueue {
buckets: BitVec,
}
impl Default for NotifyQueue {
fn default() -> Self {
Self {
buckets: BitVec::repeat(false, NOTIFY_BUCKETS),
}
}
}
impl NotifyQueue {
pub fn new() -> Self {
Self::default()
}
pub fn add<K>(&mut self, key: &K)
where
K: Hash + ?Sized,
{
self.buckets.set(slot_index_for_key(key), true);
}
}
#[cfg(test)]
mod tests;