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 2009 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 if (flags
& TASKQ_THREADS_CPU_PCT
) {
179 ASSERT3S(nthreads
, >=, 0);
180 ASSERT3S(nthreads
, <=, 100);
181 pct
= MIN(nthreads
, 100);
184 nthreads
= (sysconf(_SC_NPROCESSORS_ONLN
) * pct
) / 100;
185 nthreads
= MAX(nthreads
, 1); /* need at least 1 thread */
187 ASSERT3S(nthreads
, >=, 1);
190 rw_init(&tq
->tq_threadlock
, NULL
, RW_DEFAULT
, NULL
);
191 mutex_init(&tq
->tq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
192 cv_init(&tq
->tq_dispatch_cv
, NULL
, CV_DEFAULT
, NULL
);
193 cv_init(&tq
->tq_wait_cv
, NULL
, CV_DEFAULT
, NULL
);
194 tq
->tq_flags
= flags
| TASKQ_ACTIVE
;
195 tq
->tq_active
= nthreads
;
196 tq
->tq_nthreads
= nthreads
;
197 tq
->tq_minalloc
= minalloc
;
198 tq
->tq_maxalloc
= maxalloc
;
199 tq
->tq_task
.task_next
= &tq
->tq_task
;
200 tq
->tq_task
.task_prev
= &tq
->tq_task
;
201 tq
->tq_threadlist
= kmem_alloc(nthreads
* sizeof (thread_t
), KM_SLEEP
);
203 if (flags
& TASKQ_PREPOPULATE
) {
204 mutex_enter(&tq
->tq_lock
);
205 while (minalloc
-- > 0)
206 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
207 mutex_exit(&tq
->tq_lock
);
210 for (t
= 0; t
< nthreads
; t
++)
211 (void) thr_create(0, 0, taskq_thread
,
212 tq
, THR_BOUND
, &tq
->tq_threadlist
[t
]);
218 taskq_destroy(taskq_t
*tq
)
221 int nthreads
= tq
->tq_nthreads
;
225 mutex_enter(&tq
->tq_lock
);
227 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
228 cv_broadcast(&tq
->tq_dispatch_cv
);
230 while (tq
->tq_nthreads
!= 0)
231 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
234 while (tq
->tq_nalloc
!= 0) {
235 ASSERT(tq
->tq_freelist
!= NULL
);
236 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
239 mutex_exit(&tq
->tq_lock
);
241 for (t
= 0; t
< nthreads
; t
++)
242 (void) thr_join(tq
->tq_threadlist
[t
], NULL
, NULL
);
244 kmem_free(tq
->tq_threadlist
, nthreads
* sizeof (thread_t
));
246 rw_destroy(&tq
->tq_threadlock
);
247 mutex_destroy(&tq
->tq_lock
);
248 cv_destroy(&tq
->tq_dispatch_cv
);
249 cv_destroy(&tq
->tq_wait_cv
);
251 kmem_free(tq
, sizeof (taskq_t
));
255 taskq_member(taskq_t
*tq
, void *t
)
262 for (i
= 0; i
< tq
->tq_nthreads
; i
++)
263 if (tq
->tq_threadlist
[i
] == (thread_t
)(uintptr_t)t
)
270 system_taskq_init(void)
272 system_taskq
= taskq_create("system_taskq", 64, minclsyspri
, 4, 512,
273 TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);