diff --git a/tidepool/src/main.rs b/tidepool/src/main.rs index fbc3c5d..ead801d 100644 --- a/tidepool/src/main.rs +++ b/tidepool/src/main.rs @@ -61,9 +61,11 @@ fn single(args: cli::SingleRun) -> Result<()> { let (tx, rx) = create_mailbox(); let exit_early = Arc::new(atomic::AtomicBool::new(false)); - let exit_early2 = exit_early.clone(); - let config2 = config.clone(); - std::thread::spawn(move || run_simulation(&args.data, config2, args.seed, 0, tx, exit_early2)); + std::thread::spawn({ + let config = config.clone(); + let exit_early = exit_early.clone(); + move || run_simulation(&args.data, config, args.seed, 0, &tx, &exit_early) + }); while let Ok(msg) = rx.recv() { tracing::trace!(msg = debug(&msg)); @@ -116,8 +118,8 @@ fn multi(args: cli::MultiRun) -> Result<()> { let config = parse_config_file(&args.config_file)?; let (tx, rx) = create_mailbox(); - let tasks = args.count.unwrap_or(u32::MAX); - let tasks = Arc::new(atomic::AtomicI64::new(tasks as i64)); + let tasks = args.count.map_or(i64::MAX, |n| n as i64); + let tasks = Arc::new(atomic::AtomicI64::new(tasks)); let exit_early = Arc::new(atomic::AtomicBool::new(false)); let n_jobs = args.jobs.unwrap_or_else(num_cpus::get); @@ -127,13 +129,13 @@ fn multi(args: cli::MultiRun) -> Result<()> { tracing::debug!("spawning {n_jobs} jobs"); for id in 0..n_jobs { jobs.insert(id, None); - let tx2 = tx.clone(); - let tasks2 = tasks.clone(); - let exit_early2 = exit_early.clone(); - let config2 = config.clone(); - let data_args = args.data.clone(); - std::thread::spawn(move || { - run_simulations(&data_args, config2, id, tx2, tasks2, exit_early2); + std::thread::spawn({ + let data_args = args.data.clone(); + let config = config.clone(); + let tx = tx.clone(); + let tasks = tasks.clone(); + let exit_early = exit_early.clone(); + move || run_simulations(&data_args, config, id, &tx, &tasks, &exit_early) }); } @@ -335,8 +337,8 @@ fn run_simulation( config: config::Config, seed: Option, id: usize, - tx: mpsc::SyncSender, - exit_early: Arc, + tx: &mpsc::SyncSender, + exit_early: &atomic::AtomicBool, ) { let mut moves = data_args.list_moves.then(Vec::new); let seed = seed.unwrap_or_else(|| rand::thread_rng().next_u64()); @@ -415,20 +417,16 @@ fn run_simulations( data_args: &cli::OutputDataArgs, config: config::Config, id: usize, - tx: mpsc::SyncSender, - tasks: Arc, - exit_early: Arc, + tx: &mpsc::SyncSender, + tasks: &atomic::AtomicI64, + exit_early: &atomic::AtomicBool, ) { while tasks.fetch_sub(1, atomic::Ordering::Relaxed) > 0 { if exit_early.load(atomic::Ordering::Relaxed) { break; } - let seed = None; - let config2 = config.clone(); - let tx2 = tx.clone(); - let exit_early2 = exit_early.clone(); - run_simulation(&data_args, config2, seed, id, tx2, exit_early2); + run_simulation(data_args, config.clone(), None, id, tx, exit_early); } if tx.send(Msg::Shutdown(id)).is_err() {