use super::{Name, Thread};
use crate::error::{FrameworkError, FrameworkErrorKind::ThreadError};
use std::collections::HashMap;
#[derive(Debug, Default)]
pub struct Manager {
threads: HashMap<Name, Thread>,
}
impl Manager {
pub fn spawn<F>(&mut self, name: &Name, f: F) -> Result<(), FrameworkError>
where
F: FnOnce() -> (),
F: Send + 'static,
{
if self.threads.contains_key(name) {
fail!(ThreadError, "duplicate name: {}", name);
}
let thread = Thread::spawn(name.clone(), f)?;
self.threads.insert(name.clone(), thread).unwrap();
Ok(())
}
pub fn join(&mut self) -> Result<(), FrameworkError> {
for thread in self.threads.values() {
thread.request_termination();
}
for (_, thread) in self.threads.drain() {
thread.join()?;
}
Ok(())
}
}