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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/zfs_context.h>
33 struct task *task_next;
34 struct task *task_prev;
35 task_func_t *task_func;
39 #define TASKQ_ACTIVE 0x00010000
43 krwlock_t tq_threadlock;
44 kcondvar_t tq_dispatch_cv;
45 kcondvar_t tq_wait_cv;
46 thread_t *tq_threadlist;
58 task_alloc(taskq_t *tq, int tqflags)
62 if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
63 tq->tq_freelist = t->task_next;
65 mutex_exit(&tq->tq_lock);
66 if (tq->tq_nalloc >= tq->tq_maxalloc) {
67 if (!(tqflags & KM_SLEEP)) {
68 mutex_enter(&tq->tq_lock);
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.
80 t = kmem_alloc(sizeof (task_t), tqflags);
81 mutex_enter(&tq->tq_lock);
89 task_free(taskq_t *tq, task_t *t)
91 if (tq->tq_nalloc <= tq->tq_minalloc) {
92 t->task_next = tq->tq_freelist;
96 mutex_exit(&tq->tq_lock);
97 kmem_free(t, sizeof (task_t));
98 mutex_enter(&tq->tq_lock);
103 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
112 mutex_enter(&tq->tq_lock);
113 ASSERT(tq->tq_flags & TASKQ_ACTIVE);
114 if ((t = task_alloc(tq, tqflags)) == NULL) {
115 mutex_exit(&tq->tq_lock);
118 t->task_next = &tq->tq_task;
119 t->task_prev = tq->tq_task.task_prev;
120 t->task_next->task_prev = t;
121 t->task_prev->task_next = t;
124 cv_signal(&tq->tq_dispatch_cv);
125 mutex_exit(&tq->tq_lock);
130 taskq_wait(taskq_t *tq)
132 mutex_enter(&tq->tq_lock);
133 while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
134 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
135 mutex_exit(&tq->tq_lock);
139 taskq_thread(void *arg)
144 mutex_enter(&tq->tq_lock);
145 while (tq->tq_flags & TASKQ_ACTIVE) {
146 if ((t = tq->tq_task.task_next) == &tq->tq_task) {
147 if (--tq->tq_active == 0)
148 cv_broadcast(&tq->tq_wait_cv);
149 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
153 t->task_prev->task_next = t->task_next;
154 t->task_next->task_prev = t->task_prev;
155 mutex_exit(&tq->tq_lock);
157 rw_enter(&tq->tq_threadlock, RW_READER);
158 t->task_func(t->task_arg);
159 rw_exit(&tq->tq_threadlock);
161 mutex_enter(&tq->tq_lock);
165 cv_broadcast(&tq->tq_wait_cv);
166 mutex_exit(&tq->tq_lock);
172 taskq_create(const char *name, int nthreads, pri_t pri,
173 int minalloc, int maxalloc, uint_t flags)
175 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
178 rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
179 mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
180 cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
181 cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
182 tq->tq_flags = flags | TASKQ_ACTIVE;
183 tq->tq_active = nthreads;
184 tq->tq_nthreads = nthreads;
185 tq->tq_minalloc = minalloc;
186 tq->tq_maxalloc = maxalloc;
187 tq->tq_task.task_next = &tq->tq_task;
188 tq->tq_task.task_prev = &tq->tq_task;
189 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
191 if (flags & TASKQ_PREPOPULATE) {
192 mutex_enter(&tq->tq_lock);
193 while (minalloc-- > 0)
194 task_free(tq, task_alloc(tq, KM_SLEEP));
195 mutex_exit(&tq->tq_lock);
198 for (t = 0; t < nthreads; t++)
199 (void) thr_create(0, 0, taskq_thread,
200 tq, THR_BOUND, &tq->tq_threadlist[t]);
206 taskq_destroy(taskq_t *tq)
209 int nthreads = tq->tq_nthreads;
213 mutex_enter(&tq->tq_lock);
215 tq->tq_flags &= ~TASKQ_ACTIVE;
216 cv_broadcast(&tq->tq_dispatch_cv);
218 while (tq->tq_nthreads != 0)
219 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
222 while (tq->tq_nalloc != 0) {
223 ASSERT(tq->tq_freelist != NULL);
224 task_free(tq, task_alloc(tq, KM_SLEEP));
227 mutex_exit(&tq->tq_lock);
229 for (t = 0; t < nthreads; t++)
230 (void) thr_join(tq->tq_threadlist[t], NULL, NULL);
232 kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
234 rw_destroy(&tq->tq_threadlock);
235 mutex_destroy(&tq->tq_lock);
236 cv_destroy(&tq->tq_dispatch_cv);
237 cv_destroy(&tq->tq_wait_cv);
239 kmem_free(tq, sizeof (taskq_t));
243 taskq_member(taskq_t *tq, void *t)
250 for (i = 0; i < tq->tq_nthreads; i++)
251 if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)