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