kern_threadpool.c revision 1.15.4.3 1 1.15.4.3 martin /* $NetBSD: kern_threadpool.c,v 1.15.4.3 2020/04/08 14:08:51 martin Exp $ */
2 1.15.4.2 christos
3 1.15.4.2 christos /*-
4 1.15.4.2 christos * Copyright (c) 2014, 2018 The NetBSD Foundation, Inc.
5 1.15.4.2 christos * All rights reserved.
6 1.15.4.2 christos *
7 1.15.4.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.15.4.2 christos * by Taylor R. Campbell and Jason R. Thorpe.
9 1.15.4.2 christos *
10 1.15.4.2 christos * Redistribution and use in source and binary forms, with or without
11 1.15.4.2 christos * modification, are permitted provided that the following conditions
12 1.15.4.2 christos * are met:
13 1.15.4.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.15.4.2 christos * notice, this list of conditions and the following disclaimer.
15 1.15.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.15.4.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.15.4.2 christos * documentation and/or other materials provided with the distribution.
18 1.15.4.2 christos *
19 1.15.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.15.4.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.15.4.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.15.4.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.15.4.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.15.4.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.15.4.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.15.4.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.15.4.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.15.4.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.15.4.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.15.4.2 christos */
31 1.15.4.2 christos
32 1.15.4.2 christos /*
33 1.15.4.2 christos * Thread pools.
34 1.15.4.2 christos *
35 1.15.4.2 christos * A thread pool is a collection of worker threads idle or running
36 1.15.4.2 christos * jobs, together with an overseer thread that does not run jobs but
37 1.15.4.2 christos * can be given jobs to assign to a worker thread. Scheduling a job in
38 1.15.4.2 christos * a thread pool does not allocate or even sleep at all, except perhaps
39 1.15.4.2 christos * on an adaptive lock, unlike kthread_create. Jobs reuse threads, so
40 1.15.4.2 christos * they do not incur the expense of creating and destroying kthreads
41 1.15.4.2 christos * unless there is not much work to be done.
42 1.15.4.2 christos *
43 1.15.4.2 christos * A per-CPU thread pool (threadpool_percpu) is a collection of thread
44 1.15.4.2 christos * pools, one per CPU bound to that CPU. For each priority level in
45 1.15.4.2 christos * use, there is one shared unbound thread pool (i.e., pool of threads
46 1.15.4.2 christos * not bound to any CPU) and one shared per-CPU thread pool.
47 1.15.4.2 christos *
48 1.15.4.2 christos * To use the unbound thread pool at priority pri, call
49 1.15.4.2 christos * threadpool_get(&pool, pri). When you're done, call
50 1.15.4.2 christos * threadpool_put(pool, pri).
51 1.15.4.2 christos *
52 1.15.4.2 christos * To use the per-CPU thread pools at priority pri, call
53 1.15.4.2 christos * threadpool_percpu_get(&pool_percpu, pri), and then use the thread
54 1.15.4.2 christos * pool returned by threadpool_percpu_ref(pool_percpu) for the current
55 1.15.4.2 christos * CPU, or by threadpool_percpu_ref_remote(pool_percpu, ci) for another
56 1.15.4.2 christos * CPU. When you're done, call threadpool_percpu_put(pool_percpu,
57 1.15.4.2 christos * pri).
58 1.15.4.2 christos *
59 1.15.4.2 christos * +--MACHINE-----------------------------------------------+
60 1.15.4.2 christos * | +--CPU 0-------+ +--CPU 1-------+ +--CPU n-------+ |
61 1.15.4.2 christos * | | <overseer 0> | | <overseer 1> | ... | <overseer n> | |
62 1.15.4.2 christos * | | <idle 0a> | | <running 1a> | ... | <idle na> | |
63 1.15.4.2 christos * | | <running 0b> | | <running 1b> | ... | <idle nb> | |
64 1.15.4.2 christos * | | . | | . | ... | . | |
65 1.15.4.2 christos * | | . | | . | ... | . | |
66 1.15.4.2 christos * | | . | | . | ... | . | |
67 1.15.4.2 christos * | +--------------+ +--------------+ +--------------+ |
68 1.15.4.2 christos * | +--unbound---------+ |
69 1.15.4.2 christos * | | <overseer n+1> | |
70 1.15.4.2 christos * | | <idle (n+1)a> | |
71 1.15.4.2 christos * | | <running (n+1)b> | |
72 1.15.4.2 christos * | +------------------+ |
73 1.15.4.2 christos * +--------------------------------------------------------+
74 1.15.4.2 christos *
75 1.15.4.2 christos * XXX Why one overseer per CPU? I did that originally to avoid
76 1.15.4.2 christos * touching remote CPUs' memory when scheduling a job, but that still
77 1.15.4.2 christos * requires interprocessor synchronization. Perhaps we could get by
78 1.15.4.2 christos * with a single overseer thread, at the expense of another pointer in
79 1.15.4.2 christos * struct threadpool_job to identify the CPU on which it must run
80 1.15.4.2 christos * in order for the overseer to schedule it correctly.
81 1.15.4.2 christos */
82 1.15.4.2 christos
83 1.15.4.2 christos #include <sys/cdefs.h>
84 1.15.4.3 martin __KERNEL_RCSID(0, "$NetBSD: kern_threadpool.c,v 1.15.4.3 2020/04/08 14:08:51 martin Exp $");
85 1.15.4.2 christos
86 1.15.4.2 christos #include <sys/types.h>
87 1.15.4.2 christos #include <sys/param.h>
88 1.15.4.2 christos #include <sys/atomic.h>
89 1.15.4.2 christos #include <sys/condvar.h>
90 1.15.4.2 christos #include <sys/cpu.h>
91 1.15.4.2 christos #include <sys/kernel.h>
92 1.15.4.2 christos #include <sys/kmem.h>
93 1.15.4.2 christos #include <sys/kthread.h>
94 1.15.4.2 christos #include <sys/mutex.h>
95 1.15.4.2 christos #include <sys/once.h>
96 1.15.4.2 christos #include <sys/percpu.h>
97 1.15.4.2 christos #include <sys/pool.h>
98 1.15.4.2 christos #include <sys/proc.h>
99 1.15.4.2 christos #include <sys/queue.h>
100 1.15.4.3 martin #include <sys/sdt.h>
101 1.15.4.2 christos #include <sys/sysctl.h>
102 1.15.4.3 martin #include <sys/systm.h>
103 1.15.4.2 christos #include <sys/threadpool.h>
104 1.15.4.2 christos
105 1.15.4.3 martin /* Probes */
106 1.15.4.3 martin
107 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, get,
108 1.15.4.3 martin "pri_t"/*pri*/);
109 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, get__create,
110 1.15.4.3 martin "pri_t"/*pri*/);
111 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, get__race,
112 1.15.4.3 martin "pri_t"/*pri*/);
113 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, put,
114 1.15.4.3 martin "struct threadpool *"/*pool*/, "pri_t"/*pri*/);
115 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, put__destroy,
116 1.15.4.3 martin "struct threadpool *"/*pool*/, "pri_t"/*pri*/);
117 1.15.4.3 martin
118 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, percpu__get,
119 1.15.4.3 martin "pri_t"/*pri*/);
120 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, percpu__get__create,
121 1.15.4.3 martin "pri_t"/*pri*/);
122 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, percpu__get__race,
123 1.15.4.3 martin "pri_t"/*pri*/);
124 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, percpu__put,
125 1.15.4.3 martin "struct threadpool *"/*pool*/, "pri_t"/*pri*/);
126 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, percpu__put__destroy,
127 1.15.4.3 martin "struct threadpool *"/*pool*/, "pri_t"/*pri*/);
128 1.15.4.3 martin
129 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, create,
130 1.15.4.3 martin "struct cpu_info *"/*ci*/, "pri_t"/*pri*/);
131 1.15.4.3 martin SDT_PROBE_DEFINE3(sdt, kernel, threadpool, create__success,
132 1.15.4.3 martin "struct cpu_info *"/*ci*/, "pri_t"/*pri*/, "struct threadpool *"/*pool*/);
133 1.15.4.3 martin SDT_PROBE_DEFINE3(sdt, kernel, threadpool, create__failure,
134 1.15.4.3 martin "struct cpu_info *"/*ci*/, "pri_t"/*pri*/, "int"/*error*/);
135 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, destroy,
136 1.15.4.3 martin "struct threadpool *"/*pool*/);
137 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, destroy__wait,
138 1.15.4.3 martin "struct threadpool *"/*pool*/, "uint64_t"/*refcnt*/);
139 1.15.4.3 martin
140 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, schedule__job,
141 1.15.4.3 martin "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
142 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, schedule__job__running,
143 1.15.4.3 martin "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
144 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, schedule__job__overseer,
145 1.15.4.3 martin "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
146 1.15.4.3 martin SDT_PROBE_DEFINE3(sdt, kernel, threadpool, schedule__job__thread,
147 1.15.4.3 martin "struct threadpool *"/*pool*/,
148 1.15.4.3 martin "struct threadpool_job *"/*job*/,
149 1.15.4.3 martin "struct lwp *"/*thread*/);
150 1.15.4.3 martin
151 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, overseer__start,
152 1.15.4.3 martin "struct threadpool *"/*pool*/);
153 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, overseer__dying,
154 1.15.4.3 martin "struct threadpool *"/*pool*/);
155 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, overseer__spawn,
156 1.15.4.3 martin "struct threadpool *"/*pool*/);
157 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, overseer__race,
158 1.15.4.3 martin "struct threadpool *"/*pool*/,
159 1.15.4.3 martin "struct threadpool_job *"/*job*/);
160 1.15.4.3 martin SDT_PROBE_DEFINE3(sdt, kernel, threadpool, overseer__assign,
161 1.15.4.3 martin "struct threadpool *"/*pool*/,
162 1.15.4.3 martin "struct threadpool_job *"/*job*/,
163 1.15.4.3 martin "struct lwp *"/*thread*/);
164 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, overseer__exit,
165 1.15.4.3 martin "struct threadpool *"/*pool*/);
166 1.15.4.3 martin
167 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, thread__start,
168 1.15.4.3 martin "struct threadpool *"/*pool*/);
169 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, thread__dying,
170 1.15.4.3 martin "struct threadpool *"/*pool*/);
171 1.15.4.3 martin SDT_PROBE_DEFINE2(sdt, kernel, threadpool, thread__job,
172 1.15.4.3 martin "struct threadpool *"/*pool*/, "struct threadpool_job *"/*job*/);
173 1.15.4.3 martin SDT_PROBE_DEFINE1(sdt, kernel, threadpool, thread__exit,
174 1.15.4.3 martin "struct threadpool *"/*pool*/);
175 1.15.4.3 martin
176 1.15.4.2 christos /* Data structures */
177 1.15.4.2 christos
178 1.15.4.2 christos TAILQ_HEAD(job_head, threadpool_job);
179 1.15.4.2 christos TAILQ_HEAD(thread_head, threadpool_thread);
180 1.15.4.2 christos
181 1.15.4.2 christos struct threadpool_thread {
182 1.15.4.2 christos struct lwp *tpt_lwp;
183 1.15.4.2 christos char *tpt_lwp_savedname;
184 1.15.4.2 christos struct threadpool *tpt_pool;
185 1.15.4.2 christos struct threadpool_job *tpt_job;
186 1.15.4.2 christos kcondvar_t tpt_cv;
187 1.15.4.2 christos TAILQ_ENTRY(threadpool_thread) tpt_entry;
188 1.15.4.2 christos };
189 1.15.4.2 christos
190 1.15.4.2 christos struct threadpool {
191 1.15.4.2 christos kmutex_t tp_lock;
192 1.15.4.2 christos struct threadpool_thread tp_overseer;
193 1.15.4.2 christos struct job_head tp_jobs;
194 1.15.4.2 christos struct thread_head tp_idle_threads;
195 1.15.4.2 christos uint64_t tp_refcnt;
196 1.15.4.2 christos int tp_flags;
197 1.15.4.2 christos #define THREADPOOL_DYING 0x01
198 1.15.4.2 christos struct cpu_info *tp_cpu;
199 1.15.4.2 christos pri_t tp_pri;
200 1.15.4.2 christos };
201 1.15.4.2 christos
202 1.15.4.2 christos static void threadpool_hold(struct threadpool *);
203 1.15.4.2 christos static void threadpool_rele(struct threadpool *);
204 1.15.4.2 christos
205 1.15.4.2 christos static int threadpool_percpu_create(struct threadpool_percpu **, pri_t);
206 1.15.4.2 christos static void threadpool_percpu_destroy(struct threadpool_percpu *);
207 1.15.4.3 martin static void threadpool_percpu_init(void *, void *, struct cpu_info *);
208 1.15.4.3 martin static void threadpool_percpu_ok(void *, void *, struct cpu_info *);
209 1.15.4.3 martin static void threadpool_percpu_fini(void *, void *, struct cpu_info *);
210 1.15.4.2 christos
211 1.15.4.2 christos static threadpool_job_fn_t threadpool_job_dead;
212 1.15.4.2 christos
213 1.15.4.2 christos static void threadpool_job_hold(struct threadpool_job *);
214 1.15.4.2 christos static void threadpool_job_rele(struct threadpool_job *);
215 1.15.4.2 christos
216 1.15.4.2 christos static void threadpool_overseer_thread(void *) __dead;
217 1.15.4.2 christos static void threadpool_thread(void *) __dead;
218 1.15.4.2 christos
219 1.15.4.2 christos static pool_cache_t threadpool_thread_pc __read_mostly;
220 1.15.4.2 christos
221 1.15.4.2 christos static kmutex_t threadpools_lock __cacheline_aligned;
222 1.15.4.2 christos
223 1.15.4.2 christos /* Default to 30 second idle timeout for pool threads. */
224 1.15.4.2 christos static int threadpool_idle_time_ms = 30 * 1000;
225 1.15.4.2 christos
226 1.15.4.2 christos struct threadpool_unbound {
227 1.15.4.2 christos struct threadpool tpu_pool;
228 1.15.4.2 christos
229 1.15.4.2 christos /* protected by threadpools_lock */
230 1.15.4.2 christos LIST_ENTRY(threadpool_unbound) tpu_link;
231 1.15.4.2 christos uint64_t tpu_refcnt;
232 1.15.4.2 christos };
233 1.15.4.2 christos
234 1.15.4.2 christos static LIST_HEAD(, threadpool_unbound) unbound_threadpools;
235 1.15.4.2 christos
236 1.15.4.2 christos static struct threadpool_unbound *
237 1.15.4.2 christos threadpool_lookup_unbound(pri_t pri)
238 1.15.4.2 christos {
239 1.15.4.2 christos struct threadpool_unbound *tpu;
240 1.15.4.2 christos
241 1.15.4.2 christos LIST_FOREACH(tpu, &unbound_threadpools, tpu_link) {
242 1.15.4.2 christos if (tpu->tpu_pool.tp_pri == pri)
243 1.15.4.2 christos return tpu;
244 1.15.4.2 christos }
245 1.15.4.2 christos return NULL;
246 1.15.4.2 christos }
247 1.15.4.2 christos
248 1.15.4.2 christos static void
249 1.15.4.2 christos threadpool_insert_unbound(struct threadpool_unbound *tpu)
250 1.15.4.2 christos {
251 1.15.4.2 christos KASSERT(threadpool_lookup_unbound(tpu->tpu_pool.tp_pri) == NULL);
252 1.15.4.2 christos LIST_INSERT_HEAD(&unbound_threadpools, tpu, tpu_link);
253 1.15.4.2 christos }
254 1.15.4.2 christos
255 1.15.4.2 christos static void
256 1.15.4.2 christos threadpool_remove_unbound(struct threadpool_unbound *tpu)
257 1.15.4.2 christos {
258 1.15.4.2 christos KASSERT(threadpool_lookup_unbound(tpu->tpu_pool.tp_pri) == tpu);
259 1.15.4.2 christos LIST_REMOVE(tpu, tpu_link);
260 1.15.4.2 christos }
261 1.15.4.2 christos
262 1.15.4.2 christos struct threadpool_percpu {
263 1.15.4.2 christos percpu_t * tpp_percpu;
264 1.15.4.2 christos pri_t tpp_pri;
265 1.15.4.2 christos
266 1.15.4.2 christos /* protected by threadpools_lock */
267 1.15.4.2 christos LIST_ENTRY(threadpool_percpu) tpp_link;
268 1.15.4.2 christos uint64_t tpp_refcnt;
269 1.15.4.2 christos };
270 1.15.4.2 christos
271 1.15.4.2 christos static LIST_HEAD(, threadpool_percpu) percpu_threadpools;
272 1.15.4.2 christos
273 1.15.4.2 christos static struct threadpool_percpu *
274 1.15.4.2 christos threadpool_lookup_percpu(pri_t pri)
275 1.15.4.2 christos {
276 1.15.4.2 christos struct threadpool_percpu *tpp;
277 1.15.4.2 christos
278 1.15.4.2 christos LIST_FOREACH(tpp, &percpu_threadpools, tpp_link) {
279 1.15.4.2 christos if (tpp->tpp_pri == pri)
280 1.15.4.2 christos return tpp;
281 1.15.4.2 christos }
282 1.15.4.2 christos return NULL;
283 1.15.4.2 christos }
284 1.15.4.2 christos
285 1.15.4.2 christos static void
286 1.15.4.2 christos threadpool_insert_percpu(struct threadpool_percpu *tpp)
287 1.15.4.2 christos {
288 1.15.4.2 christos KASSERT(threadpool_lookup_percpu(tpp->tpp_pri) == NULL);
289 1.15.4.2 christos LIST_INSERT_HEAD(&percpu_threadpools, tpp, tpp_link);
290 1.15.4.2 christos }
291 1.15.4.2 christos
292 1.15.4.2 christos static void
293 1.15.4.2 christos threadpool_remove_percpu(struct threadpool_percpu *tpp)
294 1.15.4.2 christos {
295 1.15.4.2 christos KASSERT(threadpool_lookup_percpu(tpp->tpp_pri) == tpp);
296 1.15.4.2 christos LIST_REMOVE(tpp, tpp_link);
297 1.15.4.2 christos }
298 1.15.4.2 christos
299 1.15.4.2 christos static int
300 1.15.4.2 christos sysctl_kern_threadpool_idle_ms(SYSCTLFN_ARGS)
301 1.15.4.2 christos {
302 1.15.4.2 christos struct sysctlnode node;
303 1.15.4.2 christos int val, error;
304 1.15.4.2 christos
305 1.15.4.2 christos node = *rnode;
306 1.15.4.2 christos
307 1.15.4.2 christos val = threadpool_idle_time_ms;
308 1.15.4.2 christos node.sysctl_data = &val;
309 1.15.4.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
310 1.15.4.2 christos if (error == 0 && newp != NULL) {
311 1.15.4.2 christos /* Disallow negative values and 0 (forever). */
312 1.15.4.2 christos if (val < 1)
313 1.15.4.2 christos error = EINVAL;
314 1.15.4.2 christos else
315 1.15.4.2 christos threadpool_idle_time_ms = val;
316 1.15.4.2 christos }
317 1.15.4.2 christos
318 1.15.4.2 christos return error;
319 1.15.4.2 christos }
320 1.15.4.2 christos
321 1.15.4.2 christos SYSCTL_SETUP_PROTO(sysctl_threadpool_setup);
322 1.15.4.2 christos
323 1.15.4.2 christos SYSCTL_SETUP(sysctl_threadpool_setup,
324 1.15.4.2 christos "sysctl kern.threadpool subtree setup")
325 1.15.4.2 christos {
326 1.15.4.2 christos const struct sysctlnode *rnode, *cnode;
327 1.15.4.2 christos int error __diagused;
328 1.15.4.2 christos
329 1.15.4.2 christos error = sysctl_createv(clog, 0, NULL, &rnode,
330 1.15.4.2 christos CTLFLAG_PERMANENT,
331 1.15.4.2 christos CTLTYPE_NODE, "threadpool",
332 1.15.4.2 christos SYSCTL_DESCR("threadpool subsystem options"),
333 1.15.4.2 christos NULL, 0, NULL, 0,
334 1.15.4.2 christos CTL_KERN, CTL_CREATE, CTL_EOL);
335 1.15.4.2 christos KASSERT(error == 0);
336 1.15.4.2 christos
337 1.15.4.2 christos error = sysctl_createv(clog, 0, &rnode, &cnode,
338 1.15.4.2 christos CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
339 1.15.4.2 christos CTLTYPE_INT, "idle_ms",
340 1.15.4.2 christos SYSCTL_DESCR("idle thread timeout in ms"),
341 1.15.4.2 christos sysctl_kern_threadpool_idle_ms, 0, NULL, 0,
342 1.15.4.2 christos CTL_CREATE, CTL_EOL);
343 1.15.4.2 christos KASSERT(error == 0);
344 1.15.4.2 christos }
345 1.15.4.2 christos
346 1.15.4.2 christos void
347 1.15.4.2 christos threadpools_init(void)
348 1.15.4.2 christos {
349 1.15.4.2 christos
350 1.15.4.2 christos threadpool_thread_pc =
351 1.15.4.2 christos pool_cache_init(sizeof(struct threadpool_thread), 0, 0, 0,
352 1.15.4.2 christos "thplthrd", NULL, IPL_NONE, NULL, NULL, NULL);
353 1.15.4.2 christos
354 1.15.4.2 christos LIST_INIT(&unbound_threadpools);
355 1.15.4.2 christos LIST_INIT(&percpu_threadpools);
356 1.15.4.2 christos mutex_init(&threadpools_lock, MUTEX_DEFAULT, IPL_NONE);
357 1.15.4.2 christos }
358 1.15.4.2 christos
359 1.15.4.2 christos /* Thread pool creation */
360 1.15.4.2 christos
361 1.15.4.2 christos static bool
362 1.15.4.2 christos threadpool_pri_is_valid(pri_t pri)
363 1.15.4.2 christos {
364 1.15.4.2 christos return (pri == PRI_NONE || (pri >= PRI_USER && pri < PRI_COUNT));
365 1.15.4.2 christos }
366 1.15.4.2 christos
367 1.15.4.2 christos static int
368 1.15.4.2 christos threadpool_create(struct threadpool *const pool, struct cpu_info *ci,
369 1.15.4.2 christos pri_t pri)
370 1.15.4.2 christos {
371 1.15.4.2 christos struct lwp *lwp;
372 1.15.4.2 christos int ktflags;
373 1.15.4.2 christos int error;
374 1.15.4.2 christos
375 1.15.4.2 christos KASSERT(threadpool_pri_is_valid(pri));
376 1.15.4.2 christos
377 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, create, ci, pri);
378 1.15.4.3 martin
379 1.15.4.2 christos mutex_init(&pool->tp_lock, MUTEX_DEFAULT, IPL_VM);
380 1.15.4.2 christos /* XXX overseer */
381 1.15.4.2 christos TAILQ_INIT(&pool->tp_jobs);
382 1.15.4.2 christos TAILQ_INIT(&pool->tp_idle_threads);
383 1.15.4.2 christos pool->tp_refcnt = 1; /* overseer's reference */
384 1.15.4.2 christos pool->tp_flags = 0;
385 1.15.4.2 christos pool->tp_cpu = ci;
386 1.15.4.2 christos pool->tp_pri = pri;
387 1.15.4.2 christos
388 1.15.4.2 christos pool->tp_overseer.tpt_lwp = NULL;
389 1.15.4.2 christos pool->tp_overseer.tpt_pool = pool;
390 1.15.4.2 christos pool->tp_overseer.tpt_job = NULL;
391 1.15.4.2 christos cv_init(&pool->tp_overseer.tpt_cv, "poolover");
392 1.15.4.2 christos
393 1.15.4.2 christos ktflags = 0;
394 1.15.4.2 christos ktflags |= KTHREAD_MPSAFE;
395 1.15.4.2 christos if (pri < PRI_KERNEL)
396 1.15.4.2 christos ktflags |= KTHREAD_TS;
397 1.15.4.2 christos error = kthread_create(pri, ktflags, ci, &threadpool_overseer_thread,
398 1.15.4.2 christos &pool->tp_overseer, &lwp,
399 1.15.4.2 christos "pooloverseer/%d@%d", (ci ? cpu_index(ci) : -1), (int)pri);
400 1.15.4.2 christos if (error)
401 1.15.4.2 christos goto fail0;
402 1.15.4.2 christos
403 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
404 1.15.4.2 christos pool->tp_overseer.tpt_lwp = lwp;
405 1.15.4.2 christos cv_broadcast(&pool->tp_overseer.tpt_cv);
406 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
407 1.15.4.2 christos
408 1.15.4.3 martin SDT_PROBE3(sdt, kernel, threadpool, create__success, ci, pri, pool);
409 1.15.4.2 christos return 0;
410 1.15.4.2 christos
411 1.15.4.2 christos fail0: KASSERT(error);
412 1.15.4.2 christos KASSERT(pool->tp_overseer.tpt_job == NULL);
413 1.15.4.2 christos KASSERT(pool->tp_overseer.tpt_pool == pool);
414 1.15.4.2 christos KASSERT(pool->tp_flags == 0);
415 1.15.4.2 christos KASSERT(pool->tp_refcnt == 0);
416 1.15.4.2 christos KASSERT(TAILQ_EMPTY(&pool->tp_idle_threads));
417 1.15.4.2 christos KASSERT(TAILQ_EMPTY(&pool->tp_jobs));
418 1.15.4.2 christos KASSERT(!cv_has_waiters(&pool->tp_overseer.tpt_cv));
419 1.15.4.2 christos cv_destroy(&pool->tp_overseer.tpt_cv);
420 1.15.4.2 christos mutex_destroy(&pool->tp_lock);
421 1.15.4.3 martin SDT_PROBE3(sdt, kernel, threadpool, create__failure, ci, pri, error);
422 1.15.4.2 christos return error;
423 1.15.4.2 christos }
424 1.15.4.2 christos
425 1.15.4.2 christos /* Thread pool destruction */
426 1.15.4.2 christos
427 1.15.4.2 christos static void
428 1.15.4.2 christos threadpool_destroy(struct threadpool *pool)
429 1.15.4.2 christos {
430 1.15.4.2 christos struct threadpool_thread *thread;
431 1.15.4.2 christos
432 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, destroy, pool);
433 1.15.4.3 martin
434 1.15.4.2 christos /* Mark the pool dying and wait for threads to commit suicide. */
435 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
436 1.15.4.2 christos KASSERT(TAILQ_EMPTY(&pool->tp_jobs));
437 1.15.4.2 christos pool->tp_flags |= THREADPOOL_DYING;
438 1.15.4.2 christos cv_broadcast(&pool->tp_overseer.tpt_cv);
439 1.15.4.2 christos TAILQ_FOREACH(thread, &pool->tp_idle_threads, tpt_entry)
440 1.15.4.2 christos cv_broadcast(&thread->tpt_cv);
441 1.15.4.2 christos while (0 < pool->tp_refcnt) {
442 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, destroy__wait,
443 1.15.4.3 martin pool, pool->tp_refcnt);
444 1.15.4.2 christos cv_wait(&pool->tp_overseer.tpt_cv, &pool->tp_lock);
445 1.15.4.2 christos }
446 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
447 1.15.4.2 christos
448 1.15.4.2 christos KASSERT(pool->tp_overseer.tpt_job == NULL);
449 1.15.4.2 christos KASSERT(pool->tp_overseer.tpt_pool == pool);
450 1.15.4.2 christos KASSERT(pool->tp_flags == THREADPOOL_DYING);
451 1.15.4.2 christos KASSERT(pool->tp_refcnt == 0);
452 1.15.4.2 christos KASSERT(TAILQ_EMPTY(&pool->tp_idle_threads));
453 1.15.4.2 christos KASSERT(TAILQ_EMPTY(&pool->tp_jobs));
454 1.15.4.2 christos KASSERT(!cv_has_waiters(&pool->tp_overseer.tpt_cv));
455 1.15.4.2 christos cv_destroy(&pool->tp_overseer.tpt_cv);
456 1.15.4.2 christos mutex_destroy(&pool->tp_lock);
457 1.15.4.2 christos }
458 1.15.4.2 christos
459 1.15.4.2 christos static void
460 1.15.4.2 christos threadpool_hold(struct threadpool *pool)
461 1.15.4.2 christos {
462 1.15.4.2 christos
463 1.15.4.2 christos KASSERT(mutex_owned(&pool->tp_lock));
464 1.15.4.2 christos pool->tp_refcnt++;
465 1.15.4.2 christos KASSERT(pool->tp_refcnt != 0);
466 1.15.4.2 christos }
467 1.15.4.2 christos
468 1.15.4.2 christos static void
469 1.15.4.2 christos threadpool_rele(struct threadpool *pool)
470 1.15.4.2 christos {
471 1.15.4.2 christos
472 1.15.4.2 christos KASSERT(mutex_owned(&pool->tp_lock));
473 1.15.4.2 christos KASSERT(0 < pool->tp_refcnt);
474 1.15.4.2 christos if (--pool->tp_refcnt == 0)
475 1.15.4.2 christos cv_broadcast(&pool->tp_overseer.tpt_cv);
476 1.15.4.2 christos }
477 1.15.4.2 christos
478 1.15.4.2 christos /* Unbound thread pools */
479 1.15.4.2 christos
480 1.15.4.2 christos int
481 1.15.4.2 christos threadpool_get(struct threadpool **poolp, pri_t pri)
482 1.15.4.2 christos {
483 1.15.4.2 christos struct threadpool_unbound *tpu, *tmp = NULL;
484 1.15.4.2 christos int error;
485 1.15.4.2 christos
486 1.15.4.2 christos ASSERT_SLEEPABLE();
487 1.15.4.2 christos
488 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, get, pri);
489 1.15.4.3 martin
490 1.15.4.2 christos if (! threadpool_pri_is_valid(pri))
491 1.15.4.2 christos return EINVAL;
492 1.15.4.2 christos
493 1.15.4.2 christos mutex_enter(&threadpools_lock);
494 1.15.4.2 christos tpu = threadpool_lookup_unbound(pri);
495 1.15.4.2 christos if (tpu == NULL) {
496 1.15.4.2 christos mutex_exit(&threadpools_lock);
497 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, get__create, pri);
498 1.15.4.2 christos tmp = kmem_zalloc(sizeof(*tmp), KM_SLEEP);
499 1.15.4.2 christos error = threadpool_create(&tmp->tpu_pool, NULL, pri);
500 1.15.4.2 christos if (error) {
501 1.15.4.2 christos kmem_free(tmp, sizeof(*tmp));
502 1.15.4.2 christos return error;
503 1.15.4.2 christos }
504 1.15.4.2 christos mutex_enter(&threadpools_lock);
505 1.15.4.2 christos tpu = threadpool_lookup_unbound(pri);
506 1.15.4.2 christos if (tpu == NULL) {
507 1.15.4.2 christos tpu = tmp;
508 1.15.4.2 christos tmp = NULL;
509 1.15.4.2 christos threadpool_insert_unbound(tpu);
510 1.15.4.3 martin } else {
511 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, get__race, pri);
512 1.15.4.2 christos }
513 1.15.4.2 christos }
514 1.15.4.2 christos KASSERT(tpu != NULL);
515 1.15.4.2 christos tpu->tpu_refcnt++;
516 1.15.4.2 christos KASSERT(tpu->tpu_refcnt != 0);
517 1.15.4.2 christos mutex_exit(&threadpools_lock);
518 1.15.4.2 christos
519 1.15.4.2 christos if (tmp != NULL) {
520 1.15.4.2 christos threadpool_destroy(&tmp->tpu_pool);
521 1.15.4.2 christos kmem_free(tmp, sizeof(*tmp));
522 1.15.4.2 christos }
523 1.15.4.2 christos KASSERT(tpu != NULL);
524 1.15.4.2 christos *poolp = &tpu->tpu_pool;
525 1.15.4.2 christos return 0;
526 1.15.4.2 christos }
527 1.15.4.2 christos
528 1.15.4.2 christos void
529 1.15.4.2 christos threadpool_put(struct threadpool *pool, pri_t pri)
530 1.15.4.2 christos {
531 1.15.4.2 christos struct threadpool_unbound *tpu =
532 1.15.4.2 christos container_of(pool, struct threadpool_unbound, tpu_pool);
533 1.15.4.2 christos
534 1.15.4.2 christos ASSERT_SLEEPABLE();
535 1.15.4.2 christos KASSERT(threadpool_pri_is_valid(pri));
536 1.15.4.2 christos
537 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, put, pool, pri);
538 1.15.4.3 martin
539 1.15.4.2 christos mutex_enter(&threadpools_lock);
540 1.15.4.2 christos KASSERT(tpu == threadpool_lookup_unbound(pri));
541 1.15.4.2 christos KASSERT(0 < tpu->tpu_refcnt);
542 1.15.4.2 christos if (--tpu->tpu_refcnt == 0) {
543 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, put__destroy, pool, pri);
544 1.15.4.2 christos threadpool_remove_unbound(tpu);
545 1.15.4.2 christos } else {
546 1.15.4.2 christos tpu = NULL;
547 1.15.4.2 christos }
548 1.15.4.2 christos mutex_exit(&threadpools_lock);
549 1.15.4.2 christos
550 1.15.4.2 christos if (tpu) {
551 1.15.4.2 christos threadpool_destroy(&tpu->tpu_pool);
552 1.15.4.2 christos kmem_free(tpu, sizeof(*tpu));
553 1.15.4.2 christos }
554 1.15.4.2 christos }
555 1.15.4.2 christos
556 1.15.4.2 christos /* Per-CPU thread pools */
557 1.15.4.2 christos
558 1.15.4.2 christos int
559 1.15.4.2 christos threadpool_percpu_get(struct threadpool_percpu **pool_percpup, pri_t pri)
560 1.15.4.2 christos {
561 1.15.4.2 christos struct threadpool_percpu *pool_percpu, *tmp = NULL;
562 1.15.4.2 christos int error;
563 1.15.4.2 christos
564 1.15.4.2 christos ASSERT_SLEEPABLE();
565 1.15.4.2 christos
566 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, percpu__get, pri);
567 1.15.4.3 martin
568 1.15.4.2 christos if (! threadpool_pri_is_valid(pri))
569 1.15.4.2 christos return EINVAL;
570 1.15.4.2 christos
571 1.15.4.2 christos mutex_enter(&threadpools_lock);
572 1.15.4.2 christos pool_percpu = threadpool_lookup_percpu(pri);
573 1.15.4.2 christos if (pool_percpu == NULL) {
574 1.15.4.2 christos mutex_exit(&threadpools_lock);
575 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, percpu__get__create, pri);
576 1.15.4.2 christos error = threadpool_percpu_create(&tmp, pri);
577 1.15.4.2 christos if (error)
578 1.15.4.2 christos return error;
579 1.15.4.2 christos KASSERT(tmp != NULL);
580 1.15.4.2 christos mutex_enter(&threadpools_lock);
581 1.15.4.2 christos pool_percpu = threadpool_lookup_percpu(pri);
582 1.15.4.2 christos if (pool_percpu == NULL) {
583 1.15.4.2 christos pool_percpu = tmp;
584 1.15.4.2 christos tmp = NULL;
585 1.15.4.2 christos threadpool_insert_percpu(pool_percpu);
586 1.15.4.3 martin } else {
587 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, percpu__get__race,
588 1.15.4.3 martin pri);
589 1.15.4.2 christos }
590 1.15.4.2 christos }
591 1.15.4.2 christos KASSERT(pool_percpu != NULL);
592 1.15.4.2 christos pool_percpu->tpp_refcnt++;
593 1.15.4.2 christos KASSERT(pool_percpu->tpp_refcnt != 0);
594 1.15.4.2 christos mutex_exit(&threadpools_lock);
595 1.15.4.2 christos
596 1.15.4.2 christos if (tmp != NULL)
597 1.15.4.2 christos threadpool_percpu_destroy(tmp);
598 1.15.4.2 christos KASSERT(pool_percpu != NULL);
599 1.15.4.2 christos *pool_percpup = pool_percpu;
600 1.15.4.2 christos return 0;
601 1.15.4.2 christos }
602 1.15.4.2 christos
603 1.15.4.2 christos void
604 1.15.4.2 christos threadpool_percpu_put(struct threadpool_percpu *pool_percpu, pri_t pri)
605 1.15.4.2 christos {
606 1.15.4.2 christos
607 1.15.4.2 christos ASSERT_SLEEPABLE();
608 1.15.4.2 christos
609 1.15.4.2 christos KASSERT(threadpool_pri_is_valid(pri));
610 1.15.4.2 christos
611 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, percpu__put, pool_percpu, pri);
612 1.15.4.3 martin
613 1.15.4.2 christos mutex_enter(&threadpools_lock);
614 1.15.4.2 christos KASSERT(pool_percpu == threadpool_lookup_percpu(pri));
615 1.15.4.2 christos KASSERT(0 < pool_percpu->tpp_refcnt);
616 1.15.4.2 christos if (--pool_percpu->tpp_refcnt == 0) {
617 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, percpu__put__destroy,
618 1.15.4.3 martin pool_percpu, pri);
619 1.15.4.2 christos threadpool_remove_percpu(pool_percpu);
620 1.15.4.2 christos } else {
621 1.15.4.2 christos pool_percpu = NULL;
622 1.15.4.2 christos }
623 1.15.4.2 christos mutex_exit(&threadpools_lock);
624 1.15.4.2 christos
625 1.15.4.2 christos if (pool_percpu)
626 1.15.4.2 christos threadpool_percpu_destroy(pool_percpu);
627 1.15.4.2 christos }
628 1.15.4.2 christos
629 1.15.4.2 christos struct threadpool *
630 1.15.4.2 christos threadpool_percpu_ref(struct threadpool_percpu *pool_percpu)
631 1.15.4.2 christos {
632 1.15.4.2 christos struct threadpool **poolp, *pool;
633 1.15.4.2 christos
634 1.15.4.2 christos poolp = percpu_getref(pool_percpu->tpp_percpu);
635 1.15.4.2 christos pool = *poolp;
636 1.15.4.2 christos percpu_putref(pool_percpu->tpp_percpu);
637 1.15.4.2 christos
638 1.15.4.2 christos return pool;
639 1.15.4.2 christos }
640 1.15.4.2 christos
641 1.15.4.2 christos struct threadpool *
642 1.15.4.2 christos threadpool_percpu_ref_remote(struct threadpool_percpu *pool_percpu,
643 1.15.4.2 christos struct cpu_info *ci)
644 1.15.4.2 christos {
645 1.15.4.2 christos struct threadpool **poolp, *pool;
646 1.15.4.2 christos
647 1.15.4.2 christos percpu_traverse_enter();
648 1.15.4.2 christos poolp = percpu_getptr_remote(pool_percpu->tpp_percpu, ci);
649 1.15.4.2 christos pool = *poolp;
650 1.15.4.2 christos percpu_traverse_exit();
651 1.15.4.2 christos
652 1.15.4.2 christos return pool;
653 1.15.4.2 christos }
654 1.15.4.2 christos
655 1.15.4.2 christos static int
656 1.15.4.2 christos threadpool_percpu_create(struct threadpool_percpu **pool_percpup, pri_t pri)
657 1.15.4.2 christos {
658 1.15.4.2 christos struct threadpool_percpu *pool_percpu;
659 1.15.4.3 martin bool ok = true;
660 1.15.4.2 christos
661 1.15.4.2 christos pool_percpu = kmem_zalloc(sizeof(*pool_percpu), KM_SLEEP);
662 1.15.4.2 christos pool_percpu->tpp_pri = pri;
663 1.15.4.3 martin pool_percpu->tpp_percpu = percpu_create(sizeof(struct threadpool *),
664 1.15.4.3 martin threadpool_percpu_init, threadpool_percpu_fini,
665 1.15.4.3 martin (void *)(intptr_t)pri);
666 1.15.4.2 christos
667 1.15.4.3 martin /*
668 1.15.4.3 martin * Verify that all of the CPUs were initialized.
669 1.15.4.3 martin *
670 1.15.4.3 martin * XXX What to do if we add CPU hotplug?
671 1.15.4.3 martin */
672 1.15.4.3 martin percpu_foreach(pool_percpu->tpp_percpu, &threadpool_percpu_ok, &ok);
673 1.15.4.3 martin if (!ok)
674 1.15.4.3 martin goto fail;
675 1.15.4.2 christos
676 1.15.4.2 christos /* Success! */
677 1.15.4.2 christos *pool_percpup = (struct threadpool_percpu *)pool_percpu;
678 1.15.4.2 christos return 0;
679 1.15.4.2 christos
680 1.15.4.3 martin fail: percpu_free(pool_percpu->tpp_percpu, sizeof(struct threadpool *));
681 1.15.4.3 martin kmem_free(pool_percpu, sizeof(*pool_percpu));
682 1.15.4.3 martin return ENOMEM;
683 1.15.4.2 christos }
684 1.15.4.2 christos
685 1.15.4.2 christos static void
686 1.15.4.2 christos threadpool_percpu_destroy(struct threadpool_percpu *pool_percpu)
687 1.15.4.2 christos {
688 1.15.4.2 christos
689 1.15.4.2 christos percpu_free(pool_percpu->tpp_percpu, sizeof(struct threadpool *));
690 1.15.4.2 christos kmem_free(pool_percpu, sizeof(*pool_percpu));
691 1.15.4.2 christos }
692 1.15.4.2 christos
693 1.15.4.3 martin static void
694 1.15.4.3 martin threadpool_percpu_init(void *vpoolp, void *vpri, struct cpu_info *ci)
695 1.15.4.3 martin {
696 1.15.4.3 martin struct threadpool **const poolp = vpoolp;
697 1.15.4.3 martin pri_t pri = (intptr_t)(void *)vpri;
698 1.15.4.3 martin int error;
699 1.15.4.3 martin
700 1.15.4.3 martin *poolp = kmem_zalloc(sizeof(**poolp), KM_SLEEP);
701 1.15.4.3 martin error = threadpool_create(*poolp, ci, pri);
702 1.15.4.3 martin if (error) {
703 1.15.4.3 martin KASSERT(error == ENOMEM);
704 1.15.4.3 martin kmem_free(*poolp, sizeof(**poolp));
705 1.15.4.3 martin *poolp = NULL;
706 1.15.4.3 martin }
707 1.15.4.3 martin }
708 1.15.4.3 martin
709 1.15.4.3 martin static void
710 1.15.4.3 martin threadpool_percpu_ok(void *vpoolp, void *vokp, struct cpu_info *ci)
711 1.15.4.3 martin {
712 1.15.4.3 martin struct threadpool **const poolp = vpoolp;
713 1.15.4.3 martin bool *okp = vokp;
714 1.15.4.3 martin
715 1.15.4.3 martin if (*poolp == NULL)
716 1.15.4.3 martin atomic_store_relaxed(okp, false);
717 1.15.4.3 martin }
718 1.15.4.3 martin
719 1.15.4.3 martin static void
720 1.15.4.3 martin threadpool_percpu_fini(void *vpoolp, void *vprip, struct cpu_info *ci)
721 1.15.4.3 martin {
722 1.15.4.3 martin struct threadpool **const poolp = vpoolp;
723 1.15.4.3 martin
724 1.15.4.3 martin if (*poolp == NULL) /* initialization failed */
725 1.15.4.3 martin return;
726 1.15.4.3 martin threadpool_destroy(*poolp);
727 1.15.4.3 martin kmem_free(*poolp, sizeof(**poolp));
728 1.15.4.3 martin }
729 1.15.4.3 martin
730 1.15.4.2 christos /* Thread pool jobs */
731 1.15.4.2 christos
732 1.15.4.2 christos void __printflike(4,5)
733 1.15.4.2 christos threadpool_job_init(struct threadpool_job *job, threadpool_job_fn_t fn,
734 1.15.4.2 christos kmutex_t *lock, const char *fmt, ...)
735 1.15.4.2 christos {
736 1.15.4.2 christos va_list ap;
737 1.15.4.2 christos
738 1.15.4.2 christos va_start(ap, fmt);
739 1.15.4.2 christos (void)vsnprintf(job->job_name, sizeof(job->job_name), fmt, ap);
740 1.15.4.2 christos va_end(ap);
741 1.15.4.2 christos
742 1.15.4.2 christos job->job_lock = lock;
743 1.15.4.2 christos job->job_thread = NULL;
744 1.15.4.2 christos job->job_refcnt = 0;
745 1.15.4.2 christos cv_init(&job->job_cv, job->job_name);
746 1.15.4.2 christos job->job_fn = fn;
747 1.15.4.2 christos }
748 1.15.4.2 christos
749 1.15.4.2 christos static void
750 1.15.4.2 christos threadpool_job_dead(struct threadpool_job *job)
751 1.15.4.2 christos {
752 1.15.4.2 christos
753 1.15.4.2 christos panic("threadpool job %p ran after destruction", job);
754 1.15.4.2 christos }
755 1.15.4.2 christos
756 1.15.4.2 christos void
757 1.15.4.2 christos threadpool_job_destroy(struct threadpool_job *job)
758 1.15.4.2 christos {
759 1.15.4.2 christos
760 1.15.4.2 christos ASSERT_SLEEPABLE();
761 1.15.4.2 christos
762 1.15.4.2 christos KASSERTMSG((job->job_thread == NULL), "job %p still running", job);
763 1.15.4.2 christos
764 1.15.4.2 christos mutex_enter(job->job_lock);
765 1.15.4.2 christos while (0 < job->job_refcnt)
766 1.15.4.2 christos cv_wait(&job->job_cv, job->job_lock);
767 1.15.4.2 christos mutex_exit(job->job_lock);
768 1.15.4.2 christos
769 1.15.4.2 christos job->job_lock = NULL;
770 1.15.4.2 christos KASSERT(job->job_thread == NULL);
771 1.15.4.2 christos KASSERT(job->job_refcnt == 0);
772 1.15.4.2 christos KASSERT(!cv_has_waiters(&job->job_cv));
773 1.15.4.2 christos cv_destroy(&job->job_cv);
774 1.15.4.2 christos job->job_fn = threadpool_job_dead;
775 1.15.4.2 christos (void)strlcpy(job->job_name, "deadjob", sizeof(job->job_name));
776 1.15.4.2 christos }
777 1.15.4.2 christos
778 1.15.4.2 christos static void
779 1.15.4.2 christos threadpool_job_hold(struct threadpool_job *job)
780 1.15.4.2 christos {
781 1.15.4.2 christos unsigned int refcnt;
782 1.15.4.2 christos
783 1.15.4.2 christos do {
784 1.15.4.2 christos refcnt = job->job_refcnt;
785 1.15.4.2 christos KASSERT(refcnt != UINT_MAX);
786 1.15.4.2 christos } while (atomic_cas_uint(&job->job_refcnt, refcnt, (refcnt + 1))
787 1.15.4.2 christos != refcnt);
788 1.15.4.2 christos }
789 1.15.4.2 christos
790 1.15.4.2 christos static void
791 1.15.4.2 christos threadpool_job_rele(struct threadpool_job *job)
792 1.15.4.2 christos {
793 1.15.4.2 christos unsigned int refcnt;
794 1.15.4.2 christos
795 1.15.4.2 christos KASSERT(mutex_owned(job->job_lock));
796 1.15.4.2 christos
797 1.15.4.2 christos do {
798 1.15.4.2 christos refcnt = job->job_refcnt;
799 1.15.4.2 christos KASSERT(0 < refcnt);
800 1.15.4.2 christos if (refcnt == 1) {
801 1.15.4.2 christos refcnt = atomic_dec_uint_nv(&job->job_refcnt);
802 1.15.4.2 christos KASSERT(refcnt != UINT_MAX);
803 1.15.4.2 christos if (refcnt == 0)
804 1.15.4.2 christos cv_broadcast(&job->job_cv);
805 1.15.4.2 christos return;
806 1.15.4.2 christos }
807 1.15.4.2 christos } while (atomic_cas_uint(&job->job_refcnt, refcnt, (refcnt - 1))
808 1.15.4.2 christos != refcnt);
809 1.15.4.2 christos }
810 1.15.4.2 christos
811 1.15.4.2 christos void
812 1.15.4.2 christos threadpool_job_done(struct threadpool_job *job)
813 1.15.4.2 christos {
814 1.15.4.2 christos
815 1.15.4.2 christos KASSERT(mutex_owned(job->job_lock));
816 1.15.4.2 christos KASSERT(job->job_thread != NULL);
817 1.15.4.2 christos KASSERT(job->job_thread->tpt_lwp == curlwp);
818 1.15.4.2 christos
819 1.15.4.2 christos /*
820 1.15.4.2 christos * We can safely read this field; it's only modified right before
821 1.15.4.2 christos * we call the job work function, and we are only preserving it
822 1.15.4.2 christos * to use here; no one cares if it contains junk afterward.
823 1.15.4.2 christos */
824 1.15.4.2 christos lwp_lock(curlwp);
825 1.15.4.2 christos curlwp->l_name = job->job_thread->tpt_lwp_savedname;
826 1.15.4.2 christos lwp_unlock(curlwp);
827 1.15.4.2 christos
828 1.15.4.2 christos /*
829 1.15.4.2 christos * Inline the work of threadpool_job_rele(); the job is already
830 1.15.4.2 christos * locked, the most likely scenario (XXXJRT only scenario?) is
831 1.15.4.2 christos * that we're dropping the last reference (the one taken in
832 1.15.4.2 christos * threadpool_schedule_job()), and we always do the cv_broadcast()
833 1.15.4.2 christos * anyway.
834 1.15.4.2 christos */
835 1.15.4.2 christos KASSERT(0 < job->job_refcnt);
836 1.15.4.2 christos unsigned int refcnt __diagused = atomic_dec_uint_nv(&job->job_refcnt);
837 1.15.4.2 christos KASSERT(refcnt != UINT_MAX);
838 1.15.4.2 christos cv_broadcast(&job->job_cv);
839 1.15.4.2 christos job->job_thread = NULL;
840 1.15.4.2 christos }
841 1.15.4.2 christos
842 1.15.4.2 christos void
843 1.15.4.2 christos threadpool_schedule_job(struct threadpool *pool, struct threadpool_job *job)
844 1.15.4.2 christos {
845 1.15.4.2 christos
846 1.15.4.2 christos KASSERT(mutex_owned(job->job_lock));
847 1.15.4.2 christos
848 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, schedule__job, pool, job);
849 1.15.4.3 martin
850 1.15.4.2 christos /*
851 1.15.4.2 christos * If the job's already running, let it keep running. The job
852 1.15.4.2 christos * is guaranteed by the interlock not to end early -- if it had
853 1.15.4.2 christos * ended early, threadpool_job_done would have set job_thread
854 1.15.4.2 christos * to NULL under the interlock.
855 1.15.4.2 christos */
856 1.15.4.2 christos if (__predict_true(job->job_thread != NULL)) {
857 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, schedule__job__running,
858 1.15.4.3 martin pool, job);
859 1.15.4.2 christos return;
860 1.15.4.2 christos }
861 1.15.4.2 christos
862 1.15.4.2 christos threadpool_job_hold(job);
863 1.15.4.2 christos
864 1.15.4.2 christos /* Otherwise, try to assign a thread to the job. */
865 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
866 1.15.4.2 christos if (__predict_false(TAILQ_EMPTY(&pool->tp_idle_threads))) {
867 1.15.4.2 christos /* Nobody's idle. Give it to the overseer. */
868 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, schedule__job__overseer,
869 1.15.4.3 martin pool, job);
870 1.15.4.2 christos job->job_thread = &pool->tp_overseer;
871 1.15.4.2 christos TAILQ_INSERT_TAIL(&pool->tp_jobs, job, job_entry);
872 1.15.4.2 christos } else {
873 1.15.4.2 christos /* Assign it to the first idle thread. */
874 1.15.4.2 christos job->job_thread = TAILQ_FIRST(&pool->tp_idle_threads);
875 1.15.4.3 martin SDT_PROBE3(sdt, kernel, threadpool, schedule__job__thread,
876 1.15.4.3 martin pool, job, job->job_thread->tpt_lwp);
877 1.15.4.2 christos TAILQ_REMOVE(&pool->tp_idle_threads, job->job_thread,
878 1.15.4.2 christos tpt_entry);
879 1.15.4.2 christos job->job_thread->tpt_job = job;
880 1.15.4.2 christos }
881 1.15.4.2 christos
882 1.15.4.2 christos /* Notify whomever we gave it to, overseer or idle thread. */
883 1.15.4.2 christos KASSERT(job->job_thread != NULL);
884 1.15.4.2 christos cv_broadcast(&job->job_thread->tpt_cv);
885 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
886 1.15.4.2 christos }
887 1.15.4.2 christos
888 1.15.4.2 christos bool
889 1.15.4.2 christos threadpool_cancel_job_async(struct threadpool *pool, struct threadpool_job *job)
890 1.15.4.2 christos {
891 1.15.4.2 christos
892 1.15.4.2 christos KASSERT(mutex_owned(job->job_lock));
893 1.15.4.2 christos
894 1.15.4.2 christos /*
895 1.15.4.2 christos * XXXJRT This fails (albeit safely) when all of the following
896 1.15.4.2 christos * are true:
897 1.15.4.2 christos *
898 1.15.4.2 christos * => "pool" is something other than what the job was
899 1.15.4.2 christos * scheduled on. This can legitimately occur if,
900 1.15.4.2 christos * for example, a job is percpu-scheduled on CPU0
901 1.15.4.2 christos * and then CPU1 attempts to cancel it without taking
902 1.15.4.2 christos * a remote pool reference. (this might happen by
903 1.15.4.2 christos * "luck of the draw").
904 1.15.4.2 christos *
905 1.15.4.2 christos * => "job" is not yet running, but is assigned to the
906 1.15.4.2 christos * overseer.
907 1.15.4.2 christos *
908 1.15.4.2 christos * When this happens, this code makes the determination that
909 1.15.4.2 christos * the job is already running. The failure mode is that the
910 1.15.4.2 christos * caller is told the job is running, and thus has to wait.
911 1.15.4.2 christos * The overseer will eventually get to it and the job will
912 1.15.4.2 christos * proceed as if it had been already running.
913 1.15.4.2 christos */
914 1.15.4.2 christos
915 1.15.4.2 christos if (job->job_thread == NULL) {
916 1.15.4.2 christos /* Nothing to do. Guaranteed not running. */
917 1.15.4.2 christos return true;
918 1.15.4.2 christos } else if (job->job_thread == &pool->tp_overseer) {
919 1.15.4.2 christos /* Take it off the list to guarantee it won't run. */
920 1.15.4.2 christos job->job_thread = NULL;
921 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
922 1.15.4.2 christos TAILQ_REMOVE(&pool->tp_jobs, job, job_entry);
923 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
924 1.15.4.2 christos threadpool_job_rele(job);
925 1.15.4.2 christos return true;
926 1.15.4.2 christos } else {
927 1.15.4.2 christos /* Too late -- already running. */
928 1.15.4.2 christos return false;
929 1.15.4.2 christos }
930 1.15.4.2 christos }
931 1.15.4.2 christos
932 1.15.4.2 christos void
933 1.15.4.2 christos threadpool_cancel_job(struct threadpool *pool, struct threadpool_job *job)
934 1.15.4.2 christos {
935 1.15.4.2 christos
936 1.15.4.2 christos ASSERT_SLEEPABLE();
937 1.15.4.2 christos
938 1.15.4.2 christos KASSERT(mutex_owned(job->job_lock));
939 1.15.4.2 christos
940 1.15.4.2 christos if (threadpool_cancel_job_async(pool, job))
941 1.15.4.2 christos return;
942 1.15.4.2 christos
943 1.15.4.2 christos /* Already running. Wait for it to complete. */
944 1.15.4.2 christos while (job->job_thread != NULL)
945 1.15.4.2 christos cv_wait(&job->job_cv, job->job_lock);
946 1.15.4.2 christos }
947 1.15.4.2 christos
948 1.15.4.2 christos /* Thread pool overseer thread */
949 1.15.4.2 christos
950 1.15.4.2 christos static void __dead
951 1.15.4.2 christos threadpool_overseer_thread(void *arg)
952 1.15.4.2 christos {
953 1.15.4.2 christos struct threadpool_thread *const overseer = arg;
954 1.15.4.2 christos struct threadpool *const pool = overseer->tpt_pool;
955 1.15.4.2 christos struct lwp *lwp = NULL;
956 1.15.4.2 christos int ktflags;
957 1.15.4.2 christos int error;
958 1.15.4.2 christos
959 1.15.4.2 christos KASSERT((pool->tp_cpu == NULL) || (pool->tp_cpu == curcpu()));
960 1.15.4.3 martin KASSERT((pool->tp_cpu == NULL) || (curlwp->l_pflag & LP_BOUND));
961 1.15.4.2 christos
962 1.15.4.2 christos /* Wait until we're initialized. */
963 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
964 1.15.4.2 christos while (overseer->tpt_lwp == NULL)
965 1.15.4.2 christos cv_wait(&overseer->tpt_cv, &pool->tp_lock);
966 1.15.4.2 christos
967 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, overseer__start, pool);
968 1.15.4.2 christos
969 1.15.4.2 christos for (;;) {
970 1.15.4.2 christos /* Wait until there's a job. */
971 1.15.4.2 christos while (TAILQ_EMPTY(&pool->tp_jobs)) {
972 1.15.4.2 christos if (ISSET(pool->tp_flags, THREADPOOL_DYING)) {
973 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool,
974 1.15.4.3 martin overseer__dying, pool);
975 1.15.4.2 christos break;
976 1.15.4.2 christos }
977 1.15.4.2 christos cv_wait(&overseer->tpt_cv, &pool->tp_lock);
978 1.15.4.2 christos }
979 1.15.4.2 christos if (__predict_false(TAILQ_EMPTY(&pool->tp_jobs)))
980 1.15.4.2 christos break;
981 1.15.4.2 christos
982 1.15.4.2 christos /* If there are no threads, we'll have to try to start one. */
983 1.15.4.2 christos if (TAILQ_EMPTY(&pool->tp_idle_threads)) {
984 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, overseer__spawn,
985 1.15.4.3 martin pool);
986 1.15.4.2 christos threadpool_hold(pool);
987 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
988 1.15.4.2 christos
989 1.15.4.2 christos struct threadpool_thread *const thread =
990 1.15.4.2 christos pool_cache_get(threadpool_thread_pc, PR_WAITOK);
991 1.15.4.2 christos thread->tpt_lwp = NULL;
992 1.15.4.2 christos thread->tpt_pool = pool;
993 1.15.4.2 christos thread->tpt_job = NULL;
994 1.15.4.2 christos cv_init(&thread->tpt_cv, "poolthrd");
995 1.15.4.2 christos
996 1.15.4.2 christos ktflags = 0;
997 1.15.4.2 christos ktflags |= KTHREAD_MPSAFE;
998 1.15.4.2 christos if (pool->tp_pri < PRI_KERNEL)
999 1.15.4.2 christos ktflags |= KTHREAD_TS;
1000 1.15.4.2 christos error = kthread_create(pool->tp_pri, ktflags,
1001 1.15.4.2 christos pool->tp_cpu, &threadpool_thread, thread, &lwp,
1002 1.15.4.2 christos "poolthread/%d@%d",
1003 1.15.4.2 christos (pool->tp_cpu ? cpu_index(pool->tp_cpu) : -1),
1004 1.15.4.2 christos (int)pool->tp_pri);
1005 1.15.4.2 christos
1006 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
1007 1.15.4.2 christos if (error) {
1008 1.15.4.2 christos pool_cache_put(threadpool_thread_pc, thread);
1009 1.15.4.2 christos threadpool_rele(pool);
1010 1.15.4.2 christos /* XXX What to do to wait for memory? */
1011 1.15.4.2 christos (void)kpause("thrdplcr", false, hz,
1012 1.15.4.2 christos &pool->tp_lock);
1013 1.15.4.2 christos continue;
1014 1.15.4.2 christos }
1015 1.15.4.2 christos /*
1016 1.15.4.2 christos * New kthread now owns the reference to the pool
1017 1.15.4.2 christos * taken above.
1018 1.15.4.2 christos */
1019 1.15.4.2 christos KASSERT(lwp != NULL);
1020 1.15.4.2 christos TAILQ_INSERT_TAIL(&pool->tp_idle_threads, thread,
1021 1.15.4.2 christos tpt_entry);
1022 1.15.4.2 christos thread->tpt_lwp = lwp;
1023 1.15.4.2 christos lwp = NULL;
1024 1.15.4.2 christos cv_broadcast(&thread->tpt_cv);
1025 1.15.4.2 christos continue;
1026 1.15.4.2 christos }
1027 1.15.4.2 christos
1028 1.15.4.2 christos /* There are idle threads, so try giving one a job. */
1029 1.15.4.2 christos struct threadpool_job *const job = TAILQ_FIRST(&pool->tp_jobs);
1030 1.15.4.2 christos TAILQ_REMOVE(&pool->tp_jobs, job, job_entry);
1031 1.15.4.2 christos /*
1032 1.15.4.2 christos * Take an extra reference on the job temporarily so that
1033 1.15.4.2 christos * it won't disappear on us while we have both locks dropped.
1034 1.15.4.2 christos */
1035 1.15.4.2 christos threadpool_job_hold(job);
1036 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
1037 1.15.4.2 christos
1038 1.15.4.2 christos mutex_enter(job->job_lock);
1039 1.15.4.2 christos /* If the job was cancelled, we'll no longer be its thread. */
1040 1.15.4.2 christos if (__predict_true(job->job_thread == overseer)) {
1041 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
1042 1.15.4.2 christos if (__predict_false(
1043 1.15.4.2 christos TAILQ_EMPTY(&pool->tp_idle_threads))) {
1044 1.15.4.2 christos /*
1045 1.15.4.2 christos * Someone else snagged the thread
1046 1.15.4.2 christos * first. We'll have to try again.
1047 1.15.4.2 christos */
1048 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool,
1049 1.15.4.3 martin overseer__race, pool, job);
1050 1.15.4.2 christos TAILQ_INSERT_HEAD(&pool->tp_jobs, job,
1051 1.15.4.2 christos job_entry);
1052 1.15.4.2 christos } else {
1053 1.15.4.2 christos /*
1054 1.15.4.2 christos * Assign the job to the thread and
1055 1.15.4.2 christos * wake the thread so it starts work.
1056 1.15.4.2 christos */
1057 1.15.4.2 christos struct threadpool_thread *const thread =
1058 1.15.4.2 christos TAILQ_FIRST(&pool->tp_idle_threads);
1059 1.15.4.2 christos
1060 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool,
1061 1.15.4.3 martin overseer__assign, job, thread->tpt_lwp);
1062 1.15.4.2 christos KASSERT(thread->tpt_job == NULL);
1063 1.15.4.2 christos TAILQ_REMOVE(&pool->tp_idle_threads, thread,
1064 1.15.4.2 christos tpt_entry);
1065 1.15.4.2 christos thread->tpt_job = job;
1066 1.15.4.2 christos job->job_thread = thread;
1067 1.15.4.2 christos cv_broadcast(&thread->tpt_cv);
1068 1.15.4.2 christos }
1069 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
1070 1.15.4.2 christos }
1071 1.15.4.2 christos threadpool_job_rele(job);
1072 1.15.4.2 christos mutex_exit(job->job_lock);
1073 1.15.4.2 christos
1074 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
1075 1.15.4.2 christos }
1076 1.15.4.2 christos threadpool_rele(pool);
1077 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
1078 1.15.4.2 christos
1079 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, overseer__exit, pool);
1080 1.15.4.2 christos
1081 1.15.4.2 christos kthread_exit(0);
1082 1.15.4.2 christos }
1083 1.15.4.2 christos
1084 1.15.4.2 christos /* Thread pool thread */
1085 1.15.4.2 christos
1086 1.15.4.2 christos static void __dead
1087 1.15.4.2 christos threadpool_thread(void *arg)
1088 1.15.4.2 christos {
1089 1.15.4.2 christos struct threadpool_thread *const thread = arg;
1090 1.15.4.2 christos struct threadpool *const pool = thread->tpt_pool;
1091 1.15.4.2 christos
1092 1.15.4.2 christos KASSERT((pool->tp_cpu == NULL) || (pool->tp_cpu == curcpu()));
1093 1.15.4.3 martin KASSERT((pool->tp_cpu == NULL) || (curlwp->l_pflag & LP_BOUND));
1094 1.15.4.2 christos
1095 1.15.4.2 christos /* Wait until we're initialized and on the queue. */
1096 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
1097 1.15.4.2 christos while (thread->tpt_lwp == NULL)
1098 1.15.4.2 christos cv_wait(&thread->tpt_cv, &pool->tp_lock);
1099 1.15.4.2 christos
1100 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, thread__start, pool);
1101 1.15.4.2 christos
1102 1.15.4.2 christos KASSERT(thread->tpt_lwp == curlwp);
1103 1.15.4.2 christos for (;;) {
1104 1.15.4.2 christos /* Wait until we are assigned a job. */
1105 1.15.4.2 christos while (thread->tpt_job == NULL) {
1106 1.15.4.2 christos if (ISSET(pool->tp_flags, THREADPOOL_DYING)) {
1107 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool,
1108 1.15.4.3 martin thread__dying, pool);
1109 1.15.4.2 christos break;
1110 1.15.4.2 christos }
1111 1.15.4.2 christos if (cv_timedwait(&thread->tpt_cv, &pool->tp_lock,
1112 1.15.4.2 christos mstohz(threadpool_idle_time_ms)))
1113 1.15.4.2 christos break;
1114 1.15.4.2 christos }
1115 1.15.4.2 christos if (__predict_false(thread->tpt_job == NULL)) {
1116 1.15.4.2 christos TAILQ_REMOVE(&pool->tp_idle_threads, thread,
1117 1.15.4.2 christos tpt_entry);
1118 1.15.4.2 christos break;
1119 1.15.4.2 christos }
1120 1.15.4.2 christos
1121 1.15.4.2 christos struct threadpool_job *const job = thread->tpt_job;
1122 1.15.4.2 christos KASSERT(job != NULL);
1123 1.15.4.2 christos
1124 1.15.4.2 christos /* Set our lwp name to reflect what job we're doing. */
1125 1.15.4.2 christos lwp_lock(curlwp);
1126 1.15.4.2 christos char *const lwp_name __diagused = curlwp->l_name;
1127 1.15.4.2 christos thread->tpt_lwp_savedname = curlwp->l_name;
1128 1.15.4.2 christos curlwp->l_name = job->job_name;
1129 1.15.4.2 christos lwp_unlock(curlwp);
1130 1.15.4.2 christos
1131 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
1132 1.15.4.2 christos
1133 1.15.4.3 martin SDT_PROBE2(sdt, kernel, threadpool, thread__job, pool, job);
1134 1.15.4.2 christos
1135 1.15.4.2 christos /* Run the job. */
1136 1.15.4.2 christos (*job->job_fn)(job);
1137 1.15.4.2 christos
1138 1.15.4.2 christos /* lwp name restored in threadpool_job_done(). */
1139 1.15.4.2 christos KASSERTMSG((curlwp->l_name == lwp_name),
1140 1.15.4.2 christos "someone forgot to call threadpool_job_done()!");
1141 1.15.4.2 christos
1142 1.15.4.2 christos /*
1143 1.15.4.2 christos * We can compare pointers, but we can no longer deference
1144 1.15.4.2 christos * job after this because threadpool_job_done() drops the
1145 1.15.4.2 christos * last reference on the job while the job is locked.
1146 1.15.4.2 christos */
1147 1.15.4.2 christos
1148 1.15.4.2 christos mutex_spin_enter(&pool->tp_lock);
1149 1.15.4.2 christos KASSERT(thread->tpt_job == job);
1150 1.15.4.2 christos thread->tpt_job = NULL;
1151 1.15.4.2 christos TAILQ_INSERT_TAIL(&pool->tp_idle_threads, thread, tpt_entry);
1152 1.15.4.2 christos }
1153 1.15.4.2 christos threadpool_rele(pool);
1154 1.15.4.2 christos mutex_spin_exit(&pool->tp_lock);
1155 1.15.4.2 christos
1156 1.15.4.3 martin SDT_PROBE1(sdt, kernel, threadpool, thread__exit, pool);
1157 1.15.4.2 christos
1158 1.15.4.2 christos KASSERT(!cv_has_waiters(&thread->tpt_cv));
1159 1.15.4.2 christos cv_destroy(&thread->tpt_cv);
1160 1.15.4.2 christos pool_cache_put(threadpool_thread_pc, thread);
1161 1.15.4.2 christos kthread_exit(0);
1162 1.15.4.2 christos }
1163