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 2008 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
;
57 task_alloc(taskq_t
*tq
, int tqflags
)
61 if ((t
= tq
->tq_freelist
) != NULL
&& tq
->tq_nalloc
>= tq
->tq_minalloc
) {
62 tq
->tq_freelist
= t
->task_next
;
64 mutex_exit(&tq
->tq_lock
);
65 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
66 if (!(tqflags
& KM_SLEEP
)) {
67 mutex_enter(&tq
->tq_lock
);
71 * We don't want to exceed tq_maxalloc, but we can't
72 * wait for other tasks to complete (and thus free up
73 * task structures) without risking deadlock with
74 * the caller. So, we just delay for one second
75 * to throttle the allocation rate.
79 t
= kmem_alloc(sizeof (task_t
), tqflags
);
80 mutex_enter(&tq
->tq_lock
);
88 task_free(taskq_t
*tq
, task_t
*t
)
90 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
91 t
->task_next
= tq
->tq_freelist
;
95 mutex_exit(&tq
->tq_lock
);
96 kmem_free(t
, sizeof (task_t
));
97 mutex_enter(&tq
->tq_lock
);
102 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
)
111 mutex_enter(&tq
->tq_lock
);
112 ASSERT(tq
->tq_flags
& TASKQ_ACTIVE
);
113 if ((t
= task_alloc(tq
, tqflags
)) == NULL
) {
114 mutex_exit(&tq
->tq_lock
);
117 t
->task_next
= &tq
->tq_task
;
118 t
->task_prev
= tq
->tq_task
.task_prev
;
119 t
->task_next
->task_prev
= t
;
120 t
->task_prev
->task_next
= t
;
123 cv_signal(&tq
->tq_dispatch_cv
);
124 mutex_exit(&tq
->tq_lock
);
129 taskq_wait(taskq_t
*tq
)
131 mutex_enter(&tq
->tq_lock
);
132 while (tq
->tq_task
.task_next
!= &tq
->tq_task
|| tq
->tq_active
!= 0)
133 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
134 mutex_exit(&tq
->tq_lock
);
138 taskq_thread(void *arg
)
143 mutex_enter(&tq
->tq_lock
);
144 while (tq
->tq_flags
& TASKQ_ACTIVE
) {
145 if ((t
= tq
->tq_task
.task_next
) == &tq
->tq_task
) {
146 if (--tq
->tq_active
== 0)
147 cv_broadcast(&tq
->tq_wait_cv
);
148 cv_wait(&tq
->tq_dispatch_cv
, &tq
->tq_lock
);
152 t
->task_prev
->task_next
= t
->task_next
;
153 t
->task_next
->task_prev
= t
->task_prev
;
154 mutex_exit(&tq
->tq_lock
);
156 rw_enter(&tq
->tq_threadlock
, RW_READER
);
157 t
->task_func(t
->task_arg
);
158 rw_exit(&tq
->tq_threadlock
);
160 mutex_enter(&tq
->tq_lock
);
164 cv_broadcast(&tq
->tq_wait_cv
);
165 mutex_exit(&tq
->tq_lock
);
171 taskq_create(const char *name
, int nthreads
, pri_t pri
,
172 int minalloc
, int maxalloc
, uint_t flags
)
174 taskq_t
*tq
= kmem_zalloc(sizeof (taskq_t
), KM_SLEEP
);
177 rw_init(&tq
->tq_threadlock
, NULL
, RW_DEFAULT
, NULL
);
178 mutex_init(&tq
->tq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
179 cv_init(&tq
->tq_dispatch_cv
, NULL
, CV_DEFAULT
, NULL
);
180 cv_init(&tq
->tq_wait_cv
, NULL
, CV_DEFAULT
, NULL
);
181 tq
->tq_flags
= flags
| TASKQ_ACTIVE
;
182 tq
->tq_active
= nthreads
;
183 tq
->tq_nthreads
= nthreads
;
184 tq
->tq_minalloc
= minalloc
;
185 tq
->tq_maxalloc
= maxalloc
;
186 tq
->tq_task
.task_next
= &tq
->tq_task
;
187 tq
->tq_task
.task_prev
= &tq
->tq_task
;
188 tq
->tq_threadlist
= kmem_alloc(nthreads
* sizeof (thread_t
), KM_SLEEP
);
190 if (flags
& TASKQ_PREPOPULATE
) {
191 mutex_enter(&tq
->tq_lock
);
192 while (minalloc
-- > 0)
193 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
194 mutex_exit(&tq
->tq_lock
);
197 for (t
= 0; t
< nthreads
; t
++)
198 (void) thr_create(0, 0, taskq_thread
,
199 tq
, THR_BOUND
, &tq
->tq_threadlist
[t
]);
205 taskq_destroy(taskq_t
*tq
)
208 int nthreads
= tq
->tq_nthreads
;
212 mutex_enter(&tq
->tq_lock
);
214 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
215 cv_broadcast(&tq
->tq_dispatch_cv
);
217 while (tq
->tq_nthreads
!= 0)
218 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
221 while (tq
->tq_nalloc
!= 0) {
222 ASSERT(tq
->tq_freelist
!= NULL
);
223 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
226 mutex_exit(&tq
->tq_lock
);
228 for (t
= 0; t
< nthreads
; t
++)
229 (void) thr_join(tq
->tq_threadlist
[t
], NULL
, NULL
);
231 kmem_free(tq
->tq_threadlist
, nthreads
* sizeof (thread_t
));
233 rw_destroy(&tq
->tq_threadlock
);
234 mutex_destroy(&tq
->tq_lock
);
235 cv_destroy(&tq
->tq_dispatch_cv
);
236 cv_destroy(&tq
->tq_wait_cv
);
238 kmem_free(tq
, sizeof (taskq_t
));
242 taskq_member(taskq_t
*tq
, void *t
)
249 for (i
= 0; i
< tq
->tq_nthreads
; i
++)
250 if (tq
->tq_threadlist
[i
] == (thread_t
)(uintptr_t)t
)
257 system_taskq_init(void)
259 system_taskq
= taskq_create("system_taskq", 64, minclsyspri
, 4, 512,
260 TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);