1 /*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/6.2/sys/kern/subr_taskqueue.c 164286 2006-11-14 20:42:41Z cvs2svn $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/sched.h>
41 #include <sys/taskqueue.h>
42 #include <sys/unistd.h>
43 #include <machine/stdarg.h>
44
45 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
46 static void *taskqueue_giant_ih;
47 static void *taskqueue_ih;
48 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
49 static struct mtx taskqueue_queues_mutex;
50
51 struct taskqueue {
52 STAILQ_ENTRY(taskqueue) tq_link;
53 STAILQ_HEAD(, task) tq_queue;
54 const char *tq_name;
55 taskqueue_enqueue_fn tq_enqueue;
56 void *tq_context;
57 struct task *tq_running;
58 struct mtx tq_mutex;
59 struct proc **tq_pproc;
60 int tq_pcount;
61 int tq_spin;
62 int tq_flags;
63 };
64
65 #define TQ_FLAGS_ACTIVE (1 << 0)
66
67 static __inline void
68 TQ_LOCK(struct taskqueue *tq)
69 {
70 if (tq->tq_spin)
71 mtx_lock_spin(&tq->tq_mutex);
72 else
73 mtx_lock(&tq->tq_mutex);
74 }
75
76 static __inline void
77 TQ_UNLOCK(struct taskqueue *tq)
78 {
79 if (tq->tq_spin)
80 mtx_unlock_spin(&tq->tq_mutex);
81 else
82 mtx_unlock(&tq->tq_mutex);
83 }
84
85 static void init_taskqueue_list(void *data);
86
87 static __inline int
88 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
89 int t)
90 {
91 if (tq->tq_spin)
92 return (msleep_spin(p, m, wm, t));
93 return (msleep(p, m, pri, wm, t));
94 }
95
96 static void
97 init_taskqueue_list(void *data __unused)
98 {
99
100 mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
101 STAILQ_INIT(&taskqueue_queues);
102 }
103 SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
104 NULL);
105
106 static struct taskqueue *
107 _taskqueue_create(const char *name, int mflags,
108 taskqueue_enqueue_fn enqueue, void *context,
109 int mtxflags, const char *mtxname)
110 {
111 struct taskqueue *queue;
112
113 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
114 if (!queue)
115 return 0;
116
117 STAILQ_INIT(&queue->tq_queue);
118 queue->tq_name = name;
119 queue->tq_enqueue = enqueue;
120 queue->tq_context = context;
121 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
122 queue->tq_flags |= TQ_FLAGS_ACTIVE;
123 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
124
125 mtx_lock(&taskqueue_queues_mutex);
126 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
127 mtx_unlock(&taskqueue_queues_mutex);
128
129 return queue;
130 }
131
132 struct taskqueue *
133 taskqueue_create(const char *name, int mflags,
134 taskqueue_enqueue_fn enqueue, void *context,
135 struct proc **pp)
136 {
137 (void) pp;
138 return _taskqueue_create(name, mflags, enqueue, context,
139 MTX_DEF, "taskqueue");
140 }
141
142 /*
143 * Signal a taskqueue thread to terminate.
144 */
145 static void
146 taskqueue_terminate(struct proc **pp, struct taskqueue *tq)
147 {
148
149 while (tq->tq_pcount > 0) {
150 wakeup(tq);
151 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
152 }
153 }
154
155 void
156 taskqueue_free(struct taskqueue *queue)
157 {
158
159 mtx_lock(&taskqueue_queues_mutex);
160 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
161 mtx_unlock(&taskqueue_queues_mutex);
162
163 TQ_LOCK(queue);
164 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
165 taskqueue_run(queue);
166 taskqueue_terminate(queue->tq_pproc, queue);
167 mtx_destroy(&queue->tq_mutex);
168 free(queue->tq_pproc, M_TASKQUEUE);
169 free(queue, M_TASKQUEUE);
170 }
171
172 /*
173 * Returns with the taskqueue locked.
174 */
175 struct taskqueue *
176 taskqueue_find(const char *name)
177 {
178 struct taskqueue *queue;
179
180 mtx_lock(&taskqueue_queues_mutex);
181 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
182 if (strcmp(queue->tq_name, name) == 0) {
183 TQ_LOCK(queue);
184 mtx_unlock(&taskqueue_queues_mutex);
185 return queue;
186 }
187 }
188 mtx_unlock(&taskqueue_queues_mutex);
189 return NULL;
190 }
191
192 int
193 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
194 {
195 struct task *ins;
196 struct task *prev;
197
198 TQ_LOCK(queue);
199
200 /*
201 * Count multiple enqueues.
202 */
203 if (task->ta_pending) {
204 task->ta_pending++;
205 TQ_UNLOCK(queue);
206 return 0;
207 }
208
209 /*
210 * Optimise the case when all tasks have the same priority.
211 */
212 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
213 if (!prev || prev->ta_priority >= task->ta_priority) {
214 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
215 } else {
216 prev = 0;
217 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
218 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
219 if (ins->ta_priority < task->ta_priority)
220 break;
221
222 if (prev)
223 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
224 else
225 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
226 }
227
228 task->ta_pending = 1;
229 queue->tq_enqueue(queue->tq_context);
230
231 TQ_UNLOCK(queue);
232
233 return 0;
234 }
235
236 void
237 taskqueue_run(struct taskqueue *queue)
238 {
239 struct task *task;
240 int owned, pending;
241
242 owned = mtx_owned(&queue->tq_mutex);
243 if (!owned)
244 TQ_LOCK(queue);
245 while (STAILQ_FIRST(&queue->tq_queue)) {
246 /*
247 * Carefully remove the first task from the queue and
248 * zero its pending count.
249 */
250 task = STAILQ_FIRST(&queue->tq_queue);
251 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
252 pending = task->ta_pending;
253 task->ta_pending = 0;
254 queue->tq_running = task;
255 TQ_UNLOCK(queue);
256
257 task->ta_func(task->ta_context, pending);
258
259 TQ_LOCK(queue);
260 queue->tq_running = NULL;
261 wakeup(task);
262 }
263
264 /*
265 * For compatibility, unlock on return if the queue was not locked
266 * on entry, although this opens a race window.
267 */
268 if (!owned)
269 TQ_UNLOCK(queue);
270 }
271
272 void
273 taskqueue_drain(struct taskqueue *queue, struct task *task)
274 {
275 if (queue->tq_spin) { /* XXX */
276 mtx_lock_spin(&queue->tq_mutex);
277 while (task->ta_pending != 0 || task == queue->tq_running)
278 msleep_spin(task, &queue->tq_mutex, "-", 0);
279 mtx_unlock_spin(&queue->tq_mutex);
280 } else {
281 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
282
283 mtx_lock(&queue->tq_mutex);
284 while (task->ta_pending != 0 || task == queue->tq_running)
285 msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
286 mtx_unlock(&queue->tq_mutex);
287 }
288 }
289
290 static void
291 taskqueue_swi_enqueue(void *context)
292 {
293 swi_sched(taskqueue_ih, 0);
294 }
295
296 static void
297 taskqueue_swi_run(void *dummy)
298 {
299 taskqueue_run(taskqueue_swi);
300 }
301
302 static void
303 taskqueue_swi_giant_enqueue(void *context)
304 {
305 swi_sched(taskqueue_giant_ih, 0);
306 }
307
308 static void
309 taskqueue_swi_giant_run(void *dummy)
310 {
311 taskqueue_run(taskqueue_swi_giant);
312 }
313
314 int
315 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
316 const char *name, ...)
317 {
318 va_list ap;
319 struct taskqueue *tq;
320 struct thread *td;
321 char ktname[MAXCOMLEN];
322 int i, error;
323
324 if (count <= 0)
325 return (EINVAL);
326 tq = *tqp;
327
328 va_start(ap, name);
329 vsnprintf(ktname, MAXCOMLEN, name, ap);
330 va_end(ap);
331
332 tq->tq_pproc = malloc(sizeof(struct proc *) * count, M_TASKQUEUE,
333 M_NOWAIT | M_ZERO);
334 if (tq->tq_pproc == NULL) {
335 printf("%s: no memory for %s threads\n", __func__, ktname);
336 return (ENOMEM);
337 }
338
339 for (i = 0; i < count; i++) {
340 if (count == 1)
341 error = kthread_create(taskqueue_thread_loop, tqp,
342 &tq->tq_pproc[i], RFSTOPPED, 0, ktname);
343 else
344 error = kthread_create(taskqueue_thread_loop, tqp,
345 &tq->tq_pproc[i], RFSTOPPED, 0, "%s_%d", ktname, i);
346 if (error) {
347 /* should be ok to continue, taskqueue_free will dtrt */
348 printf("%s: kthread_create(%s): error %d",
349 __func__, ktname, error);
350 tq->tq_pproc[i] = NULL; /* paranoid */
351 } else
352 tq->tq_pcount++;
353 }
354 mtx_lock_spin(&sched_lock);
355 for (i = 0; i < count; i++) {
356 if (tq->tq_pproc[i] == NULL)
357 continue;
358 td = FIRST_THREAD_IN_PROC(tq->tq_pproc[i]);
359 sched_prio(td, pri);
360 setrunqueue(td, SRQ_BORING);
361 }
362 mtx_unlock_spin(&sched_lock);
363
364 return (0);
365 }
366
367 void
368 taskqueue_thread_loop(void *arg)
369 {
370 struct taskqueue **tqp, *tq;
371
372 tqp = arg;
373 tq = *tqp;
374 TQ_LOCK(tq);
375 do {
376 taskqueue_run(tq);
377 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
378 } while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0);
379
380 /* rendezvous with thread that asked us to terminate */
381 tq->tq_pcount--;
382 wakeup_one(tq->tq_pproc);
383 TQ_UNLOCK(tq);
384 kthread_exit(0);
385 }
386
387 void
388 taskqueue_thread_enqueue(void *context)
389 {
390 struct taskqueue **tqp, *tq;
391
392 tqp = context;
393 tq = *tqp;
394
395 mtx_assert(&tq->tq_mutex, MA_OWNED);
396 wakeup_one(tq);
397 }
398
399 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
400 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
401 INTR_MPSAFE, &taskqueue_ih));
402
403 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
404 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
405 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
406
407 TASKQUEUE_DEFINE_THREAD(thread);
408
409 struct taskqueue *
410 taskqueue_create_fast(const char *name, int mflags,
411 taskqueue_enqueue_fn enqueue, void *context)
412 {
413 return _taskqueue_create(name, mflags, enqueue, context,
414 MTX_SPIN, "fast_taskqueue");
415 }
416
417 /* NB: for backwards compatibility */
418 int
419 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
420 {
421 return taskqueue_enqueue(queue, task);
422 }
423
424 static void *taskqueue_fast_ih;
425
426 static void
427 taskqueue_fast_enqueue(void *context)
428 {
429 swi_sched(taskqueue_fast_ih, 0);
430 }
431
432 static void
433 taskqueue_fast_run(void *dummy)
434 {
435 taskqueue_run(taskqueue_fast);
436 }
437
438 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, 0,
439 swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
440 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
Cache object: e7b2ceb8b14ddad31dcdc90eefc844ac
|