use std::{collections::BTreeSet, sync::Arc};
use crate::sync::*;
use crate::trace_lock;
pub struct Runtime {
running_components: Arc<Mutex<BTreeSet<String>>>,
}
impl Default for Runtime {
fn default() -> Self {
Self {
running_components: Arc::new(Mutex::new(BTreeSet::new())),
}
}
}
impl Runtime {
pub fn components(&self) -> Vec<String> {
let running_components = trace_lock!(self.running_components);
running_components.iter().cloned().collect()
}
pub fn register_component(&self, key: &str) {
debug!("registering component {}", key);
let mut running_components = trace_lock!(self.running_components);
if running_components.contains(key) {
trace!("Shouldn't be registering component {} more than once", key);
} else {
running_components.insert(key.to_string());
}
}
pub fn deregister_component(&self, key: &str) {
debug!("deregistering component {}", key);
let mut running_components = trace_lock!(self.running_components);
if !running_components.contains(key) {
trace!(
"Shouldn't be deregistering component {} which doesn't exist",
key
);
} else {
running_components.remove(key);
}
}
}