Skip to content

Commit 59c7d20

Browse files
author
KDr2
committed
support produce in nested call
1 parent 00eff5b commit 59c7d20

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/tapedtask.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function TapedTask(tf::TapedFunction, args...)
3131
close(consume_ch)
3232
end
3333
t = TapedTask(task, tf, counter, produce_ch, consume_ch)
34+
task.storage === nothing && (task.storage = IdDict())
35+
task.storage[:tapedtask] = t
3436
tf.owner = t
3537
return t
3638
end
@@ -50,7 +52,27 @@ function step_in(tf::TapedFunction, counter::Ref{Int}, args)
5052
end
5153
end
5254

53-
function internel_produce(instr::Instruction, val)
55+
@inline function is_in_tapedtask()
56+
ct = current_task()
57+
ct.storage === nothing && return false
58+
haskey(ct.storage, :tapedtask) || return false
59+
# check if we are recording a tape
60+
ct.storage[:tapedtask].tf.tape === NULL_TAPE && return false
61+
return true
62+
end
63+
64+
function produce(val)
65+
is_in_tapedtask() || return nothing
66+
ttask = current_task().storage[:tapedtask]
67+
put!(ttask.produce_ch, val)
68+
take!(ttask.consume_ch) # wait for next consumer
69+
return nothing
70+
end
71+
72+
#=
73+
# Another way (the old way) to impl `produce`, which does NOT
74+
# support `produce` in a nested call
75+
function internal_produce(instr::Instruction, val)
5476
tape = gettape(instr)
5577
tf = tape.owner
5678
ttask = tf.owner
@@ -63,6 +85,7 @@ function (instr::Instruction{typeof(produce)})()
6385
args = val(instr.input[1])
6486
internel_produce(instr, args)
6587
end
88+
=#
6689

6790
function consume(ttask::TapedTask)
6891
if istaskstarted(ttask.task)

0 commit comments

Comments
 (0)