Skip to main content

xmtp_mls_core/
mutex_registry.rs

1use std::{collections::HashMap, sync::Arc};
2
3use tokio::sync::Mutex;
4use xmtp_proto::types::GroupId;
5
6/// A registry of mutexes that can be locked and unlocked by a given key.
7#[derive(Debug, Clone, Default)]
8pub struct MutexRegistry {
9    mutexes: HashMap<GroupId, Arc<Mutex<()>>>,
10}
11
12impl MutexRegistry {
13    pub fn new() -> Self {
14        Self {
15            mutexes: HashMap::new(),
16        }
17    }
18
19    pub fn get_mutex(&mut self, key: GroupId) -> Arc<Mutex<()>> {
20        self.mutexes
21            .entry(key)
22            .or_insert_with(|| Arc::new(Mutex::new(())))
23            .clone()
24    }
25}