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