@@ -35,6 +35,19 @@ julia> Threads.threadid(Threads.@spawn "foo")
35
35
"""
36
36
threadid () = Int (ccall (:jl_threadid , Int16, ())+ 1 )
37
37
38
+ """
39
+ Threads.threadpool_threadidx() -> Int
40
+
41
+ Return the index of the current thread in its threadpool.
42
+ i.e. For the first default thread when 1 interactive thread exists this returns 1
43
+ because the interactive threadpool comes first in threadid order.
44
+ """
45
+ function threadpool_threadidx ()
46
+ pool = threadpool ()
47
+ first_tid = _threadpool_first_threadid (pool)
48
+ return Int (threadid () - first_tid + 1 )
49
+ end
50
+
38
51
# lower bound on the largest threadid()
39
52
"""
40
53
Threads.maxthreadid() -> Int
@@ -144,17 +157,29 @@ function threadpoolsize(pool::Symbol = :default)
144
157
return _nthreads_in_pool (tpid)
145
158
end
146
159
160
+ function _threadpool_first_threadid (pool:: Symbol )
161
+ if pool === :interactive
162
+ return 1
163
+ end
164
+ if pool === :default
165
+ ni = _nthreads_in_pool (Int8 (0 ))
166
+ return ni+ 1
167
+ else
168
+ error (" invalid threadpool specified" )
169
+ end
170
+ end
171
+
147
172
"""
148
173
threadpooltids(pool::Symbol)
149
174
150
175
Returns a vector of IDs of threads in the given pool.
151
176
"""
152
177
function threadpooltids (pool:: Symbol )
153
- ni = _nthreads_in_pool ( Int8 ( 0 ) )
178
+ first_tid = _threadpool_first_threadid (pool )
154
179
if pool === :interactive
155
- return collect (1 : ni )
180
+ return collect (1 : _nthreads_in_pool ( Int8 ( 0 )) )
156
181
elseif pool === :default
157
- return collect (ni + 1 : ni + _nthreads_in_pool (Int8 (1 )))
182
+ return collect (first_tid : first_tid + _nthreads_in_pool (Int8 (1 )))
158
183
else
159
184
error (" invalid threadpool specified" )
160
185
end
0 commit comments