Files
continuwuity/src/core/utils/mutex_map.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

123 lines
2.8 KiB
Rust
Raw Normal View History

use std::{fmt::Debug, hash::Hash, sync::Arc};
2024-06-14 21:11:31 +00:00
2024-07-09 20:04:43 +00:00
use tokio::sync::OwnedMutexGuard as Omg;
2024-06-14 21:11:31 +00:00
use crate::{Result, SyncMutex, err};
2024-06-14 21:11:31 +00:00
/// Map of Mutexes
pub struct MutexMap<Key, Val> {
map: Map<Key, Val>,
}
2024-07-09 20:04:43 +00:00
pub struct Guard<Key, Val> {
map: Map<Key, Val>,
val: Omg<Val>,
2024-06-14 21:11:31 +00:00
}
2024-07-09 20:04:43 +00:00
type Map<Key, Val> = Arc<MapMutex<Key, Val>>;
type MapMutex<Key, Val> = SyncMutex<HashMap<Key, Val>>;
2024-07-09 20:04:43 +00:00
type HashMap<Key, Val> = std::collections::HashMap<Key, Value<Val>>;
type Value<Val> = Arc<tokio::sync::Mutex<Val>>;
2024-06-14 21:11:31 +00:00
impl<Key, Val> MutexMap<Key, Val>
where
2025-01-02 05:30:51 +00:00
Key: Clone + Eq + Hash + Send,
Val: Default + Send,
2024-06-14 21:11:31 +00:00
{
#[must_use]
pub fn new() -> Self {
Self {
2024-07-09 20:04:43 +00:00
map: Map::new(MapMutex::new(HashMap::new())),
2024-06-14 21:11:31 +00:00
}
}
2025-01-02 05:30:51 +00:00
#[tracing::instrument(level = "trace", skip(self))]
pub async fn lock<'a, K>(&'a self, k: &'a K) -> Guard<Key, Val>
2024-06-14 21:11:31 +00:00
where
2025-01-02 05:30:51 +00:00
K: Debug + Send + ?Sized + Sync,
Key: TryFrom<&'a K>,
<Key as TryFrom<&'a K>>::Error: Debug,
2024-06-14 21:11:31 +00:00
{
let val = self
.map
.lock()
.entry(k.try_into().expect("failed to construct key"))
2024-06-14 21:11:31 +00:00
.or_default()
.clone();
2024-07-09 20:04:43 +00:00
Guard::<Key, Val> {
map: Arc::clone(&self.map),
val: val.lock_owned().await,
2024-06-14 21:11:31 +00:00
}
}
2024-07-09 20:04:43 +00:00
#[tracing::instrument(level = "trace", skip(self))]
pub fn try_lock<'a, K>(&self, k: &'a K) -> Result<Guard<Key, Val>>
where
K: Debug + Send + ?Sized + Sync,
Key: TryFrom<&'a K>,
<Key as TryFrom<&'a K>>::Error: Debug,
{
let val = self
.map
.lock()
.entry(k.try_into().expect("failed to construct key"))
.or_default()
.clone();
Ok(Guard::<Key, Val> {
map: Arc::clone(&self.map),
val: val.try_lock_owned().map_err(|_| err!("would yield"))?,
})
}
#[tracing::instrument(level = "trace", skip(self))]
pub fn try_try_lock<'a, K>(&self, k: &'a K) -> Result<Guard<Key, Val>>
where
K: Debug + Send + ?Sized + Sync,
Key: TryFrom<&'a K>,
<Key as TryFrom<&'a K>>::Error: Debug,
{
let val = self
.map
.try_lock()
.ok_or_else(|| err!("would block"))?
.entry(k.try_into().expect("failed to construct key"))
.or_default()
.clone();
Ok(Guard::<Key, Val> {
map: Arc::clone(&self.map),
val: val.try_lock_owned().map_err(|_| err!("would yield"))?,
})
}
2024-07-09 20:04:43 +00:00
#[must_use]
pub fn contains(&self, k: &Key) -> bool { self.map.lock().contains_key(k) }
2024-07-09 20:04:43 +00:00
#[must_use]
pub fn is_empty(&self) -> bool { self.map.lock().is_empty() }
2024-07-09 20:04:43 +00:00
#[must_use]
pub fn len(&self) -> usize { self.map.lock().len() }
2024-06-14 21:11:31 +00:00
}
impl<Key, Val> Default for MutexMap<Key, Val>
where
2025-01-02 05:30:51 +00:00
Key: Clone + Eq + Hash + Send,
Val: Default + Send,
2024-06-14 21:11:31 +00:00
{
fn default() -> Self { Self::new() }
}
2024-07-09 20:04:43 +00:00
impl<Key, Val> Drop for Guard<Key, Val> {
2025-01-02 05:30:51 +00:00
#[tracing::instrument(name = "unlock", level = "trace", skip_all)]
2024-07-09 20:04:43 +00:00
fn drop(&mut self) {
if Arc::strong_count(Omg::mutex(&self.val)) <= 2 {
self.map.lock().retain(|_, val| {
!Arc::ptr_eq(val, Omg::mutex(&self.val)) || Arc::strong_count(val) > 2
});
2024-07-09 20:04:43 +00:00
}
}
}