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