subr_workqueue.c revision 1.4 1 /* $NetBSD: subr_workqueue.c,v 1.4 2006/09/16 11:14:36 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.4 2006/09/16 11:14:36 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 int 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 #if 0 /* notyet */
66 s = splraiseipl(wq->wq_ipl);
67 #else
68 s = splhigh(); /* XXX */
69 #endif
70 simple_lock(&q->q_lock);
71 q->q_savedipl = s;
72 }
73
74 static void
75 workqueue_unlock(struct workqueue *wq, struct workqueue_queue *q)
76 {
77 int s = q->q_savedipl;
78
79 simple_unlock(&q->q_lock);
80 splx(s);
81 }
82
83 static void
84 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
85 {
86 struct work *wk;
87 struct work *next;
88
89 /*
90 * note that "list" is not a complete SIMPLEQ.
91 */
92
93 for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
94 next = SIMPLEQ_NEXT(wk, wk_entry);
95 (*wq->wq_func)(wk, wq->wq_arg);
96 }
97 }
98
99 static void
100 workqueue_run(struct workqueue *wq)
101 {
102 struct workqueue_queue *q = &wq->wq_queue;
103
104 for (;;) {
105 struct workqhead tmp;
106 int error;
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 workqueue_lock(wq, q);
117 while (SIMPLEQ_EMPTY(&q->q_queue)) {
118 error = ltsleep(q, wq->wq_prio, wq->wq_name, 0,
119 &q->q_lock);
120 if (error) {
121 panic("%s: %s error=%d",
122 __func__, wq->wq_name, error);
123 }
124 }
125 tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
126 SIMPLEQ_INIT(&q->q_queue);
127 workqueue_unlock(wq, q);
128
129 workqueue_runlist(wq, &tmp);
130 }
131 }
132
133 static void
134 workqueue_worker(void *arg)
135 {
136 struct workqueue *wq = arg;
137
138 workqueue_run(wq);
139 }
140
141 static void
142 workqueue_init(struct workqueue *wq, const char *name,
143 void (*callback_func)(struct work *, void *), void *callback_arg,
144 int prio, int ipl)
145 {
146
147 wq->wq_ipl = ipl;
148 wq->wq_prio = prio;
149 wq->wq_name = name;
150 wq->wq_func = callback_func;
151 wq->wq_arg = callback_arg;
152 }
153
154 static int
155 workqueue_initqueue(struct workqueue *wq)
156 {
157 struct workqueue_queue *q = &wq->wq_queue;
158 int error;
159
160 simple_lock_init(&q->q_lock);
161 SIMPLEQ_INIT(&q->q_queue);
162 error = kthread_create1(workqueue_worker, wq, &q->q_worker,
163 wq->wq_name);
164
165 return error;
166 }
167
168 /* --- */
169
170 int
171 workqueue_create(struct workqueue **wqp, const char *name,
172 void (*callback_func)(struct work *, void *), void *callback_arg,
173 int prio, int ipl, int flags)
174 {
175 struct workqueue *wq;
176 int error;
177
178 wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
179 if (wq == NULL) {
180 return ENOMEM;
181 }
182
183 workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
184
185 error = workqueue_initqueue(wq);
186 if (error) {
187 kmem_free(wq, sizeof(*wq));
188 return error;
189 }
190
191 *wqp = wq;
192 return 0;
193 }
194
195 void
196 workqueue_enqueue(struct workqueue *wq, struct work *wk)
197 {
198 struct workqueue_queue *q = &wq->wq_queue;
199 boolean_t wasempty;
200
201 workqueue_lock(wq, q);
202 wasempty = SIMPLEQ_EMPTY(&q->q_queue);
203 SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
204 workqueue_unlock(wq, q);
205
206 if (wasempty) {
207 wakeup(q);
208 }
209 }
210