subr_workqueue.c revision 1.17 1 /* $NetBSD: subr_workqueue.c,v 1.17 2007/07/20 12:43:26 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2002, 2005 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.17 2007/07/20 12:43:26 yamt Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kthread.h>
35 #include <sys/kmem.h>
36 #include <sys/proc.h>
37 #include <sys/workqueue.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/queue.h>
41
42 typedef struct work_impl {
43 SIMPLEQ_ENTRY(work_impl) wk_entry;
44 } work_impl_t;
45
46 SIMPLEQ_HEAD(workqhead, work_impl);
47
48 struct workqueue_queue {
49 kmutex_t q_mutex;
50 kcondvar_t q_cv;
51 struct workqhead q_queue;
52 struct lwp *q_worker;
53 struct cpu_info *q_ci;
54 SLIST_ENTRY(workqueue_queue) q_list;
55 };
56
57 struct workqueue {
58 SLIST_HEAD(, workqueue_queue) wq_queue;
59 void (*wq_func)(struct work *, void *);
60 void *wq_arg;
61 const char *wq_name;
62 pri_t wq_prio;
63 ipl_cookie_t wq_ipl;
64 };
65
66 #define POISON 0xaabbccdd
67
68 static struct workqueue_queue *
69 workqueue_queue_lookup(struct workqueue *wq, struct cpu_info *ci)
70 {
71 struct workqueue_queue *q;
72
73 SLIST_FOREACH(q, &wq->wq_queue, q_list)
74 if (q->q_ci == ci)
75 return q;
76
77 return SLIST_FIRST(&wq->wq_queue);
78 }
79
80 static void
81 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
82 {
83 work_impl_t *wk;
84 work_impl_t *next;
85
86 /*
87 * note that "list" is not a complete SIMPLEQ.
88 */
89
90 for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
91 next = SIMPLEQ_NEXT(wk, wk_entry);
92 (*wq->wq_func)((void *)wk, wq->wq_arg);
93 }
94 }
95
96 static void
97 workqueue_run(struct workqueue *wq)
98 {
99 struct workqueue_queue *q;
100
101 /* find the workqueue of this kthread */
102 q = workqueue_queue_lookup(wq, curlwp->l_cpu);
103 KASSERT(q != NULL);
104
105 for (;;) {
106 struct workqhead tmp;
107
108 /*
109 * we violate abstraction of SIMPLEQ.
110 */
111
112 #if defined(DIAGNOSTIC)
113 tmp.sqh_last = (void *)POISON;
114 #endif /* defined(DIAGNOSTIC) */
115
116 mutex_enter(&q->q_mutex);
117 while (SIMPLEQ_EMPTY(&q->q_queue))
118 cv_wait(&q->q_cv, &q->q_mutex);
119 tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
120 SIMPLEQ_INIT(&q->q_queue);
121 mutex_exit(&q->q_mutex);
122
123 workqueue_runlist(wq, &tmp);
124 }
125 }
126
127 static void
128 workqueue_worker(void *arg)
129 {
130 struct workqueue *wq = arg;
131 struct lwp *l;
132
133 l = curlwp;
134 lwp_lock(l);
135 l->l_priority = wq->wq_prio;
136 l->l_usrpri = wq->wq_prio;
137 lwp_unlock(l);
138
139 workqueue_run(wq);
140 }
141
142 static void
143 workqueue_init(struct workqueue *wq, const char *name,
144 void (*callback_func)(struct work *, void *), void *callback_arg,
145 pri_t prio, int ipl)
146 {
147
148 wq->wq_ipl = makeiplcookie(ipl);
149 wq->wq_prio = prio;
150 wq->wq_name = name;
151 wq->wq_func = callback_func;
152 wq->wq_arg = callback_arg;
153 SLIST_INIT(&wq->wq_queue);
154 }
155
156 static int
157 workqueue_initqueue(struct workqueue *wq, int ipl,
158 int flags, struct cpu_info *ci)
159 {
160 struct workqueue_queue *q;
161 int error, ktf;
162 cpuid_t cpuid;
163
164 #ifdef MULTIPROCESSOR
165 cpuid = ci->ci_cpuid;
166 #else
167 cpuid = 0;
168 #endif
169
170 q = kmem_alloc(sizeof(struct workqueue_queue), KM_SLEEP);
171 SLIST_INSERT_HEAD(&wq->wq_queue, q, q_list);
172 q->q_ci = ci;
173
174 mutex_init(&q->q_mutex, MUTEX_DRIVER, ipl);
175 cv_init(&q->q_cv, wq->wq_name);
176 SIMPLEQ_INIT(&q->q_queue);
177 ktf = ((flags & WQ_MPSAFE) != 0 ? KTHREAD_MPSAFE : 0);
178 error = kthread_create(wq->wq_prio, ktf, ci, workqueue_worker,
179 wq, &q->q_worker, "%s/%d", wq->wq_name, (int)cpuid);
180
181 return error;
182 }
183
184 struct workqueue_exitargs {
185 work_impl_t wqe_wk;
186 struct workqueue_queue *wqe_q;
187 };
188
189 static void
190 workqueue_exit(struct work *wk, void *arg)
191 {
192 struct workqueue_exitargs *wqe = (void *)wk;
193 struct workqueue_queue *q = wqe->wqe_q;
194
195 /*
196 * only competition at this point is workqueue_finiqueue.
197 */
198
199 KASSERT(q->q_worker == curlwp);
200 mutex_enter(&q->q_mutex);
201 q->q_worker = NULL;
202 cv_signal(&q->q_cv);
203 mutex_exit(&q->q_mutex);
204 kthread_exit(0);
205 }
206
207 static void
208 workqueue_finiqueue(struct workqueue *wq, struct workqueue_queue *q)
209 {
210 struct workqueue_exitargs wqe;
211
212 wq->wq_func = workqueue_exit;
213
214 wqe.wqe_q = q;
215 KASSERT(SIMPLEQ_EMPTY(&q->q_queue));
216 KASSERT(q->q_worker != NULL);
217 mutex_enter(&q->q_mutex);
218 SIMPLEQ_INSERT_TAIL(&q->q_queue, &wqe.wqe_wk, wk_entry);
219 cv_signal(&q->q_cv);
220 while (q->q_worker != NULL) {
221 cv_wait(&q->q_cv, &q->q_mutex);
222 }
223 mutex_exit(&q->q_mutex);
224 mutex_destroy(&q->q_mutex);
225 cv_destroy(&q->q_cv);
226 kmem_free(q, sizeof(struct workqueue_queue));
227 }
228
229 /* --- */
230
231 int
232 workqueue_create(struct workqueue **wqp, const char *name,
233 void (*callback_func)(struct work *, void *), void *callback_arg,
234 pri_t prio, int ipl, int flags)
235 {
236 struct workqueue *wq;
237 int error = 0;
238
239 KASSERT(sizeof(work_impl_t) <= sizeof(struct work));
240
241 wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
242
243 workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
244
245 #ifdef MULTIPROCESSOR
246 if (flags & WQ_PERCPU) {
247 struct cpu_info *ci;
248 CPU_INFO_ITERATOR cii;
249
250 /* create the work-queue for each CPU */
251 for (CPU_INFO_FOREACH(cii, ci)) {
252 error = workqueue_initqueue(wq, ipl, flags, ci);
253 if (error)
254 break;
255 }
256 if (error)
257 workqueue_destroy(wq);
258
259 } else {
260 error = workqueue_initqueue(wq, ipl, flags, curcpu());
261 if (error) {
262 kmem_free(wq, sizeof(*wq));
263 return error;
264 }
265 }
266 #else
267 error = workqueue_initqueue(wq, ipl, flags, curcpu());
268 if (error) {
269 kmem_free(wq, sizeof(*wq));
270 return error;
271 }
272 #endif
273
274 *wqp = wq;
275 return 0;
276 }
277
278 void
279 workqueue_destroy(struct workqueue *wq)
280 {
281 struct workqueue_queue *q;
282
283 while ((q = SLIST_FIRST(&wq->wq_queue)) != NULL) {
284 SLIST_REMOVE_HEAD(&wq->wq_queue, q_list);
285 workqueue_finiqueue(wq, q);
286 }
287 kmem_free(wq, sizeof(*wq));
288 }
289
290 void
291 workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
292 {
293 struct workqueue_queue *q;
294 work_impl_t *wk = (void *)wk0;
295
296 q = workqueue_queue_lookup(wq, ci);
297 KASSERT(q != NULL);
298
299 mutex_enter(&q->q_mutex);
300 SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
301 cv_signal(&q->q_cv);
302 mutex_exit(&q->q_mutex);
303 }
304