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
26 * $FreeBSD: src/sys/kern/subr_taskqueue.c,v 1.1.2.2 2001/03/31 03:33:44 hsu Exp $
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/taskqueue.h>
34 #include <sys/interrupt.h>
35 #include <sys/malloc.h>
36 #include <machine/ipl.h>
38 MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
40 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
43 STAILQ_ENTRY(taskqueue) tq_link;
44 STAILQ_HEAD(, task) tq_queue;
46 taskqueue_enqueue_fn tq_enqueue;
52 taskqueue_create(const char *name, int mflags,
53 taskqueue_enqueue_fn enqueue, void *context)
55 struct taskqueue *queue;
59 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags);
62 STAILQ_INIT(&queue->tq_queue);
63 queue->tq_name = name;
64 queue->tq_enqueue = enqueue;
65 queue->tq_context = context;
66 queue->tq_draining = 0;
70 STAILQ_INIT(&taskqueue_queues);
73 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
80 taskqueue_free(struct taskqueue *queue)
83 queue->tq_draining = 1;
89 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
92 free(queue, M_TASKQUEUE);
96 taskqueue_find(const char *name)
98 struct taskqueue *queue;
102 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link)
103 if (!strcmp(queue->tq_name, name)) {
112 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
120 * Don't allow new tasks on a queue which is being freed.
122 if (queue->tq_draining) {
128 * Count multiple enqueues.
130 if (task->ta_pending) {
137 * Optimise the case when all tasks have the same priority.
139 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
140 if (!prev || prev->ta_priority >= task->ta_priority) {
141 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
144 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
145 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
146 if (ins->ta_priority < task->ta_priority)
150 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
152 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
155 task->ta_pending = 1;
156 if (queue->tq_enqueue)
157 queue->tq_enqueue(queue->tq_context);
165 taskqueue_run(struct taskqueue *queue)
172 while (STAILQ_FIRST(&queue->tq_queue)) {
174 * Carefully remove the first task from the queue and
175 * zero its pending count.
177 task = STAILQ_FIRST(&queue->tq_queue);
178 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
179 pending = task->ta_pending;
180 task->ta_pending = 0;
183 task->ta_func(task->ta_context, pending);
191 taskqueue_swi_enqueue(void *context)
197 taskqueue_swi_run(void)
199 taskqueue_run(taskqueue_swi);
202 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
203 register_swi(SWI_TQ, taskqueue_swi_run));