2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/sched.h>
42 #include <sys/taskqueue.h>
43 #include <sys/unistd.h>
44 #include <machine/stdarg.h>
46 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
47 static void *taskqueue_giant_ih;
48 static void *taskqueue_ih;
50 struct taskqueue_busy {
51 struct task *tb_running;
52 TAILQ_ENTRY(taskqueue_busy) tb_link;
56 STAILQ_HEAD(, task) tq_queue;
57 taskqueue_enqueue_fn tq_enqueue;
59 TAILQ_HEAD(, taskqueue_busy) tq_active;
61 struct thread **tq_threads;
67 #define TQ_FLAGS_ACTIVE (1 << 0)
68 #define TQ_FLAGS_BLOCKED (1 << 1)
69 #define TQ_FLAGS_PENDING (1 << 2)
71 static void taskqueue_run_locked(struct taskqueue *);
74 TQ_LOCK(struct taskqueue *tq)
77 mtx_lock_spin(&tq->tq_mutex);
79 mtx_lock(&tq->tq_mutex);
83 TQ_UNLOCK(struct taskqueue *tq)
86 mtx_unlock_spin(&tq->tq_mutex);
88 mtx_unlock(&tq->tq_mutex);
92 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
96 return (msleep_spin(p, m, wm, t));
97 return (msleep(p, m, pri, wm, t));
100 static struct taskqueue *
101 _taskqueue_create(const char *name __unused, int mflags,
102 taskqueue_enqueue_fn enqueue, void *context,
103 int mtxflags, const char *mtxname)
105 struct taskqueue *queue;
107 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
111 STAILQ_INIT(&queue->tq_queue);
112 TAILQ_INIT(&queue->tq_active);
113 queue->tq_enqueue = enqueue;
114 queue->tq_context = context;
115 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
116 queue->tq_flags |= TQ_FLAGS_ACTIVE;
117 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
123 taskqueue_create(const char *name, int mflags,
124 taskqueue_enqueue_fn enqueue, void *context)
126 return _taskqueue_create(name, mflags, enqueue, context,
127 MTX_DEF, "taskqueue");
131 * Signal a taskqueue thread to terminate.
134 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
137 while (tq->tq_tcount > 0) {
139 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
144 taskqueue_free(struct taskqueue *queue)
148 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
149 taskqueue_run_locked(queue);
150 taskqueue_terminate(queue->tq_threads, queue);
151 KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
152 mtx_destroy(&queue->tq_mutex);
153 free(queue->tq_threads, M_TASKQUEUE);
154 free(queue, M_TASKQUEUE);
158 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
166 * Count multiple enqueues.
168 if (task->ta_pending) {
169 if (task->ta_pending < USHRT_MAX)
176 * Optimise the case when all tasks have the same priority.
178 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
179 if (!prev || prev->ta_priority >= task->ta_priority) {
180 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
183 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
184 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
185 if (ins->ta_priority < task->ta_priority)
189 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
191 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
194 task->ta_pending = 1;
195 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
196 queue->tq_enqueue(queue->tq_context);
198 queue->tq_flags |= TQ_FLAGS_PENDING;
206 taskqueue_block(struct taskqueue *queue)
210 queue->tq_flags |= TQ_FLAGS_BLOCKED;
215 taskqueue_unblock(struct taskqueue *queue)
219 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
220 if (queue->tq_flags & TQ_FLAGS_PENDING) {
221 queue->tq_flags &= ~TQ_FLAGS_PENDING;
222 queue->tq_enqueue(queue->tq_context);
228 taskqueue_run_locked(struct taskqueue *queue)
230 struct taskqueue_busy tb;
234 mtx_assert(&queue->tq_mutex, MA_OWNED);
235 tb.tb_running = NULL;
236 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
238 while (STAILQ_FIRST(&queue->tq_queue)) {
240 * Carefully remove the first task from the queue and
241 * zero its pending count.
243 task = STAILQ_FIRST(&queue->tq_queue);
244 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
245 pending = task->ta_pending;
246 task->ta_pending = 0;
247 tb.tb_running = task;
250 task->ta_func(task->ta_context, pending);
253 tb.tb_running = NULL;
256 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
260 taskqueue_run(struct taskqueue *queue)
264 taskqueue_run_locked(queue);
269 task_is_running(struct taskqueue *queue, struct task *task)
271 struct taskqueue_busy *tb;
273 mtx_assert(&queue->tq_mutex, MA_OWNED);
274 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
275 if (tb->tb_running == task)
282 taskqueue_drain(struct taskqueue *queue, struct task *task)
284 if (queue->tq_spin) { /* XXX */
285 mtx_lock_spin(&queue->tq_mutex);
286 while (task->ta_pending != 0 || task_is_running(queue, task))
287 msleep_spin(task, &queue->tq_mutex, "-", 0);
288 mtx_unlock_spin(&queue->tq_mutex);
290 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
292 mtx_lock(&queue->tq_mutex);
293 while (task->ta_pending != 0 || task_is_running(queue, task))
294 msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
295 mtx_unlock(&queue->tq_mutex);
300 taskqueue_swi_enqueue(void *context)
302 swi_sched(taskqueue_ih, 0);
306 taskqueue_swi_run(void *dummy)
308 taskqueue_run(taskqueue_swi);
312 taskqueue_swi_giant_enqueue(void *context)
314 swi_sched(taskqueue_giant_ih, 0);
318 taskqueue_swi_giant_run(void *dummy)
320 taskqueue_run(taskqueue_swi_giant);
324 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
325 const char *name, ...)
329 struct taskqueue *tq;
331 char ktname[MAXCOMLEN + 1];
339 vsnprintf(ktname, sizeof(ktname), name, ap);
342 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
344 if (tq->tq_threads == NULL) {
345 printf("%s: no memory for %s threads\n", __func__, ktname);
349 for (i = 0; i < count; i++) {
351 error = kthread_add(taskqueue_thread_loop, tqp, NULL,
352 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
354 error = kthread_add(taskqueue_thread_loop, tqp, NULL,
355 &tq->tq_threads[i], RFSTOPPED, 0,
358 /* should be ok to continue, taskqueue_free will dtrt */
359 printf("%s: kthread_add(%s): error %d", __func__,
361 tq->tq_threads[i] = NULL; /* paranoid */
365 for (i = 0; i < count; i++) {
366 if (tq->tq_threads[i] == NULL)
368 td = tq->tq_threads[i];
371 sched_add(td, SRQ_BORING);
379 taskqueue_thread_loop(void *arg)
381 struct taskqueue **tqp, *tq;
386 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
387 taskqueue_run_locked(tq);
389 * Because taskqueue_run() can drop tq_mutex, we need to
390 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
391 * meantime, which means we missed a wakeup.
393 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
395 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
398 /* rendezvous with thread that asked us to terminate */
400 wakeup_one(tq->tq_threads);
406 taskqueue_thread_enqueue(void *context)
408 struct taskqueue **tqp, *tq;
413 mtx_assert(&tq->tq_mutex, MA_OWNED);
417 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
418 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
419 INTR_MPSAFE, &taskqueue_ih));
421 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
422 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
423 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
425 TASKQUEUE_DEFINE_THREAD(thread);
428 taskqueue_create_fast(const char *name, int mflags,
429 taskqueue_enqueue_fn enqueue, void *context)
431 return _taskqueue_create(name, mflags, enqueue, context,
432 MTX_SPIN, "fast_taskqueue");
435 /* NB: for backwards compatibility */
437 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
439 return taskqueue_enqueue(queue, task);
442 static void *taskqueue_fast_ih;
445 taskqueue_fast_enqueue(void *context)
447 swi_sched(taskqueue_fast_ih, 0);
451 taskqueue_fast_run(void *dummy)
453 taskqueue_run(taskqueue_fast);
456 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
457 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
458 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
461 taskqueue_member(struct taskqueue *queue, struct thread *td)
466 for (i = 0, j = 0; ; i++) {
467 if (queue->tq_threads[i] == NULL)
469 if (queue->tq_threads[i] == td) {
473 if (++j >= queue->tq_tcount)