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 #include <sys/zfs_context.h>
29 taskq_t *system_taskq;
32 struct task *task_next;
33 struct task *task_prev;
34 task_func_t *task_func;
38 #define TASKQ_ACTIVE 0x00010000
42 krwlock_t tq_threadlock;
43 kcondvar_t tq_dispatch_cv;
44 kcondvar_t tq_wait_cv;
45 thread_t *tq_threadlist;
52 kcondvar_t tq_maxalloc_cv;
59 task_alloc(taskq_t *tq, int tqflags)
64 again: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
65 tq->tq_freelist = t->task_next;
67 if (tq->tq_nalloc >= tq->tq_maxalloc) {
68 if (!(tqflags & KM_SLEEP))
72 * We don't want to exceed tq_maxalloc, but we can't
73 * wait for other tasks to complete (and thus free up
74 * task structures) without risking deadlock with
75 * the caller. So, we just delay for one second
76 * to throttle the allocation rate. If we have tasks
77 * complete before one second timeout expires then
78 * taskq_ent_free will signal us and we will
79 * immediately retry the allocation.
81 tq->tq_maxalloc_wait++;
82 rv = cv_timedwait(&tq->tq_maxalloc_cv,
83 &tq->tq_lock, ddi_get_lbolt() + hz);
84 tq->tq_maxalloc_wait--;
86 goto again; /* signaled */
88 mutex_exit(&tq->tq_lock);
90 t = kmem_alloc(sizeof (task_t), tqflags);
92 mutex_enter(&tq->tq_lock);
100 task_free(taskq_t *tq, task_t *t)
102 if (tq->tq_nalloc <= tq->tq_minalloc) {
103 t->task_next = tq->tq_freelist;
107 mutex_exit(&tq->tq_lock);
108 kmem_free(t, sizeof (task_t));
109 mutex_enter(&tq->tq_lock);
112 if (tq->tq_maxalloc_wait)
113 cv_signal(&tq->tq_maxalloc_cv);
117 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
126 mutex_enter(&tq->tq_lock);
127 ASSERT(tq->tq_flags & TASKQ_ACTIVE);
128 if ((t = task_alloc(tq, tqflags)) == NULL) {
129 mutex_exit(&tq->tq_lock);
132 if (tqflags & TQ_FRONT) {
133 t->task_next = tq->tq_task.task_next;
134 t->task_prev = &tq->tq_task;
136 t->task_next = &tq->tq_task;
137 t->task_prev = tq->tq_task.task_prev;
139 t->task_next->task_prev = t;
140 t->task_prev->task_next = t;
143 cv_signal(&tq->tq_dispatch_cv);
144 mutex_exit(&tq->tq_lock);
149 taskq_wait(taskq_t *tq)
151 mutex_enter(&tq->tq_lock);
152 while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
153 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
154 mutex_exit(&tq->tq_lock);
158 taskq_thread(void *arg)
163 mutex_enter(&tq->tq_lock);
164 while (tq->tq_flags & TASKQ_ACTIVE) {
165 if ((t = tq->tq_task.task_next) == &tq->tq_task) {
166 if (--tq->tq_active == 0)
167 cv_broadcast(&tq->tq_wait_cv);
168 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
172 t->task_prev->task_next = t->task_next;
173 t->task_next->task_prev = t->task_prev;
174 mutex_exit(&tq->tq_lock);
176 rw_enter(&tq->tq_threadlock, RW_READER);
177 t->task_func(t->task_arg);
178 rw_exit(&tq->tq_threadlock);
180 mutex_enter(&tq->tq_lock);
184 cv_broadcast(&tq->tq_wait_cv);
185 mutex_exit(&tq->tq_lock);
191 taskq_create(const char *name, int nthreads, pri_t pri,
192 int minalloc, int maxalloc, uint_t flags)
194 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
197 if (flags & TASKQ_THREADS_CPU_PCT) {
199 ASSERT3S(nthreads, >=, 0);
200 ASSERT3S(nthreads, <=, 100);
201 pct = MIN(nthreads, 100);
204 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
205 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
207 ASSERT3S(nthreads, >=, 1);
210 rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
211 mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
212 cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
213 cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
214 cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
215 tq->tq_flags = flags | TASKQ_ACTIVE;
216 tq->tq_active = nthreads;
217 tq->tq_nthreads = nthreads;
218 tq->tq_minalloc = minalloc;
219 tq->tq_maxalloc = maxalloc;
220 tq->tq_task.task_next = &tq->tq_task;
221 tq->tq_task.task_prev = &tq->tq_task;
222 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
224 if (flags & TASKQ_PREPOPULATE) {
225 mutex_enter(&tq->tq_lock);
226 while (minalloc-- > 0)
227 task_free(tq, task_alloc(tq, KM_SLEEP));
228 mutex_exit(&tq->tq_lock);
231 for (t = 0; t < nthreads; t++)
232 (void) thr_create(0, 0, taskq_thread,
233 tq, THR_BOUND, &tq->tq_threadlist[t]);
239 taskq_destroy(taskq_t *tq)
242 int nthreads = tq->tq_nthreads;
246 mutex_enter(&tq->tq_lock);
248 tq->tq_flags &= ~TASKQ_ACTIVE;
249 cv_broadcast(&tq->tq_dispatch_cv);
251 while (tq->tq_nthreads != 0)
252 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
255 while (tq->tq_nalloc != 0) {
256 ASSERT(tq->tq_freelist != NULL);
257 task_free(tq, task_alloc(tq, KM_SLEEP));
260 mutex_exit(&tq->tq_lock);
262 for (t = 0; t < nthreads; t++)
263 (void) thr_join(tq->tq_threadlist[t], NULL, NULL);
265 kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
267 rw_destroy(&tq->tq_threadlock);
268 mutex_destroy(&tq->tq_lock);
269 cv_destroy(&tq->tq_dispatch_cv);
270 cv_destroy(&tq->tq_wait_cv);
271 cv_destroy(&tq->tq_maxalloc_cv);
273 kmem_free(tq, sizeof (taskq_t));
277 taskq_member(taskq_t *tq, void *t)
284 for (i = 0; i < tq->tq_nthreads; i++)
285 if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
292 system_taskq_init(void)
294 system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
295 TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
299 system_taskq_fini(void)
301 taskq_destroy(system_taskq);
302 system_taskq = NULL; /* defensive */