Skip to content

Commit 526df3e

Browse files
vchuravynalimilan
authored andcommitted
Separate foreign threads into a :foreign threadpool (#50912)
Co-authored-by: Gabriel Baraldi <[email protected]> Co-authored-by: Dilum Aluthge <[email protected]> (cherry picked from commit 8be469e)
1 parent ec76d27 commit 526df3e

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

base/partr.jl

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ end
9595

9696
function multiq_insert(task::Task, priority::UInt16)
9797
tpid = ccall(:jl_get_task_threadpoolid, Int8, (Any,), task)
98+
@assert tpid > -1
9899
heap_p = multiq_size(tpid)
99100
tp = tpid + 1
100101

@@ -131,6 +132,9 @@ function multiq_deletemin()
131132

132133
tid = Threads.threadid()
133134
tp = ccall(:jl_threadpoolid, Int8, (Int16,), tid-1) + 1
135+
if tp == 0 # Foreign thread
136+
return nothing
137+
end
134138
tpheaps = heaps[tp]
135139

136140
@label retry
@@ -182,6 +186,9 @@ end
182186
function multiq_check_empty()
183187
tid = Threads.threadid()
184188
tp = ccall(:jl_threadpoolid, Int8, (Int16,), tid-1) + 1
189+
if tp == 0 # Foreign thread
190+
return true
191+
end
185192
for i = UInt32(1):length(heaps[tp])
186193
if heaps[tp][i].ntasks != 0
187194
return false

base/task.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ function enq_work(t::Task)
794794
else
795795
@label not_sticky
796796
tp = Threads.threadpool(t)
797-
if Threads.threadpoolsize(tp) == 1
797+
if tp === :foreign || Threads.threadpoolsize(tp) == 1
798798
# There's only one thread in the task's assigned thread pool;
799799
# use its work queue.
800800
tid = (tp === :interactive) ? 1 : Threads.threadpoolsize(:interactive)+1

base/threadingconstructs.jl

+10-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ function _tpid_to_sym(tpid::Int8)
6363
return :interactive
6464
elseif tpid == 1
6565
return :default
66+
elseif tpid == -1
67+
return :foreign
6668
else
6769
throw(ArgumentError("Unrecognized threadpool id $tpid"))
6870
end
@@ -73,6 +75,8 @@ function _sym_to_tpid(tp::Symbol)
7375
return Int8(0)
7476
elseif tp === :default
7577
return Int8(1)
78+
elseif tp == :foreign
79+
return Int8(-1)
7680
else
7781
throw(ArgumentError("Unrecognized threadpool name `$(repr(tp))`"))
7882
end
@@ -81,7 +85,7 @@ end
8185
"""
8286
Threads.threadpool(tid = threadid()) -> Symbol
8387
84-
Returns the specified thread's threadpool; either `:default` or `:interactive`.
88+
Returns the specified thread's threadpool; either `:default`, `:interactive`, or `:foreign`.
8589
"""
8690
function threadpool(tid = threadid())
8791
tpid = ccall(:jl_threadpoolid, Int8, (Int16,), tid-1)
@@ -108,6 +112,8 @@ See also: `BLAS.get_num_threads` and `BLAS.set_num_threads` in the
108112
function threadpoolsize(pool::Symbol = :default)
109113
if pool === :default || pool === :interactive
110114
tpid = _sym_to_tpid(pool)
115+
elseif pool == :foreign
116+
error("Threadpool size of `:foreign` is indeterminant")
111117
else
112118
error("invalid threadpool specified")
113119
end
@@ -151,7 +157,7 @@ function threading_run(fun, static)
151157
else
152158
# TODO: this should be the current pool (except interactive) if there
153159
# are ever more than two pools.
154-
ccall(:jl_set_task_threadpoolid, Cint, (Any, Int8), t, _sym_to_tpid(:default))
160+
@assert ccall(:jl_set_task_threadpoolid, Cint, (Any, Int8), t, _sym_to_tpid(:default)) == 1
155161
end
156162
tasks[i] = t
157163
schedule(t)
@@ -357,10 +363,10 @@ end
357363

358364
function _spawn_set_thrpool(t::Task, tp::Symbol)
359365
tpid = _sym_to_tpid(tp)
360-
if _nthreads_in_pool(tpid) == 0
366+
if tpid == -1 || _nthreads_in_pool(tpid) == 0
361367
tpid = _sym_to_tpid(:default)
362368
end
363-
ccall(:jl_set_task_threadpoolid, Cint, (Any, Int8), t, tpid)
369+
@assert ccall(:jl_set_task_threadpoolid, Cint, (Any, Int8), t, tpid) == 1
364370
nothing
365371
end
366372

src/partr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ JL_DLLEXPORT int jl_set_task_tid(jl_task_t *task, int16_t tid) JL_NOTSAFEPOINT
7070

7171
JL_DLLEXPORT int jl_set_task_threadpoolid(jl_task_t *task, int8_t tpid) JL_NOTSAFEPOINT
7272
{
73-
if (tpid < 0 || tpid >= jl_n_threadpools)
73+
if (tpid < -1 || tpid >= jl_n_threadpools)
7474
return 0;
7575
task->threadpoolid = tpid;
7676
return 1;

src/threading.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ JL_DLLEXPORT int8_t jl_threadpoolid(int16_t tid) JL_NOTSAFEPOINT
332332
if (tid < n)
333333
return (int8_t)i;
334334
}
335-
return 0; // everything else uses threadpool 0 (though does not become part of any threadpool)
335+
return -1; // everything else uses threadpool -1 (does not belong to any threadpool)
336336
}
337337

338338
jl_ptls_t jl_init_threadtls(int16_t tid)

0 commit comments

Comments
 (0)