Skip to content

Commit

Permalink
Allow unquoted symbols for threadpool in Threads.@spawn (#50182)
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Samaroo <jpsamaroo@gmail.com>
  • Loading branch information
IanButterworth and jpsamaroo committed Jun 15, 2023
1 parent 0b87d95 commit 9d1ac97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
32 changes: 21 additions & 11 deletions base/threadingconstructs.jl
Expand Up @@ -59,11 +59,23 @@ function _nthreads_in_pool(tpid::Int8)
end

function _tpid_to_sym(tpid::Int8)
return tpid == 0 ? :interactive : :default
if tpid == 0
return :interactive
elseif tpid == 1
return :default
else
throw(ArgumentError("Unrecognized threadpool id $tpid"))
end
end

function _sym_to_tpid(tp::Symbol)
return tp === :interactive ? Int8(0) : Int8(1)
if tp === :interactive
return Int8(0)
elseif tp === :default
return Int8(1)
else
throw(ArgumentError("Unrecognized threadpool name `$(repr(tp))`"))
end
end

"""
Expand Down Expand Up @@ -386,20 +398,18 @@ Hello from 4
```
"""
macro spawn(args...)
tp = :default
tp = QuoteNode(:default)
na = length(args)
if na == 2
ttype, ex = args
if ttype isa QuoteNode
ttype = ttype.value
elseif ttype isa Symbol
# TODO: allow unquoted symbols
ttype = nothing
end
if ttype === :interactive || ttype === :default
tp = ttype
if ttype !== :interactive && ttype !== :default
throw(ArgumentError("unsupported threadpool in @spawn: $ttype"))
end
tp = QuoteNode(ttype)
else
throw(ArgumentError("unsupported threadpool in @spawn: $ttype"))
tp = ttype
end
elseif na == 1
ex = args[1]
Expand All @@ -415,7 +425,7 @@ macro spawn(args...)
let $(letargs...)
local task = Task($thunk)
task.sticky = false
_spawn_set_thrpool(task, $(QuoteNode(tp)))
_spawn_set_thrpool(task, $(esc(tp)))
if $(Expr(:islocal, var))
put!($var, task)
end
Expand Down
6 changes: 6 additions & 0 deletions test/threadpool_use.jl
Expand Up @@ -9,5 +9,11 @@ using Base.Threads
@test fetch(Threads.@spawn Threads.threadpool()) === :default
@test fetch(Threads.@spawn :default Threads.threadpool()) === :default
@test fetch(Threads.@spawn :interactive Threads.threadpool()) === :interactive
tp = :default
@test fetch(Threads.@spawn tp Threads.threadpool()) === :default
tp = :interactive
@test fetch(Threads.@spawn tp Threads.threadpool()) === :interactive
tp = :foo
@test_throws ArgumentError Threads.@spawn tp Threads.threadpool()
@test Threads.threadpooltids(:interactive) == [1]
@test Threads.threadpooltids(:default) == [2]

0 comments on commit 9d1ac97

Please sign in to comment.