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;
68 #define TQ_FLAGS_ACTIVE (1 << 0)
69 #define TQ_FLAGS_BLOCKED (1 << 1)
70 #define TQ_FLAGS_PENDING (1 << 2)
72 #define DT_CALLOUT_ARMED (1 << 0)
73 #define DT_DRAIN_IN_PROGRESS (1 << 1)
78 mtx_lock_spin(&(tq)->tq_mutex); \
80 mtx_lock(&(tq)->tq_mutex); \
83 #define TQ_UNLOCK(tq) \
86 mtx_unlock_spin(&(tq)->tq_mutex); \
88 mtx_unlock(&(tq)->tq_mutex); \
92 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
93 int priority, task_fn_t func, void *context)
96 TASK_INIT(&timeout_task->t, priority, func, context);
97 callout_init_mtx(&timeout_task->c, &queue->tq_mutex, 0);
98 timeout_task->q = queue;
103 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
107 return (msleep_spin(p, m, wm, t));
108 return (msleep(p, m, pri, wm, t));
111 static struct taskqueue *
112 _taskqueue_create(const char *name __unused, int mflags,
113 taskqueue_enqueue_fn enqueue, void *context,
114 int mtxflags, const char *mtxname)
116 struct taskqueue *queue;
118 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
122 STAILQ_INIT(&queue->tq_queue);
123 TAILQ_INIT(&queue->tq_active);
124 queue->tq_enqueue = enqueue;
125 queue->tq_context = context;
126 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
127 queue->tq_flags |= TQ_FLAGS_ACTIVE;
128 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
134 taskqueue_create(const char *name, int mflags,
135 taskqueue_enqueue_fn enqueue, void *context)
137 return _taskqueue_create(name, mflags, enqueue, context,
138 MTX_DEF, "taskqueue");
142 * Signal a taskqueue thread to terminate.
145 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
148 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
150 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
155 taskqueue_free(struct taskqueue *queue)
159 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
160 taskqueue_terminate(queue->tq_threads, queue);
161 KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
162 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
163 mtx_destroy(&queue->tq_mutex);
164 free(queue->tq_threads, M_TASKQUEUE);
165 free(queue, M_TASKQUEUE);
169 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
175 * Count multiple enqueues.
177 if (task->ta_pending) {
178 if (task->ta_pending < USHRT_MAX)
184 * Optimise the case when all tasks have the same priority.
186 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
187 if (!prev || prev->ta_priority >= task->ta_priority) {
188 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
191 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
192 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
193 if (ins->ta_priority < task->ta_priority)
197 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
199 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
202 task->ta_pending = 1;
203 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
204 queue->tq_enqueue(queue->tq_context);
206 queue->tq_flags |= TQ_FLAGS_PENDING;
211 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
216 res = taskqueue_enqueue_locked(queue, task);
223 taskqueue_timeout_func(void *arg)
225 struct taskqueue *queue;
226 struct timeout_task *timeout_task;
229 queue = timeout_task->q;
230 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
231 timeout_task->f &= ~DT_CALLOUT_ARMED;
232 queue->tq_callouts--;
233 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
237 taskqueue_enqueue_timeout(struct taskqueue *queue,
238 struct timeout_task *timeout_task, int ticks)
243 KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
245 KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
246 timeout_task->q = queue;
247 res = timeout_task->t.ta_pending;
248 if (timeout_task->f & DT_DRAIN_IN_PROGRESS) {
252 } else if (ticks == 0) {
253 taskqueue_enqueue_locked(queue, &timeout_task->t);
255 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
258 queue->tq_callouts++;
259 timeout_task->f |= DT_CALLOUT_ARMED;
261 ticks = -ticks; /* Ignore overflow. */
264 callout_reset(&timeout_task->c, ticks,
265 taskqueue_timeout_func, timeout_task);
273 taskqueue_drain_running(struct taskqueue *queue)
276 while (!TAILQ_EMPTY(&queue->tq_active))
277 TQ_SLEEP(queue, &queue->tq_active, &queue->tq_mutex,
282 taskqueue_block(struct taskqueue *queue)
286 queue->tq_flags |= TQ_FLAGS_BLOCKED;
291 taskqueue_unblock(struct taskqueue *queue)
295 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
296 if (queue->tq_flags & TQ_FLAGS_PENDING) {
297 queue->tq_flags &= ~TQ_FLAGS_PENDING;
298 queue->tq_enqueue(queue->tq_context);
304 taskqueue_run_locked(struct taskqueue *queue)
306 struct taskqueue_busy tb;
310 mtx_assert(&queue->tq_mutex, MA_OWNED);
311 tb.tb_running = NULL;
312 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
314 while (STAILQ_FIRST(&queue->tq_queue)) {
316 * Carefully remove the first task from the queue and
317 * zero its pending count.
319 task = STAILQ_FIRST(&queue->tq_queue);
320 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
321 pending = task->ta_pending;
322 task->ta_pending = 0;
323 tb.tb_running = task;
326 task->ta_func(task->ta_context, pending);
329 tb.tb_running = NULL;
332 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
333 if (TAILQ_EMPTY(&queue->tq_active))
334 wakeup(&queue->tq_active);
338 taskqueue_run(struct taskqueue *queue)
342 taskqueue_run_locked(queue);
347 task_is_running(struct taskqueue *queue, struct task *task)
349 struct taskqueue_busy *tb;
351 mtx_assert(&queue->tq_mutex, MA_OWNED);
352 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
353 if (tb->tb_running == task)
360 * Only use this function in single threaded contexts. It returns
361 * non-zero if the given task is either pending or running. Else the
362 * task is idle and can be queued again or freed.
365 taskqueue_poll_is_busy(struct taskqueue *queue, struct task *task)
370 retval = task->ta_pending > 0 || task_is_running(queue, task);
377 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
381 if (task->ta_pending > 0)
382 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
384 *pendp = task->ta_pending;
385 task->ta_pending = 0;
386 return (task_is_running(queue, task) ? EBUSY : 0);
390 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
395 error = taskqueue_cancel_locked(queue, task, pendp);
402 taskqueue_cancel_timeout(struct taskqueue *queue,
403 struct timeout_task *timeout_task, u_int *pendp)
405 u_int pending, pending1;
409 pending = !!callout_stop(&timeout_task->c);
410 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
411 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
412 timeout_task->f &= ~DT_CALLOUT_ARMED;
413 queue->tq_callouts--;
418 *pendp = pending + pending1;
423 taskqueue_drain(struct taskqueue *queue, struct task *task)
427 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
430 while (task->ta_pending != 0 || task_is_running(queue, task))
431 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
436 taskqueue_drain_all(struct taskqueue *queue)
441 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
444 task = STAILQ_LAST(&queue->tq_queue, task, ta_link);
446 while (task->ta_pending != 0)
447 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
448 taskqueue_drain_running(queue);
449 KASSERT(STAILQ_EMPTY(&queue->tq_queue),
450 ("taskqueue queue is not empty after draining"));
455 taskqueue_drain_timeout(struct taskqueue *queue,
456 struct timeout_task *timeout_task)
460 * Set flag to prevent timer from re-starting during drain:
463 KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0,
464 ("Drain already in progress"));
465 timeout_task->f |= DT_DRAIN_IN_PROGRESS;
468 callout_drain(&timeout_task->c);
469 taskqueue_drain(queue, &timeout_task->t);
472 * Clear flag to allow timer to re-start:
475 timeout_task->f &= ~DT_DRAIN_IN_PROGRESS;
480 taskqueue_swi_enqueue(void *context)
482 swi_sched(taskqueue_ih, 0);
486 taskqueue_swi_run(void *dummy)
488 taskqueue_run(taskqueue_swi);
492 taskqueue_swi_giant_enqueue(void *context)
494 swi_sched(taskqueue_giant_ih, 0);
498 taskqueue_swi_giant_run(void *dummy)
500 taskqueue_run(taskqueue_swi_giant);
504 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
505 const char *name, ...)
509 struct taskqueue *tq;
511 char ktname[MAXCOMLEN + 1];
519 vsnprintf(ktname, sizeof(ktname), name, ap);
522 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
524 if (tq->tq_threads == NULL) {
525 printf("%s: no memory for %s threads\n", __func__, ktname);
529 for (i = 0; i < count; i++) {
531 error = kthread_add(taskqueue_thread_loop, tqp, NULL,
532 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
534 error = kthread_add(taskqueue_thread_loop, tqp, NULL,
535 &tq->tq_threads[i], RFSTOPPED, 0,
538 /* should be ok to continue, taskqueue_free will dtrt */
539 printf("%s: kthread_add(%s): error %d", __func__,
541 tq->tq_threads[i] = NULL; /* paranoid */
545 for (i = 0; i < count; i++) {
546 if (tq->tq_threads[i] == NULL)
548 td = tq->tq_threads[i];
551 sched_add(td, SRQ_BORING);
559 taskqueue_thread_loop(void *arg)
561 struct taskqueue **tqp, *tq;
566 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
567 taskqueue_run_locked(tq);
569 * Because taskqueue_run() can drop tq_mutex, we need to
570 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
571 * meantime, which means we missed a wakeup.
573 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
575 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
577 taskqueue_run_locked(tq);
579 /* rendezvous with thread that asked us to terminate */
581 wakeup_one(tq->tq_threads);
587 taskqueue_thread_enqueue(void *context)
589 struct taskqueue **tqp, *tq;
594 mtx_assert(&tq->tq_mutex, MA_OWNED);
598 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
599 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
600 INTR_MPSAFE, &taskqueue_ih));
602 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
603 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
604 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
606 TASKQUEUE_DEFINE_THREAD(thread);
609 taskqueue_create_fast(const char *name, int mflags,
610 taskqueue_enqueue_fn enqueue, void *context)
612 return _taskqueue_create(name, mflags, enqueue, context,
613 MTX_SPIN, "fast_taskqueue");
616 /* NB: for backwards compatibility */
618 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
620 return taskqueue_enqueue(queue, task);
623 static void *taskqueue_fast_ih;
626 taskqueue_fast_enqueue(void *context)
628 swi_sched(taskqueue_fast_ih, 0);
632 taskqueue_fast_run(void *dummy)
634 taskqueue_run(taskqueue_fast);
637 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
638 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
639 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
642 taskqueue_member(struct taskqueue *queue, struct thread *td)
646 for (i = 0, j = 0; ; i++) {
647 if (queue->tq_threads[i] == NULL)
649 if (queue->tq_threads[i] == td) {
653 if (++j >= queue->tq_tcount)