Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unquoted symbols for threadpool in Threads.@spawn #50182

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions base/threadingconstructs.jl
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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]