xref: /freebsd/sys/kern/subr_taskqueue.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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  *	$FreeBSD$
27  */
28 
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>
37 
38 MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
39 
40 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
41 
42 struct taskqueue {
43 	STAILQ_ENTRY(taskqueue)	tq_link;
44 	STAILQ_HEAD(, task)	tq_queue;
45 	const char		*tq_name;
46 	taskqueue_enqueue_fn	tq_enqueue;
47 	void			*tq_context;
48 	int			tq_draining;
49 };
50 
51 struct taskqueue *
52 taskqueue_create(const char *name, int mflags,
53 		 taskqueue_enqueue_fn enqueue, void *context)
54 {
55 	struct taskqueue *queue;
56 	static int once = 1;
57 	int s;
58 
59 	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags);
60 	if (!queue)
61 		return 0;
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;
67 
68 	s = splhigh();
69 	if (once) {
70 		STAILQ_INIT(&taskqueue_queues);
71 		once = 0;
72 	}
73 	STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
74 	splx(s);
75 
76 	return queue;
77 }
78 
79 void
80 taskqueue_free(struct taskqueue *queue)
81 {
82 	int s = splhigh();
83 	queue->tq_draining = 1;
84 	splx(s);
85 
86 	taskqueue_run(queue);
87 
88 	s = splhigh();
89 	STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
90 	splx(s);
91 
92 	free(queue, M_TASKQUEUE);
93 }
94 
95 struct taskqueue *
96 taskqueue_find(const char *name)
97 {
98 	struct taskqueue *queue;
99 	int s;
100 
101 	s = splhigh();
102 	STAILQ_FOREACH(queue, &taskqueue_queues, tq_link)
103 		if (!strcmp(queue->tq_name, name)) {
104 			splx(s);
105 			return queue;
106 		}
107 	splx(s);
108 	return 0;
109 }
110 
111 int
112 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
113 {
114 	struct task *ins;
115 	struct task *prev;
116 
117 	int s = splhigh();
118 
119 	/*
120 	 * Don't allow new tasks on a queue which is being freed.
121 	 */
122 	if (queue->tq_draining) {
123 		splx(s);
124 		return EPIPE;
125 	}
126 
127 	/*
128 	 * Count multiple enqueues.
129 	 */
130 	if (task->ta_pending) {
131 		task->ta_pending++;
132 		splx(s);
133 		return 0;
134 	}
135 
136 	/*
137 	 * Optimise the case when all tasks have the same priority.
138 	 */
139 	prev = STAILQ_LAST(&queue->tq_queue);
140 	if (!prev || prev->ta_priority >= task->ta_priority) {
141 		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
142 	} else {
143 		prev = 0;
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)
147 				break;
148 
149 		if (prev)
150 			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
151 		else
152 			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
153 	}
154 
155 	task->ta_pending = 1;
156 	if (queue->tq_enqueue)
157 		queue->tq_enqueue(queue->tq_context);
158 
159 	splx(s);
160 
161 	return 0;
162 }
163 
164 void
165 taskqueue_run(struct taskqueue *queue)
166 {
167 	int s;
168 	struct task *task;
169 	int pending;
170 
171 	s = splhigh();
172 	while (STAILQ_FIRST(&queue->tq_queue)) {
173 		/*
174 		 * Carefully remove the first task from the queue and
175 		 * zero its pending count.
176 		 */
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;
181 		splx(s);
182 
183 		task->ta_func(task->ta_context, pending);
184 
185 		s = splhigh();
186 	}
187 	splx(s);
188 }
189 
190 static void
191 taskqueue_swi_enqueue(void *context)
192 {
193 	setsofttq();
194 }
195 
196 static void
197 taskqueue_swi_run(void)
198 {
199 	taskqueue_run(taskqueue_swi);
200 }
201 
202 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
203 		 register_swi(SWI_TQ, taskqueue_swi_run));
204