use std::sync::mpsc;
use std::thread;
use snafu::Snafu;
mod cell;
mod fitness;
mod parameters;
mod sim_result;
mod simulation;
pub use fitness::FitnessEvaluator;
pub use parameters::Parameters;
pub use sim_result::SimResult;
use simulation::Simulation;
#[derive(Debug, Snafu)]
pub enum SimulationError {
JoinError,
}
struct Handle {
pub thread_handle: thread::JoinHandle<()>,
pub stop_tx: mpsc::Sender<bool>,
}
pub struct ThreadSim {
handle: Handle,
rx: mpsc::Receiver<SimResult>,
}
impl ThreadSim {
pub fn start<T>(params: Parameters<T>) -> ThreadSim
where
T: 'static + FitnessEvaluator + Send + Sync,
{
let (result_tx, result_rx) = mpsc::channel();
let (stop_tx, stop_rx) = mpsc::channel();
let sim = Simulation::new(params);
let thread_handle =
thread::spawn(move || ThreadSim::internal_sim_loop(sim, stop_rx, result_tx));
ThreadSim {
handle: Handle {
stop_tx,
thread_handle,
},
rx: result_rx,
}
}
pub fn results(&self) -> &mpsc::Receiver<SimResult> {
&self.rx
}
pub fn stop(self) -> Result<(), SimulationError> {
self.handle
.stop_tx
.send(true)
.map_err(|_e| SimulationError::JoinError)?;
self.handle
.thread_handle
.join()
.map_err(|_e| SimulationError::JoinError)?;
Ok(())
}
fn internal_sim_loop<T: FitnessEvaluator + Send + Sync>(
mut sim: Simulation<T>,
stop_rx: mpsc::Receiver<bool>,
result_tx: mpsc::Sender<SimResult>,
) {
sim.run(result_tx, stop_rx)
}
}