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