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