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 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
30 #include <sys/zfs_context.h>
33 taskq_t
*system_taskq
;
35 #define TASKQ_ACTIVE 0x00010000
39 krwlock_t tq_threadlock
;
40 kcondvar_t tq_dispatch_cv
;
41 kcondvar_t tq_wait_cv
;
42 kthread_t
**tq_threadlist
;
49 kcondvar_t tq_maxalloc_cv
;
51 taskq_ent_t
*tq_freelist
;
56 task_alloc(taskq_t
*tq
, int tqflags
)
61 again
: if ((t
= tq
->tq_freelist
) != NULL
&& tq
->tq_nalloc
>= tq
->tq_minalloc
) {
62 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
63 tq
->tq_freelist
= t
->tqent_next
;
65 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
66 if (!(tqflags
& KM_SLEEP
))
70 * We don't want to exceed tq_maxalloc, but we can't
71 * wait for other tasks to complete (and thus free up
72 * task structures) without risking deadlock with
73 * the caller. So, we just delay for one second
74 * to throttle the allocation rate. If we have tasks
75 * complete before one second timeout expires then
76 * taskq_ent_free will signal us and we will
77 * immediately retry the allocation.
79 tq
->tq_maxalloc_wait
++;
80 rv
= cv_timedwait(&tq
->tq_maxalloc_cv
,
81 &tq
->tq_lock
, ddi_get_lbolt() + hz
);
82 tq
->tq_maxalloc_wait
--;
84 goto again
; /* signaled */
86 mutex_exit(&tq
->tq_lock
);
88 t
= kmem_alloc(sizeof (taskq_ent_t
), tqflags
);
90 mutex_enter(&tq
->tq_lock
);
92 /* Make sure we start without any flags */
101 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
103 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
104 t
->tqent_next
= tq
->tq_freelist
;
108 mutex_exit(&tq
->tq_lock
);
109 kmem_free(t
, sizeof (taskq_ent_t
));
110 mutex_enter(&tq
->tq_lock
);
113 if (tq
->tq_maxalloc_wait
)
114 cv_signal(&tq
->tq_maxalloc_cv
);
118 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
)
127 mutex_enter(&tq
->tq_lock
);
128 ASSERT(tq
->tq_flags
& TASKQ_ACTIVE
);
129 if ((t
= task_alloc(tq
, tqflags
)) == NULL
) {
130 mutex_exit(&tq
->tq_lock
);
133 if (tqflags
& TQ_FRONT
) {
134 t
->tqent_next
= tq
->tq_task
.tqent_next
;
135 t
->tqent_prev
= &tq
->tq_task
;
137 t
->tqent_next
= &tq
->tq_task
;
138 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
140 t
->tqent_next
->tqent_prev
= t
;
141 t
->tqent_prev
->tqent_next
= t
;
142 t
->tqent_func
= func
;
145 cv_signal(&tq
->tq_dispatch_cv
);
146 mutex_exit(&tq
->tq_lock
);
151 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
,
158 taskq_empty_ent(taskq_ent_t
*t
)
160 return (t
->tqent_next
== NULL
);
164 taskq_init_ent(taskq_ent_t
*t
)
166 t
->tqent_next
= NULL
;
167 t
->tqent_prev
= NULL
;
168 t
->tqent_func
= NULL
;
174 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
177 ASSERT(func
!= NULL
);
178 ASSERT(!(tq
->tq_flags
& TASKQ_DYNAMIC
));
181 * Mark it as a prealloc'd task. This is important
182 * to ensure that we don't free it later.
184 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
186 * Enqueue the task to the underlying queue.
188 mutex_enter(&tq
->tq_lock
);
190 if (flags
& TQ_FRONT
) {
191 t
->tqent_next
= tq
->tq_task
.tqent_next
;
192 t
->tqent_prev
= &tq
->tq_task
;
194 t
->tqent_next
= &tq
->tq_task
;
195 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
197 t
->tqent_next
->tqent_prev
= t
;
198 t
->tqent_prev
->tqent_next
= t
;
199 t
->tqent_func
= func
;
201 cv_signal(&tq
->tq_dispatch_cv
);
202 mutex_exit(&tq
->tq_lock
);
206 taskq_wait(taskq_t
*tq
)
208 mutex_enter(&tq
->tq_lock
);
209 while (tq
->tq_task
.tqent_next
!= &tq
->tq_task
|| tq
->tq_active
!= 0)
210 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
211 mutex_exit(&tq
->tq_lock
);
215 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
221 taskq_thread(void *arg
)
227 mutex_enter(&tq
->tq_lock
);
228 while (tq
->tq_flags
& TASKQ_ACTIVE
) {
229 if ((t
= tq
->tq_task
.tqent_next
) == &tq
->tq_task
) {
230 if (--tq
->tq_active
== 0)
231 cv_broadcast(&tq
->tq_wait_cv
);
232 cv_wait(&tq
->tq_dispatch_cv
, &tq
->tq_lock
);
236 t
->tqent_prev
->tqent_next
= t
->tqent_next
;
237 t
->tqent_next
->tqent_prev
= t
->tqent_prev
;
238 t
->tqent_next
= NULL
;
239 t
->tqent_prev
= NULL
;
240 prealloc
= t
->tqent_flags
& TQENT_FLAG_PREALLOC
;
241 mutex_exit(&tq
->tq_lock
);
243 rw_enter(&tq
->tq_threadlock
, RW_READER
);
244 t
->tqent_func(t
->tqent_arg
);
245 rw_exit(&tq
->tq_threadlock
);
247 mutex_enter(&tq
->tq_lock
);
252 cv_broadcast(&tq
->tq_wait_cv
);
253 mutex_exit(&tq
->tq_lock
);
259 taskq_create(const char *name
, int nthreads
, pri_t pri
,
260 int minalloc
, int maxalloc
, uint_t flags
)
262 taskq_t
*tq
= kmem_zalloc(sizeof (taskq_t
), KM_SLEEP
);
265 if (flags
& TASKQ_THREADS_CPU_PCT
) {
267 ASSERT3S(nthreads
, >=, 0);
268 ASSERT3S(nthreads
, <=, 100);
269 pct
= MIN(nthreads
, 100);
272 nthreads
= (sysconf(_SC_NPROCESSORS_ONLN
) * pct
) / 100;
273 nthreads
= MAX(nthreads
, 1); /* need at least 1 thread */
275 ASSERT3S(nthreads
, >=, 1);
278 rw_init(&tq
->tq_threadlock
, NULL
, RW_DEFAULT
, NULL
);
279 mutex_init(&tq
->tq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
280 cv_init(&tq
->tq_dispatch_cv
, NULL
, CV_DEFAULT
, NULL
);
281 cv_init(&tq
->tq_wait_cv
, NULL
, CV_DEFAULT
, NULL
);
282 cv_init(&tq
->tq_maxalloc_cv
, NULL
, CV_DEFAULT
, NULL
);
283 tq
->tq_flags
= flags
| TASKQ_ACTIVE
;
284 tq
->tq_active
= nthreads
;
285 tq
->tq_nthreads
= nthreads
;
286 tq
->tq_minalloc
= minalloc
;
287 tq
->tq_maxalloc
= maxalloc
;
288 tq
->tq_task
.tqent_next
= &tq
->tq_task
;
289 tq
->tq_task
.tqent_prev
= &tq
->tq_task
;
290 tq
->tq_threadlist
= kmem_alloc(nthreads
* sizeof (kthread_t
*),
293 if (flags
& TASKQ_PREPOPULATE
) {
294 mutex_enter(&tq
->tq_lock
);
295 while (minalloc
-- > 0)
296 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
297 mutex_exit(&tq
->tq_lock
);
300 for (t
= 0; t
< nthreads
; t
++)
301 VERIFY((tq
->tq_threadlist
[t
] = thread_create(NULL
, 0,
302 taskq_thread
, tq
, TS_RUN
, NULL
, 0, 0)) != NULL
);
308 taskq_destroy(taskq_t
*tq
)
310 int nthreads
= tq
->tq_nthreads
;
314 mutex_enter(&tq
->tq_lock
);
316 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
317 cv_broadcast(&tq
->tq_dispatch_cv
);
319 while (tq
->tq_nthreads
!= 0)
320 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
323 while (tq
->tq_nalloc
!= 0) {
324 ASSERT(tq
->tq_freelist
!= NULL
);
325 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
328 mutex_exit(&tq
->tq_lock
);
330 kmem_free(tq
->tq_threadlist
, nthreads
* sizeof (kthread_t
*));
332 rw_destroy(&tq
->tq_threadlock
);
333 mutex_destroy(&tq
->tq_lock
);
334 cv_destroy(&tq
->tq_dispatch_cv
);
335 cv_destroy(&tq
->tq_wait_cv
);
336 cv_destroy(&tq
->tq_maxalloc_cv
);
338 kmem_free(tq
, sizeof (taskq_t
));
342 taskq_member(taskq_t
*tq
, kthread_t
*t
)
349 for (i
= 0; i
< tq
->tq_nthreads
; i
++)
350 if (tq
->tq_threadlist
[i
] == t
)
357 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
363 system_taskq_init(void)
365 system_taskq
= taskq_create("system_taskq", 64, minclsyspri
, 4, 512,
366 TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);
370 system_taskq_fini(void)
372 taskq_destroy(system_taskq
);
373 system_taskq
= NULL
; /* defensive */