subr_workqueue.c revision 1.8 1 /* $NetBSD: subr_workqueue.c,v 1.8 2006/12/21 15:55:25 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.8 2006/12/21 15:55:25 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
39 SIMPLEQ_HEAD(workqhead, work);
40
41 struct workqueue_queue {
42 struct simplelock q_lock;
43 int q_savedipl;
44 struct workqhead q_queue;
45 struct proc *q_worker;
46 };
47
48 struct workqueue {
49 struct workqueue_queue wq_queue; /* todo: make this per-cpu */
50
51 void (*wq_func)(struct work *, void *);
52 void *wq_arg;
53 const char *wq_name;
54 int wq_prio;
55 ipl_cookie_t wq_ipl;
56 };
57
58 #define POISON 0xaabbccdd
59
60 static void
61 workqueue_lock(struct workqueue *wq, struct workqueue_queue *q)
62 {
63 int s;
64
65 s = splraiseipl(wq->wq_ipl);
66 simple_lock(&q->q_lock);
67 q->q_savedipl = s;
68 }
69
70 static void
71 workqueue_unlock(struct workqueue *wq, struct workqueue_queue *q)
72 {
73 int s = q->q_savedipl;
74
75 simple_unlock(&q->q_lock);
76 splx(s);
77 }
78
79 static void
80 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
81 {
82 struct work *wk;
83 struct work *next;
84
85 /*
86 * note that "list" is not a complete SIMPLEQ.
87 */
88
89 for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
90 next = SIMPLEQ_NEXT(wk, wk_entry);
91 (*wq->wq_func)(wk, wq->wq_arg);
92 }
93 }
94
95 static void
96 workqueue_run(struct workqueue *wq)
97 {
98 struct workqueue_queue *q = &wq->wq_queue;
99
100 for (;;) {
101 struct workqhead tmp;
102 int error;
103
104 /*
105 * we violate abstraction of SIMPLEQ.
106 */
107
108 #if defined(DIAGNOSTIC)
109 tmp.sqh_last = (void *)POISON;
110 #endif /* defined(DIAGNOSTIC) */
111
112 workqueue_lock(wq, q);
113 while (SIMPLEQ_EMPTY(&q->q_queue)) {
114 error = ltsleep(q, wq->wq_prio, wq->wq_name, 0,
115 &q->q_lock);
116 if (error) {
117 panic("%s: %s error=%d",
118 __func__, wq->wq_name, error);
119 }
120 }
121 tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
122 SIMPLEQ_INIT(&q->q_queue);
123 workqueue_unlock(wq, q);
124
125 workqueue_runlist(wq, &tmp);
126 }
127 }
128
129 static void
130 workqueue_worker(void *arg)
131 {
132 struct workqueue *wq = arg;
133
134 workqueue_run(wq);
135 }
136
137 static void
138 workqueue_init(struct workqueue *wq, const char *name,
139 void (*callback_func)(struct work *, void *), void *callback_arg,
140 int prio, int ipl)
141 {
142
143 wq->wq_ipl = makeiplcookie(ipl);
144 wq->wq_prio = prio;
145 wq->wq_name = name;
146 wq->wq_func = callback_func;
147 wq->wq_arg = callback_arg;
148 }
149
150 static int
151 workqueue_initqueue(struct workqueue *wq)
152 {
153 struct workqueue_queue *q = &wq->wq_queue;
154 int error;
155
156 simple_lock_init(&q->q_lock);
157 SIMPLEQ_INIT(&q->q_queue);
158 error = kthread_create1(workqueue_worker, wq, &q->q_worker,
159 wq->wq_name);
160
161 return error;
162 }
163
164 struct workqueue_exitargs {
165 struct work wqe_wk;
166 struct workqueue_queue *wqe_q;
167 };
168
169 static void
170 workqueue_exit(struct work *wk, void *arg)
171 {
172 struct workqueue_exitargs *wqe = (void *)wk;
173 struct workqueue_queue *q = wqe->wqe_q;
174
175 /*
176 * no need to raise ipl because only competition at this point
177 * is workqueue_finiqueue.
178 */
179
180 KASSERT(q->q_worker == curproc);
181 simple_lock(&q->q_lock);
182 q->q_worker = NULL;
183 simple_unlock(&q->q_lock);
184 wakeup(q);
185 kthread_exit(0);
186 }
187
188 static void
189 workqueue_finiqueue(struct workqueue *wq)
190 {
191 struct workqueue_queue *q = &wq->wq_queue;
192 struct workqueue_exitargs wqe;
193
194 wq->wq_func = workqueue_exit;
195
196 wqe.wqe_q = q;
197 KASSERT(SIMPLEQ_EMPTY(&q->q_queue));
198 KASSERT(q->q_worker != NULL);
199 workqueue_lock(wq, q);
200 SIMPLEQ_INSERT_TAIL(&q->q_queue, &wqe.wqe_wk, wk_entry);
201 wakeup(q);
202 while (q->q_worker != NULL) {
203 int error;
204
205 error = ltsleep(q, wq->wq_prio, "wqfini", 0, &q->q_lock);
206 if (error) {
207 panic("%s: %s error=%d",
208 __func__, wq->wq_name, error);
209 }
210 }
211 workqueue_unlock(wq, q);
212 }
213
214 /* --- */
215
216 int
217 workqueue_create(struct workqueue **wqp, const char *name,
218 void (*callback_func)(struct work *, void *), void *callback_arg,
219 int prio, int ipl, int flags)
220 {
221 struct workqueue *wq;
222 int error;
223
224 wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
225 if (wq == NULL) {
226 return ENOMEM;
227 }
228
229 workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
230
231 error = workqueue_initqueue(wq);
232 if (error) {
233 kmem_free(wq, sizeof(*wq));
234 return error;
235 }
236
237 *wqp = wq;
238 return 0;
239 }
240
241 void
242 workqueue_destroy(struct workqueue *wq)
243 {
244
245 workqueue_finiqueue(wq);
246 kmem_free(wq, sizeof(*wq));
247 }
248
249 void
250 workqueue_enqueue(struct workqueue *wq, struct work *wk)
251 {
252 struct workqueue_queue *q = &wq->wq_queue;
253 boolean_t wasempty;
254
255 workqueue_lock(wq, q);
256 wasempty = SIMPLEQ_EMPTY(&q->q_queue);
257 SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
258 workqueue_unlock(wq, q);
259
260 if (wasempty) {
261 wakeup(q);
262 }
263 }
264