Skip to content

Commit dc46cb3

Browse files
committed
Cleanup produce and coroutine transform
1 parent 7167fc6 commit dc46cb3

5 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/finitestatemachines/transforms.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function transformVars!(expr::Expr, symbols::Base.KeyIterator{Dict{Symbol,Type}}
33
if expr.head == :kw && i == 1
44

55
elseif expr.head == Symbol(".") && i == 2
6-
6+
77
elseif isa(expr.args[i], Symbol) && in(expr.args[i], symbols)
88
expr.args[i] = :(_fsm.$(expr.args[i]))
99
elseif isa(expr.args[i], Expr)
@@ -55,7 +55,6 @@ function transformYield!(expr::Expr, n::UInt8=0x00, super::Expr=:(), line_no::In
5555
if isa(arg, Expr)
5656
if arg.head == :try
5757
n = transformTry!(arg.args[1], n, super, line_no, expr, i)
58-
println("hi")
5958
elseif arg.head == :line
6059
line_no = i+1
6160
super = expr

src/tasks/base.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ end
88
produce(v...) = produce(v)
99

1010
function consume(producer::Task, values...)
11-
istaskdone(producer) && return wait(producer)
11+
istaskdone(producer) && return producer.value
1212
ct = current_task()
1313
ct.result = length(values)==1 ? values[1] : values
1414
producer.consumers = ct
15-
producer.state == :runnable ? Base.schedule_and_wait(producer) : wait()
15+
Base.schedule_and_wait(producer)
1616
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ testpath(f) = joinpath(dirname(@__FILE__), f)
44
for test_file in [
55
"finitestatemachines/test_transforms.jl",
66
"finitestatemachines/test_utils.jl",
7+
"tasks/test_produce_consume.jl",
78
"test_base.jl",
89
"test_events.jl",
910
"test_operators.jl",

test/tasks/test_produce_consume.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using SimJulia
2+
3+
function fibonnaci(sim::Simulation)
4+
a = 0.0
5+
b = 1.0
6+
for i = 1:10
7+
println("Fibonnaci value equals ", a, " at time ", now(sim))
8+
yield(Timeout(sim, 1.0))
9+
a, b = b, a+b
10+
end
11+
a
12+
end
13+
14+
function wait_for_process(sim::Simulation, process::Process)
15+
val = yield(process)
16+
println("Fibonnaci ended with ", val, " at time ", now(sim))
17+
end
18+
19+
sim = Simulation()
20+
fib = @Process fibonnaci(sim)
21+
wait = @Process wait_for_process(sim, fib)
22+
run(sim)

test/test_coroutines.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ end
1717
@stateful function test_process_exception(sim::Simulation, ev::AbstractEvent)
1818
try
1919
value = @yield return ev
20-
println("hi")
2120
catch
2221
println("$value has been thrown")
2322
end

0 commit comments

Comments
 (0)