Skip to content

Commit

Permalink
Add #[coroutine]
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Garrisi committed Sep 23, 2024
1 parent 2d4c203 commit accf71f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/carwash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ 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(move |_| {
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
Expand Down
2 changes: 1 addition & 1 deletion examples/monitoring-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl PCBStateCtx {
}

fn process_code(r: Resources) -> Box<Process<PCBState>> {
Box::new(move |_| {
Box::new(#[coroutine] move |_| {
let mut current_pcb_id = 0;
let mut ctx = PCBStateCtx::new(r);
loop {
Expand Down
4 changes: 2 additions & 2 deletions examples/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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(move |_| {
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);
Expand All @@ -53,7 +53,7 @@ fn main() {
yield MyState::Wait(10.0);
}
}));
let p2 = s.create_process(Box::new(move |_| {
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
for _ in 0..10 {
// wait for the CPU
let ret = yield MyState::Pull(queue);
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ use resources::{Resource, Store};
/// }
///
/// let mut sim = Simulation::new();
/// sim.create_process(Box::new(move |_| {
/// sim.create_process(Box::new(#[coroutine] move |_| {
/// yield ItemState { stage: StageType::FirstPass,
/// effect: Effect::TimeOut(10.0),
/// log: true,
Expand Down Expand Up @@ -530,7 +530,7 @@ mod tests {
use crate::{Effect, Simulation};

let mut s = Simulation::new();
let p = s.create_process(Box::new(|_| {
let p = s.create_process(Box::new(#[coroutine] |_| {
let mut a = 0.0;
loop {
a += 1.0;
Expand All @@ -553,7 +553,7 @@ mod tests {
use crate::{Effect, EndCondition, Simulation};

let mut s = Simulation::new();
let p = s.create_process(Box::new(|_| {
let p = s.create_process(Box::new(#[coroutine] |_| {
let tik = 0.7;
loop {
println!("tik");
Expand All @@ -575,13 +575,13 @@ 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(move |_| {
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(move |_| {
let p2 = s.create_process(Box::new(#[coroutine] move |_| {
yield Effect::Request(r);
yield Effect::TimeOut(3.0);
yield Effect::Release(r);
Expand All @@ -608,13 +608,13 @@ 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(move |_| {
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(move |_| {
let p2 = sim.create_process(Box::new(#[coroutine] move |_| {
yield Effect::Push(store);
yield Effect::TimeOut(3.0);
yield Effect::Push(store);
Expand Down

0 comments on commit accf71f

Please sign in to comment.