Skip to content

Commit cc3e7b6

Browse files
Add option to use @spawn :samepool for using the same threadpool as the caller (#57109)
1 parent 47a6ad0 commit cc3e7b6

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ New language features
2323
- actual running time for the task (`Base.Experimental.task_running_time_ns`), and
2424
- wall-time for the task (`Base.Experimental.task_wall_time_ns`).
2525
- Support for Unicode 16 ([#56925]).
26+
- `Threads.@spawn` now takes a `:samepool` argument to specify the same threadpool as the caller.
27+
`Threads.@spawn :samepool foo()` which is shorthand for `Threads.@spawn Threads.threadpool() foo()` ([#57109])
2628

2729
Language changes
2830
----------------

base/threadingconstructs.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,11 @@ function _spawn_set_thrpool(t::Task, tp::Symbol)
440440
end
441441

442442
"""
443-
Threads.@spawn [:default|:interactive] expr
443+
Threads.@spawn [:default|:interactive|:samepool] expr
444444
445445
Create a [`Task`](@ref) and [`schedule`](@ref) it to run on any available
446-
thread in the specified threadpool (`:default` if unspecified). The task is
446+
thread in the specified threadpool: `:default`, `:interactive`, or `:samepool`
447+
to use the same as the caller. `:default` is used if unspecified. The task is
447448
allocated to a thread once one becomes available. To wait for the task to
448449
finish, call [`wait`](@ref) on the result of this macro, or call
449450
[`fetch`](@ref) to wait and then obtain its return value.
@@ -468,6 +469,9 @@ the variable's value in the current task.
468469
!!! compat "Julia 1.9"
469470
A threadpool may be specified as of Julia 1.9.
470471
472+
!!! compat "Julia 1.12"
473+
The same threadpool may be specified as of Julia 1.12.
474+
471475
# Examples
472476
```julia-repl
473477
julia> t() = println("Hello from ", Threads.threadid());
@@ -486,7 +490,7 @@ macro spawn(args...)
486490
ttype, ex = args
487491
if ttype isa QuoteNode
488492
ttype = ttype.value
489-
if ttype !== :interactive && ttype !== :default
493+
if !in(ttype, (:interactive, :default, :samepool))
490494
throw(ArgumentError(LazyString("unsupported threadpool in @spawn: ", ttype)))
491495
end
492496
tp = QuoteNode(ttype)
@@ -507,7 +511,11 @@ macro spawn(args...)
507511
let $(letargs...)
508512
local task = Task($thunk)
509513
task.sticky = false
510-
_spawn_set_thrpool(task, $(esc(tp)))
514+
local tp = $(esc(tp))
515+
if tp == :samepool
516+
tp = Threads.threadpool()
517+
end
518+
_spawn_set_thrpool(task, tp)
511519
if $(Expr(:islocal, var))
512520
put!($var, task)
513521
end

test/threadpool_use.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ 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+
@test fetch(Threads.@spawn :samepool Threads.threadpool()) === Threads.threadpool()
13+
@sync for tp in [:interactive, :default]
14+
Threads.@spawn tp begin
15+
@test fetch(Threads.@spawn :samepool Threads.threadpool()) === Threads.threadpool()
16+
end
17+
end
18+
wait(Threads.@spawn :interactive begin
19+
@test fetch(Threads.@spawn :samepool Threads.threadpool()) === Threads.threadpool()
20+
end)
1221
tp = :default
1322
@test fetch(Threads.@spawn tp Threads.threadpool()) === :default
1423
tp = :interactive

0 commit comments

Comments
 (0)