4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
29 #include <sys/zfs_context.h>
32 taskq_t *system_taskq;
34 #define TASKQ_ACTIVE 0x00010000
38 krwlock_t tq_threadlock;
39 kcondvar_t tq_dispatch_cv;
40 kcondvar_t tq_wait_cv;
41 thread_t *tq_threadlist;
48 kcondvar_t tq_maxalloc_cv;
50 taskq_ent_t *tq_freelist;
55 task_alloc(taskq_t *tq, int tqflags)
60 again: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
61 tq->tq_freelist = t->tqent_next;
63 if (tq->tq_nalloc >= tq->tq_maxalloc) {
64 if (!(tqflags & KM_SLEEP))
68 * We don't want to exceed tq_maxalloc, but we can't
69 * wait for other tasks to complete (and thus free up
70 * task structures) without risking deadlock with
71 * the caller. So, we just delay for one second
72 * to throttle the allocation rate. If we have tasks
73 * complete before one second timeout expires then
74 * taskq_ent_free will signal us and we will
75 * immediately retry the allocation.
77 tq->tq_maxalloc_wait++;
78 rv = cv_timedwait(&tq->tq_maxalloc_cv,
79 &tq->tq_lock, ddi_get_lbolt() + hz);
80 tq->tq_maxalloc_wait--;
82 goto again; /* signaled */
84 mutex_exit(&tq->tq_lock);
86 t = kmem_alloc(sizeof (taskq_ent_t), tqflags & KM_SLEEP);
88 mutex_enter(&tq->tq_lock);
96 task_free(taskq_t *tq, taskq_ent_t *t)
98 if (tq->tq_nalloc <= tq->tq_minalloc) {
99 t->tqent_next = tq->tq_freelist;
103 mutex_exit(&tq->tq_lock);
104 kmem_free(t, sizeof (taskq_ent_t));
105 mutex_enter(&tq->tq_lock);
108 if (tq->tq_maxalloc_wait)
109 cv_signal(&tq->tq_maxalloc_cv);
113 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
122 mutex_enter(&tq->tq_lock);
123 ASSERT(tq->tq_flags & TASKQ_ACTIVE);
124 if ((t = task_alloc(tq, tqflags)) == NULL) {
125 mutex_exit(&tq->tq_lock);
128 if (tqflags & TQ_FRONT) {
129 t->tqent_next = tq->tq_task.tqent_next;
130 t->tqent_prev = &tq->tq_task;
132 t->tqent_next = &tq->tq_task;
133 t->tqent_prev = tq->tq_task.tqent_prev;
135 t->tqent_next->tqent_prev = t;
136 t->tqent_prev->tqent_next = t;
137 t->tqent_func = func;
139 cv_signal(&tq->tq_dispatch_cv);
140 mutex_exit(&tq->tq_lock);
145 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
148 ASSERT(func != NULL);
149 ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
152 * Mark it as a prealloc'd task. This is important
153 * to ensure that we don't free it later.
155 t->tqent_flags |= TQENT_FLAG_PREALLOC;
157 * Enqueue the task to the underlying queue.
159 mutex_enter(&tq->tq_lock);
161 if (flags & TQ_FRONT) {
162 t->tqent_next = tq->tq_task.tqent_next;
163 t->tqent_prev = &tq->tq_task;
165 t->tqent_next = &tq->tq_task;
166 t->tqent_prev = tq->tq_task.tqent_prev;
168 t->tqent_next->tqent_prev = t;
169 t->tqent_prev->tqent_next = t;
170 t->tqent_func = func;
172 cv_signal(&tq->tq_dispatch_cv);
173 mutex_exit(&tq->tq_lock);
177 taskq_wait(taskq_t *tq)
179 mutex_enter(&tq->tq_lock);
180 while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
181 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
182 mutex_exit(&tq->tq_lock);
186 taskq_thread(void *arg)
192 mutex_enter(&tq->tq_lock);
193 while (tq->tq_flags & TASKQ_ACTIVE) {
194 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
195 if (--tq->tq_active == 0)
196 cv_broadcast(&tq->tq_wait_cv);
197 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
201 t->tqent_prev->tqent_next = t->tqent_next;
202 t->tqent_next->tqent_prev = t->tqent_prev;
203 t->tqent_next = NULL;
204 t->tqent_prev = NULL;
205 prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
206 mutex_exit(&tq->tq_lock);
208 rw_enter(&tq->tq_threadlock, RW_READER);
209 t->tqent_func(t->tqent_arg);
210 rw_exit(&tq->tq_threadlock);
212 mutex_enter(&tq->tq_lock);
217 cv_broadcast(&tq->tq_wait_cv);
218 mutex_exit(&tq->tq_lock);
224 taskq_create(const char *name, int nthreads, pri_t pri,
225 int minalloc, int maxalloc, uint_t flags)
227 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
230 if (flags & TASKQ_THREADS_CPU_PCT) {
232 ASSERT3S(nthreads, >=, 0);
233 ASSERT3S(nthreads, <=, 100);
234 pct = MIN(nthreads, 100);
237 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
238 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
240 ASSERT3S(nthreads, >=, 1);
243 rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
244 mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
245 cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
246 cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
247 cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
248 tq->tq_flags = flags | TASKQ_ACTIVE;
249 tq->tq_active = nthreads;
250 tq->tq_nthreads = nthreads;
251 tq->tq_minalloc = minalloc;
252 tq->tq_maxalloc = maxalloc;
253 tq->tq_task.tqent_next = &tq->tq_task;
254 tq->tq_task.tqent_prev = &tq->tq_task;
255 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
257 if (flags & TASKQ_PREPOPULATE) {
258 mutex_enter(&tq->tq_lock);
259 while (minalloc-- > 0)
260 task_free(tq, task_alloc(tq, KM_SLEEP));
261 mutex_exit(&tq->tq_lock);
264 for (t = 0; t < nthreads; t++)
265 (void) thr_create(0, 0, taskq_thread,
266 tq, THR_BOUND, &tq->tq_threadlist[t]);
272 taskq_destroy(taskq_t *tq)
275 int nthreads = tq->tq_nthreads;
279 mutex_enter(&tq->tq_lock);
281 tq->tq_flags &= ~TASKQ_ACTIVE;
282 cv_broadcast(&tq->tq_dispatch_cv);
284 while (tq->tq_nthreads != 0)
285 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
288 while (tq->tq_nalloc != 0) {
289 ASSERT(tq->tq_freelist != NULL);
290 task_free(tq, task_alloc(tq, KM_SLEEP));
293 mutex_exit(&tq->tq_lock);
295 for (t = 0; t < nthreads; t++)
296 (void) thr_join(tq->tq_threadlist[t], NULL, NULL);
298 kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
300 rw_destroy(&tq->tq_threadlock);
301 mutex_destroy(&tq->tq_lock);
302 cv_destroy(&tq->tq_dispatch_cv);
303 cv_destroy(&tq->tq_wait_cv);
304 cv_destroy(&tq->tq_maxalloc_cv);
306 kmem_free(tq, sizeof (taskq_t));
310 taskq_member(taskq_t *tq, void *t)
317 for (i = 0; i < tq->tq_nthreads; i++)
318 if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
325 system_taskq_init(void)
327 system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
328 TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
332 system_taskq_fini(void)
334 taskq_destroy(system_taskq);
335 system_taskq = NULL; /* defensive */