Skip to content

Commit 9d1ac97

Browse files
Allow unquoted symbols for threadpool in Threads.@spawn (#50182)
Co-authored-by: Julian Samaroo <jpsamaroo@gmail.com>
1 parent 0b87d95 commit 9d1ac97

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

base/threadingconstructs.jl

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,23 @@ function _nthreads_in_pool(tpid::Int8)
5959
end
6060

6161
function _tpid_to_sym(tpid::Int8)
62-
return tpid == 0 ? :interactive : :default
62+
if tpid == 0
63+
return :interactive
64+
elseif tpid == 1
65+
return :default
66+
else
67+
throw(ArgumentError("Unrecognized threadpool id $tpid"))
68+
end
6369
end
6470

6571
function _sym_to_tpid(tp::Symbol)
66-
return tp === :interactive ? Int8(0) : Int8(1)
72+
if tp === :interactive
73+
return Int8(0)
74+
elseif tp === :default
75+
return Int8(1)
76+
else
77+
throw(ArgumentError("Unrecognized threadpool name `$(repr(tp))`"))
78+
end
6779
end
6880

6981
"""
@@ -386,20 +398,18 @@ Hello from 4
386398
```
387399
"""
388400
macro spawn(args...)
389-
tp = :default
401+
tp = QuoteNode(:default)
390402
na = length(args)
391403
if na == 2
392404
ttype, ex = args
393405
if ttype isa QuoteNode
394406
ttype = ttype.value
395-
elseif ttype isa Symbol
396-
# TODO: allow unquoted symbols
397-
ttype = nothing
398-
end
399-
if ttype === :interactive || ttype === :default
400-
tp = ttype
407+
if ttype !== :interactive && ttype !== :default
408+
throw(ArgumentError("unsupported threadpool in @spawn: $ttype"))
409+
end
410+
tp = QuoteNode(ttype)
401411
else
402-
throw(ArgumentError("unsupported threadpool in @spawn: $ttype"))
412+
tp = ttype
403413
end
404414
elseif na == 1
405415
ex = args[1]
@@ -415,7 +425,7 @@ macro spawn(args...)
415425
let $(letargs...)
416426
local task = Task($thunk)
417427
task.sticky = false
418-
_spawn_set_thrpool(task, $(QuoteNode(tp)))
428+
_spawn_set_thrpool(task, $(esc(tp)))
419429
if $(Expr(:islocal, var))
420430
put!($var, task)
421431
end

test/threadpool_use.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ using Base.Threads
99
@test fetch(Threads.@spawn Threads.threadpool()) === :default
1010
@test fetch(Threads.@spawn :default Threads.threadpool()) === :default
1111
@test fetch(Threads.@spawn :interactive Threads.threadpool()) === :interactive
12+
tp = :default
13+
@test fetch(Threads.@spawn tp Threads.threadpool()) === :default
14+
tp = :interactive
15+
@test fetch(Threads.@spawn tp Threads.threadpool()) === :interactive
16+
tp = :foo
17+
@test_throws ArgumentError Threads.@spawn tp Threads.threadpool()
1218
@test Threads.threadpooltids(:interactive) == [1]
1319
@test Threads.threadpooltids(:default) == [2]

0 commit comments

Comments
 (0)