@@ -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
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"""
8690function 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
108112function 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
358364function _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
365371end
366372
0 commit comments