use crate::ids::IdOf;
use crate::lite_runtime::TaskAddress;
use std::collections::HashMap;
#[derive(Debug)]
pub struct TaskDistributor<T> {
tasks: HashMap<IdOf<T>, TaskAddress<T>>,
}
impl<T> Default for TaskDistributor<T> {
fn default() -> Self {
Self {
tasks: HashMap::new(),
}
}
}
impl<T> TaskDistributor<T> {
pub fn new() -> Self {
Self::default()
}
}
impl<T> TaskDistributor<T> {
pub fn insert(&mut self, addr: TaskAddress<T>) {
self.tasks.insert(addr.id(), addr);
}
pub fn get(&mut self, id: &IdOf<T>) -> Option<&TaskAddress<T>> {
self.tasks.get(id)
}
pub fn remove(&mut self, id: &IdOf<T>) -> Option<TaskAddress<T>> {
self.tasks.remove(id)
}
pub fn stop_all(&self) {
for addr in self.tasks.values() {
if let Err(err) = addr.stop() {
log::error!("Can't stop the task {:?}: {}", addr.id(), err);
}
}
}
pub fn len(&self) -> usize {
self.tasks.len()
}
pub fn is_empty(&self) -> bool {
self.tasks.is_empty()
}
}