tro3/ThreadPools.jl

Rebase @tspawnat to Base.@spawn for interpolation syntax

Closed this issue · 2 comments

macro tspawnat(thrdid, expr)
thunk = esc(:(()->($expr)))
var = esc(Base.sync_varname)
tid = esc(thrdid)
quote
if $tid < 1 || $tid > Threads.nthreads()
throw(AssertionError("@tspawnat thread assignment ($($tid)) must be between 1 and Threads.nthreads() (1:$(Threads.nthreads()))"))
end
local task = Task($thunk)
task.sticky = false
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), task, $tid-1)
if $(Expr(:isdefined, var))
push!($var, task)
end
schedule(task)
task
end
end

Could the interpolation syntax support present in Threads.@spawn be added here? Looks like it could be done fairly easily with a bit of copy-paste:

https://github.com/JuliaLang/julia/blob/02990278b31515a4212c2f5cbfe123661f2e26f2/base/threadingconstructs.jl#L166-L182

tro3 commented

Interesting - I bet the Base.@spawn code got updated past v1.3, because at the time, the 1.3 code was mimicked as closely as possible. I'll check to see if an update here would break 1.3 compatibility, but otherwise this is a no-brainer.

It might, actually. I'm not sure if Base._lift_one_interp! was available in v1.3. I tested on v1.4.2. If it's indeed breaking for v1.3 could a reasonable workaround be to wrap the definitions in a conditional checking Julia's version? e.g. if Julia >1.4 use interpolated version, else existing? That ought to make the upgrade transparent and consistent with the functionality of @spawn by Julia version