use crate::diagnostics::{Error, Result, error::helpers};
use super::{Mutex, RwLock, SemaphoreSync, CondVar, AtomicCounter, AtomicFlag};
use std::sync::{Arc, Mutex as StdMutex};
use std::collections::HashMap;
#[derive(Debug)]
pub struct SyncRegistry {
mutexes: StdMutex<HashMap<String, Mutex>>,
rwlocks: StdMutex<HashMap<String, RwLock>>,
semaphores: StdMutex<HashMap<String, SemaphoreSync>>,
condvars: StdMutex<HashMap<String, CondVar>>,
counters: StdMutex<HashMap<String, AtomicCounter>>,
flags: StdMutex<HashMap<String, AtomicFlag>>,
}
impl SyncRegistry {
pub fn new() -> Self {
Self {
mutexes: StdMutex::new(HashMap::new()),
rwlocks: StdMutex::new(HashMap::new()),
semaphores: StdMutex::new(HashMap::new()),
condvars: StdMutex::new(HashMap::new()),
counters: StdMutex::new(HashMap::new()),
flags: StdMutex::new(HashMap::new()),
}
}
pub fn register_mutex(&self, name: String, mutex: Mutex) -> Result<()> {
let mut mutexes = self.mutexes.lock()
.map_err(|_| Error::runtime_error("Failed to lock mutex registry".to_string(), None))?;
mutexes.insert(name, mutex);
Ok(())
}
pub fn get_mutex(&self, name: &str) -> Result<Mutex> {
let mutexes = self.mutexes.lock()
.map_err(|_| helpers::runtime_error_simple("Failed to lock mutex registry"))?;
mutexes.get(name)
.cloned()
.ok_or_else(|| helpers::runtime_error_simple(format!("Mutex '{name}' not found")))
}
}
impl Default for SyncRegistry {
fn default() -> Self {
Self::new()
}
}
static SYNC_REGISTRY: std::sync::OnceLock<Arc<SyncRegistry>> = std::sync::OnceLock::new();
pub fn global_sync_registry() -> Arc<SyncRegistry> {
SYNC_REGISTRY.get_or_init(|| Arc::new(SyncRegistry::new())).clone()
}