taskq.c revision 185029
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include <sys/zfs_context.h>
29
30int taskq_now;
31
32typedef struct task {
33	struct task	*task_next;
34	struct task	*task_prev;
35	task_func_t	*task_func;
36	void		*task_arg;
37} task_t;
38
39#define	TASKQ_ACTIVE	0x00010000
40
41struct taskq {
42	kmutex_t	tq_lock;
43	krwlock_t	tq_threadlock;
44	kcondvar_t	tq_dispatch_cv;
45	kcondvar_t	tq_wait_cv;
46	thread_t	*tq_threadlist;
47	int		tq_flags;
48	int		tq_active;
49	int		tq_nthreads;
50	int		tq_nalloc;
51	int		tq_minalloc;
52	int		tq_maxalloc;
53	task_t		*tq_freelist;
54	task_t		tq_task;
55};
56
57static task_t *
58task_alloc(taskq_t *tq, int tqflags)
59{
60	task_t *t;
61
62	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
63		tq->tq_freelist = t->task_next;
64	} else {
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);
69				return (NULL);
70			}
71			/*
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.
77			 */
78			delay(hz);
79		}
80		t = kmem_alloc(sizeof (task_t), tqflags);
81		mutex_enter(&tq->tq_lock);
82		if (t != NULL)
83			tq->tq_nalloc++;
84	}
85	return (t);
86}
87
88static void
89task_free(taskq_t *tq, task_t *t)
90{
91	if (tq->tq_nalloc <= tq->tq_minalloc) {
92		t->task_next = tq->tq_freelist;
93		tq->tq_freelist = t;
94	} else {
95		tq->tq_nalloc--;
96		mutex_exit(&tq->tq_lock);
97		kmem_free(t, sizeof (task_t));
98		mutex_enter(&tq->tq_lock);
99	}
100}
101
102taskqid_t
103taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
104{
105	task_t *t;
106
107	if (taskq_now) {
108		func(arg);
109		return (1);
110	}
111
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);
116		return (0);
117	}
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;
122	t->task_func = func;
123	t->task_arg = arg;
124	cv_signal(&tq->tq_dispatch_cv);
125	mutex_exit(&tq->tq_lock);
126	return (1);
127}
128
129void
130taskq_wait(taskq_t *tq)
131{
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);
136}
137
138static void *
139taskq_thread(void *arg)
140{
141	taskq_t *tq = arg;
142	task_t *t;
143
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);
150			tq->tq_active++;
151			continue;
152		}
153		t->task_prev->task_next = t->task_next;
154		t->task_next->task_prev = t->task_prev;
155		mutex_exit(&tq->tq_lock);
156
157		rw_enter(&tq->tq_threadlock, RW_READER);
158		t->task_func(t->task_arg);
159		rw_exit(&tq->tq_threadlock);
160
161		mutex_enter(&tq->tq_lock);
162		task_free(tq, t);
163	}
164	tq->tq_nthreads--;
165	cv_broadcast(&tq->tq_wait_cv);
166	mutex_exit(&tq->tq_lock);
167	return (NULL);
168}
169
170/*ARGSUSED*/
171taskq_t *
172taskq_create(const char *name, int nthreads, pri_t pri,
173	int minalloc, int maxalloc, uint_t flags)
174{
175	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
176	int t;
177
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);
190
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);
196	}
197
198	for (t = 0; t < nthreads; t++)
199		(void) thr_create(0, 0, taskq_thread,
200		    tq, THR_BOUND, &tq->tq_threadlist[t]);
201
202	return (tq);
203}
204
205void
206taskq_destroy(taskq_t *tq)
207{
208	int t;
209	int nthreads = tq->tq_nthreads;
210
211	taskq_wait(tq);
212
213	mutex_enter(&tq->tq_lock);
214
215	tq->tq_flags &= ~TASKQ_ACTIVE;
216	cv_broadcast(&tq->tq_dispatch_cv);
217
218	while (tq->tq_nthreads != 0)
219		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
220
221	tq->tq_minalloc = 0;
222	while (tq->tq_nalloc != 0) {
223		ASSERT(tq->tq_freelist != NULL);
224		task_free(tq, task_alloc(tq, KM_SLEEP));
225	}
226
227	mutex_exit(&tq->tq_lock);
228
229	for (t = 0; t < nthreads; t++)
230		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
231
232	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
233
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);
238
239	kmem_free(tq, sizeof (taskq_t));
240}
241
242int
243taskq_member(taskq_t *tq, void *t)
244{
245	int i;
246
247	if (taskq_now)
248		return (1);
249
250	for (i = 0; i < tq->tq_nthreads; i++)
251		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
252			return (1);
253
254	return (0);
255}
256