Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Garrisi committed Sep 23, 2024
1 parent 231c179 commit bd9e5cb
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 106 deletions.
23 changes: 13 additions & 10 deletions examples/carwash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ fn car_process<'a>(
// Generate random drive_time and wash_time at beginning
let t_drive = distr_drive.sample(rng);
let t_wash = distr_wash.sample(rng);
Box::new(#[coroutine] move |_| {
// The car drives for `t_drive` time
yield Drive(t_drive);
// Arrives at carwash and waits for a machine to be free
yield WaitMachine(carwash);
// The car wash for `t_wash` time, keeping the carwash machine (resource) occupied
yield Wash(t_wash);
// The car leaves the carwash, freeing the resource
yield Leave(carwash);
})
Box::new(
#[coroutine]
move |_| {
// The car drives for `t_drive` time
yield Drive(t_drive);
// Arrives at carwash and waits for a machine to be free
yield WaitMachine(carwash);
// The car wash for `t_wash` time, keeping the carwash machine (resource) occupied
yield Wash(t_wash);
// The car leaves the carwash, freeing the resource
yield Leave(carwash);
},
)
}

fn main() {
Expand Down
45 changes: 24 additions & 21 deletions examples/monitoring-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,31 @@ impl PCBStateCtx {
}

fn process_code(r: Resources) -> Box<Process<PCBState>> {
Box::new(#[coroutine] move |_| {
let mut current_pcb_id = 0;
let mut ctx = PCBStateCtx::new(r);
loop {
// PCB first processing stage
yield ctx.need_pip();
yield ctx.pip_work();
yield ctx.free_pip(PCBStage::SurfaceMountPlaced);
// requeue PCB for second processing stage
yield ctx.need_pip();
yield ctx.pip_work();
yield ctx.free_pip(PCBStage::ThruHolePlaced);
// queue for electrical testing
yield ctx.need_et();
yield ctx.et_work();
yield ctx.free_et(PCBStage::ElectricalTestPerformed);
yield ctx.mark(PCBStage::Done);
Box::new(
#[coroutine]
move |_| {
let mut current_pcb_id = 0;
let mut ctx = PCBStateCtx::new(r);
loop {
// PCB first processing stage
yield ctx.need_pip();
yield ctx.pip_work();
yield ctx.free_pip(PCBStage::SurfaceMountPlaced);
// requeue PCB for second processing stage
yield ctx.need_pip();
yield ctx.pip_work();
yield ctx.free_pip(PCBStage::ThruHolePlaced);
// queue for electrical testing
yield ctx.need_et();
yield ctx.et_work();
yield ctx.free_et(PCBStage::ElectricalTestPerformed);
yield ctx.mark(PCBStage::Done);

current_pcb_id += 1;
ctx.set_new_id(current_pcb_id);
}
})
current_pcb_id += 1;
ctx.set_new_id(current_pcb_id);
}
},
)
}

fn main() {
Expand Down
48 changes: 27 additions & 21 deletions examples/one_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,33 @@ use desim::{Effect, EndCondition, Simulation};
fn main() {
let mut s = Simulation::new();
let cpu = s.create_resource(Box::new(SimpleResource::new(1)));
let p1 = s.create_process(Box::new(#[coroutine] move |_| {
for _ in 0..10 {
// wait for the cpu to be available
yield Effect::Request(cpu);
// do some job that requires a fixed amount of 5 time units
yield Effect::TimeOut(5.0);
// release the CPU
yield Effect::Release(cpu);
}
}));
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
let mut rng = Rng::from_entropy();
loop {
// wait for the CPU
yield Effect::Request(cpu);
// do some job for a random amount of time units between 0 and 10
yield Effect::TimeOut((rng.next_u32() % 10) as f64);
// release the CPU
yield Effect::Release(cpu);
}
}));
let p1 = s.create_process(Box::new(
#[coroutine]
move |_| {
for _ in 0..10 {
// wait for the cpu to be available
yield Effect::Request(cpu);
// do some job that requires a fixed amount of 5 time units
yield Effect::TimeOut(5.0);
// release the CPU
yield Effect::Release(cpu);
}
},
));
let p2 = s.create_process(Box::new(
#[coroutine]
move |_| {
let mut rng = Rng::from_entropy();
loop {
// wait for the CPU
yield Effect::Request(cpu);
// do some job for a random amount of time units between 0 and 10
yield Effect::TimeOut((rng.next_u32() % 10) as f64);
// release the CPU
yield Effect::Release(cpu);
}
},
));
// let p1 to start immediately...
s.schedule_event(0.0, p1, Effect::TimeOut(0.));
// ...and p2 after 17 time units
Expand Down
44 changes: 25 additions & 19 deletions examples/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,31 @@ impl SimState for MyState {
fn main() {
let mut s = Simulation::new();
let queue = s.create_store(Box::new(SimpleStore::new(1)));
let p1 = s.create_process(Box::new(#[coroutine] move |_| {
for i in 0..10 {
// wait for the cpu to be available
yield MyState::Push(queue, i);
// do some job that requires a fixed amount of 5 time units
// release the CPU
yield MyState::Wait(10.0);
}
}));
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
for _ in 0..10 {
// wait for the CPU
let ret = yield MyState::Pull(queue);
println!("ret: {:?}", ret);
// do some job for a random amount of time units between 0 and 10
// yield MyState::Wait(10.0);
// release the CPU
}
}));
let p1 = s.create_process(Box::new(
#[coroutine]
move |_| {
for i in 0..10 {
// wait for the cpu to be available
yield MyState::Push(queue, i);
// do some job that requires a fixed amount of 5 time units
// release the CPU
yield MyState::Wait(10.0);
}
},
));
let p2 = s.create_process(Box::new(
#[coroutine]
move |_| {
for _ in 0..10 {
// wait for the CPU
let ret = yield MyState::Pull(queue);
println!("ret: {:?}", ret);
// do some job for a random amount of time units between 0 and 10
// yield MyState::Wait(10.0);
// release the CPU
}
},
));
// let p1 to start immediately...
s.schedule_event(0.0, p1, MyState::default());
// ...and p2 after 17 time units
Expand Down
88 changes: 53 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,17 @@ mod tests {
use crate::{Effect, Simulation};

let mut s = Simulation::new();
let p = s.create_process(Box::new(#[coroutine] |_| {
let mut a = 0.0;
loop {
a += 1.0;

yield Effect::TimeOut(a);
}
}));
let p = s.create_process(Box::new(
#[coroutine]
|_| {
let mut a = 0.0;
loop {
a += 1.0;

yield Effect::TimeOut(a);
}
},
));
s.schedule_event(0.0, p, Effect::TimeOut(0.));
s.step();
s.step();
Expand All @@ -553,13 +556,16 @@ mod tests {
use crate::{Effect, EndCondition, Simulation};

let mut s = Simulation::new();
let p = s.create_process(Box::new(#[coroutine] |_| {
let tik = 0.7;
loop {
println!("tik");
yield Effect::TimeOut(tik);
}
}));
let p = s.create_process(Box::new(
#[coroutine]
|_| {
let tik = 0.7;
loop {
println!("tik");
yield Effect::TimeOut(tik);
}
},
));
s.schedule_event(0.0, p, Effect::TimeOut(0.));
let s = s.run(EndCondition::Time(10.0));
println!("{}", s.time());
Expand All @@ -575,17 +581,23 @@ mod tests {
let r = s.create_resource(Box::new(SimpleResource::new(1)));

// simple process that lock the resource for 7 time units
let p1 = s.create_process(Box::new(#[coroutine] move |_| {
yield Effect::Request(r);
yield Effect::TimeOut(7.0);
yield Effect::Release(r);
}));
let p1 = s.create_process(Box::new(
#[coroutine]
move |_| {
yield Effect::Request(r);
yield Effect::TimeOut(7.0);
yield Effect::Release(r);
},
));
// simple process that holds the resource for 3 time units
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
yield Effect::Request(r);
yield Effect::TimeOut(3.0);
yield Effect::Release(r);
}));
let p2 = s.create_process(Box::new(
#[coroutine]
move |_| {
yield Effect::Request(r);
yield Effect::TimeOut(3.0);
yield Effect::Release(r);
},
));

// let p1 start immediately...
s.schedule_event(0.0, p1, Effect::TimeOut(0.));
Expand All @@ -608,17 +620,23 @@ mod tests {
let store = sim.create_store(Box::new(SimpleStore::new(1)));

// simple process that pulls out of the store immediately and after 7 time units
let p1 = sim.create_process(Box::new(#[coroutine] move |_| {
yield Effect::Pull(store);
yield Effect::TimeOut(7.0);
yield Effect::Pull(store);
}));
let p1 = sim.create_process(Box::new(
#[coroutine]
move |_| {
yield Effect::Pull(store);
yield Effect::TimeOut(7.0);
yield Effect::Pull(store);
},
));
// simple process that pushes into the store immediately and after 3 time units
let p2 = sim.create_process(Box::new(#[coroutine] move |_| {
yield Effect::Push(store);
yield Effect::TimeOut(3.0);
yield Effect::Push(store);
}));
let p2 = sim.create_process(Box::new(
#[coroutine]
move |_| {
yield Effect::Push(store);
yield Effect::TimeOut(3.0);
yield Effect::Push(store);
},
));

// let p1 start immediately...
sim.schedule_event(0.0, p1, Effect::TimeOut(0.));
Expand Down

0 comments on commit bd9e5cb

Please sign in to comment.