4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/zfs_context.h>
34 struct task *task_next;
35 struct task *task_prev;
36 task_func_t *task_func;
40 #define TASKQ_ACTIVE 0x00010000
44 krwlock_t tq_threadlock;
45 kcondvar_t tq_dispatch_cv;
46 kcondvar_t tq_wait_cv;
47 thread_t *tq_threadlist;
59 task_alloc(taskq_t *tq, int tqflags)
63 if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
64 tq->tq_freelist = t->task_next;
66 mutex_exit(&tq->tq_lock);
67 if (tq->tq_nalloc >= tq->tq_maxalloc) {
68 if (!(tqflags & KM_SLEEP)) {
69 mutex_enter(&tq->tq_lock);
73 * We don't want to exceed tq_maxalloc, but we can't
74 * wait for other tasks to complete (and thus free up
75 * task structures) without risking deadlock with
76 * the caller. So, we just delay for one second
77 * to throttle the allocation rate.
81 t = kmem_alloc(sizeof (task_t), tqflags);
82 mutex_enter(&tq->tq_lock);
90 task_free(taskq_t *tq, task_t *t)
92 if (tq->tq_nalloc <= tq->tq_minalloc) {
93 t->task_next = tq->tq_freelist;
97 mutex_exit(&tq->tq_lock);
98 kmem_free(t, sizeof (task_t));
99 mutex_enter(&tq->tq_lock);
104 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
113 mutex_enter(&tq->tq_lock);
114 ASSERT(tq->tq_flags & TASKQ_ACTIVE);
115 if ((t = task_alloc(tq, tqflags)) == NULL) {
116 mutex_exit(&tq->tq_lock);
119 t->task_next = &tq->tq_task;
120 t->task_prev = tq->tq_task.task_prev;
121 t->task_next->task_prev = t;
122 t->task_prev->task_next = t;
125 cv_signal(&tq->tq_dispatch_cv);
126 mutex_exit(&tq->tq_lock);
131 taskq_wait(taskq_t *tq)
133 mutex_enter(&tq->tq_lock);
134 while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
135 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
136 mutex_exit(&tq->tq_lock);
140 taskq_thread(void *arg)
145 mutex_enter(&tq->tq_lock);
146 while (tq->tq_flags & TASKQ_ACTIVE) {
147 if ((t = tq->tq_task.task_next) == &tq->tq_task) {
148 if (--tq->tq_active == 0)
149 cv_broadcast(&tq->tq_wait_cv);
150 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
154 t->task_prev->task_next = t->task_next;
155 t->task_next->task_prev = t->task_prev;
156 mutex_exit(&tq->tq_lock);
158 rw_enter(&tq->tq_threadlock, RW_READER);
159 t->task_func(t->task_arg);
160 rw_exit(&tq->tq_threadlock);
162 mutex_enter(&tq->tq_lock);
166 cv_broadcast(&tq->tq_wait_cv);
167 mutex_exit(&tq->tq_lock);
173 taskq_create(const char *name, int nthreads, pri_t pri,
174 int minalloc, int maxalloc, uint_t flags)
176 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
179 rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
180 tq->tq_flags = flags | TASKQ_ACTIVE;
181 tq->tq_active = nthreads;
182 tq->tq_nthreads = nthreads;
183 tq->tq_minalloc = minalloc;
184 tq->tq_maxalloc = maxalloc;
185 tq->tq_task.task_next = &tq->tq_task;
186 tq->tq_task.task_prev = &tq->tq_task;
187 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
189 if (flags & TASKQ_PREPOPULATE) {
190 mutex_enter(&tq->tq_lock);
191 while (minalloc-- > 0)
192 task_free(tq, task_alloc(tq, KM_SLEEP));
193 mutex_exit(&tq->tq_lock);
196 for (t = 0; t < nthreads; t++)
197 (void) thr_create(0, 0, taskq_thread,
198 tq, THR_BOUND, &tq->tq_threadlist[t]);
204 taskq_destroy(taskq_t *tq)
207 int nthreads = tq->tq_nthreads;
211 mutex_enter(&tq->tq_lock);
213 tq->tq_flags &= ~TASKQ_ACTIVE;
214 cv_broadcast(&tq->tq_dispatch_cv);
216 while (tq->tq_nthreads != 0)
217 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
220 while (tq->tq_nalloc != 0) {
221 ASSERT(tq->tq_freelist != NULL);
222 task_free(tq, task_alloc(tq, KM_SLEEP));
225 mutex_exit(&tq->tq_lock);
227 for (t = 0; t < nthreads; t++)
228 (void) thr_join(tq->tq_threadlist[t], NULL, NULL);
230 kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
232 rw_destroy(&tq->tq_threadlock);
234 kmem_free(tq, sizeof (taskq_t));
238 taskq_member(taskq_t *tq, void *t)
245 for (i = 0; i < tq->tq_nthreads; i++)
246 if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)