diff --git a/benches/barbershop.rs b/benches/barbershop.rs index 9d239f801d0b5830e9b02277fa7d1a7c5894e26e..01e7b8c162b504c0915a25e7c5edfda8e582bba5 100644 --- a/benches/barbershop.rs +++ b/benches/barbershop.rs @@ -62,18 +62,18 @@ impl BarberShop { sim.activate(async move { let dist = Uniform::new(12.0, 24.0); - // wait some time before activating the first customer - sim.advance(rng_a.sample(dist)).await; - - // generate new customers until the store closes officially - while sim.now() < stop_time { + loop { + // wait some time before activating the next customer + sim.advance(rng_a.sample(dist)).await; + + // generate new customers until the store closes officially + if sim.now() >= stop_time { return; } + // activate the next customer sim.activate(Customer { joe: joe.clone(), rng: SmallRng::from_seed(rng_s.gen()) }.actions(sim)); - // wait some time before activating the next customer - sim.advance(rng_a.sample(dist)).await; } }); diff --git a/src/bin/barbershop.rs b/src/bin/barbershop.rs index d90e2eaedd76082b6e66e09717d74328bdf53e02..23960ae3ddaf82815d77acd7d740ab29450ceef4 100644 --- a/src/bin/barbershop.rs +++ b/src/bin/barbershop.rs @@ -6,48 +6,6 @@ const STOP_TIME: Time = 480000.0; const SEED_A : u64 = 100000; const SEED_S : u64 = 200000; -fn main() { - // pseudo random number generators referenced from within the main process - let mut rng_a = SmallRng::seed_from_u64(SEED_A); - let mut rng_s = SmallRng::seed_from_u64(SEED_S); - - // the lifetimes are automatically extended until the end of the scope - let joe = &Facility::new(); - let rv = &RandomVar::new(); - - // the main process - simulation(|sim| Process::new(async move { - // activate a process to generate the customers - sim.activate(async move { - let dist = Uniform::new(12.0, 24.0); - - // wait some time before activating the first customer - sim.advance(rng_a.sample(dist)).await; - - // generate new customers until the store closes officially - while sim.now() < STOP_TIME { - // activate the next customer - sim.activate(Customer { - joe: joe.clone(), - rv: rv.clone(), - rng: SmallRng::from_seed(rng_s.gen()) - }.actions(sim)); - - // wait some time before activating the next customer - sim.advance(rng_a.sample(dist)).await; - } - }); - - // wait until the store closes - sim.advance(STOP_TIME).await; - - // finish processing the queue (no more customers arrive) - joe.seize().await; - })); - - println!("Stats: {:.3}", rv); -} - /// Customer process with access to the barber and a random number generator. struct Customer<'c> { joe: &'c Facility, rv: &'c RandomVar, rng: SmallRng } @@ -64,3 +22,40 @@ impl<'c> Customer<'c> { self.joe.release(); } } + +async fn sim_main<'m>(sim: SimContext<'m>, joe: &'m Facility, rv: &'m RandomVar) { + // pseudo random number generators referenced from within the main process + let mut rng_a = SmallRng::seed_from_u64(SEED_A); + let mut rng_s = SmallRng::seed_from_u64(SEED_S); + + // activate a process to generate the customers + sim.activate(async move { + let dist = Uniform::new(12.0, 24.0); + loop { + sim.advance(rng_a.sample(dist)).await; + if sim.now() >= STOP_TIME { return; } + sim.activate(Customer { + joe: joe.clone(), + rv: rv.clone(), + rng: SmallRng::from_seed(rng_s.gen()) + }.actions(sim)); + } + }); + + // wait until the store closes + sim.advance(STOP_TIME).await; + + // finish processing the queue (no more customers arrive) + joe.seize().await; +} + +fn main() { + // the lifetimes are automatically extended until the end of the scope + let joe = &Facility::new(); + let rv = &RandomVar::new(); + + // the main process + simulation(|sim| Process::new(sim_main(sim, joe, rv))); + + println!("Stats: {:.3}", rv); +}