forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.j
30 lines (26 loc) · 691 Bytes
/
task.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
show(t::Task) = print("Task")
function wait(t::Task)
@assert !is(t,current_task())
while !task_done(t)
yieldto(t)
end
yieldto(t) # return last value
end
let _generator_stack = {}
global produce, consume
function produce(v)
caller = pop(_generator_stack::Array{Any,1})
yieldto(caller, v)
end
function consume(G::Task, args...)
push(_generator_stack::Array{Any,1}, current_task())
v = yieldto(G, args...)
if task_done(G)
pop(_generator_stack::Array{Any,1})
end
v
end
end
start(t::Task) = consume(t)
done(t::Task, val) = task_done(t)
next(t::Task, val) = (val, consume(t))