use lazy_static::lazy_static;
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, RwLock};
lazy_static! {
static ref LOCKS: Arc<RwLock<HashMap<String, Mutex<()>>>> =
Arc::new(RwLock::new(HashMap::new()));
}
pub fn serial_core(name: &str, function: fn()) {
let new_key = {
let unlock = LOCKS.read().unwrap();
!unlock.deref().contains_key(name)
};
if new_key {
LOCKS
.write()
.unwrap()
.deref_mut()
.insert(name.to_string(), Mutex::new(()));
}
let unlock = LOCKS.read().unwrap();
let _guard = unlock.deref()[name].lock();
function();
}