xref: /freebsd/sys/kern/subr_taskqueue.c (revision c69db88340f9a7659ab4faa7fd328a3fb858b72e)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/taskqueue.h>
39 
40 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
41 
42 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
43 
44 static void	*taskqueue_ih;
45 static void	*taskqueue_giant_ih;
46 static struct mtx taskqueue_queues_mutex;
47 
48 struct taskqueue {
49 	STAILQ_ENTRY(taskqueue)	tq_link;
50 	STAILQ_HEAD(, task)	tq_queue;
51 	const char		*tq_name;
52 	taskqueue_enqueue_fn	tq_enqueue;
53 	void			*tq_context;
54 	int			tq_draining;
55 	struct mtx		tq_mutex;
56 };
57 
58 static void	init_taskqueue_list(void *data);
59 
60 static void
61 init_taskqueue_list(void *data __unused)
62 {
63 
64 	mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
65 	STAILQ_INIT(&taskqueue_queues);
66 }
67 SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
68     NULL);
69 
70 struct taskqueue *
71 taskqueue_create(const char *name, int mflags,
72 		 taskqueue_enqueue_fn enqueue, void *context)
73 {
74 	struct taskqueue *queue;
75 
76 	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
77 	if (!queue)
78 		return 0;
79 
80 	STAILQ_INIT(&queue->tq_queue);
81 	queue->tq_name = name;
82 	queue->tq_enqueue = enqueue;
83 	queue->tq_context = context;
84 	queue->tq_draining = 0;
85 	mtx_init(&queue->tq_mutex, "taskqueue", NULL, MTX_DEF);
86 
87 	mtx_lock(&taskqueue_queues_mutex);
88 	STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
89 	mtx_unlock(&taskqueue_queues_mutex);
90 
91 	return queue;
92 }
93 
94 void
95 taskqueue_free(struct taskqueue *queue)
96 {
97 
98 	mtx_lock(&queue->tq_mutex);
99 	KASSERT(queue->tq_draining == 0, ("free'ing a draining taskqueue"));
100 	queue->tq_draining = 1;
101 	mtx_unlock(&queue->tq_mutex);
102 
103 	taskqueue_run(queue);
104 
105 	mtx_lock(&taskqueue_queues_mutex);
106 	STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
107 	mtx_unlock(&taskqueue_queues_mutex);
108 
109 	mtx_destroy(&queue->tq_mutex);
110 	free(queue, M_TASKQUEUE);
111 }
112 
113 /*
114  * Returns with the taskqueue locked.
115  */
116 struct taskqueue *
117 taskqueue_find(const char *name)
118 {
119 	struct taskqueue *queue;
120 
121 	mtx_lock(&taskqueue_queues_mutex);
122 	STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
123 		mtx_lock(&queue->tq_mutex);
124 		if (!strcmp(queue->tq_name, name)) {
125 			mtx_unlock(&taskqueue_queues_mutex);
126 			return queue;
127 		}
128 		mtx_unlock(&queue->tq_mutex);
129 	}
130 	mtx_unlock(&taskqueue_queues_mutex);
131 	return 0;
132 }
133 
134 int
135 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
136 {
137 	struct task *ins;
138 	struct task *prev;
139 
140 	mtx_lock(&queue->tq_mutex);
141 
142 	/*
143 	 * Don't allow new tasks on a queue which is being freed.
144 	 */
145 	if (queue->tq_draining) {
146 		mtx_unlock(&queue->tq_mutex);
147 		return EPIPE;
148 	}
149 
150 	/*
151 	 * Count multiple enqueues.
152 	 */
153 	if (task->ta_pending) {
154 		task->ta_pending++;
155 		mtx_unlock(&queue->tq_mutex);
156 		return 0;
157 	}
158 
159 	/*
160 	 * Optimise the case when all tasks have the same priority.
161 	 */
162 	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
163 	if (!prev || prev->ta_priority >= task->ta_priority) {
164 		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
165 	} else {
166 		prev = 0;
167 		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
168 		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
169 			if (ins->ta_priority < task->ta_priority)
170 				break;
171 
172 		if (prev)
173 			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
174 		else
175 			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
176 	}
177 
178 	task->ta_pending = 1;
179 	if (queue->tq_enqueue)
180 		queue->tq_enqueue(queue->tq_context);
181 
182 	mtx_unlock(&queue->tq_mutex);
183 
184 	return 0;
185 }
186 
187 void
188 taskqueue_run(struct taskqueue *queue)
189 {
190 	struct task *task;
191 	int pending;
192 
193 	mtx_lock(&queue->tq_mutex);
194 	while (STAILQ_FIRST(&queue->tq_queue)) {
195 		/*
196 		 * Carefully remove the first task from the queue and
197 		 * zero its pending count.
198 		 */
199 		task = STAILQ_FIRST(&queue->tq_queue);
200 		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
201 		pending = task->ta_pending;
202 		task->ta_pending = 0;
203 		mtx_unlock(&queue->tq_mutex);
204 
205 		task->ta_func(task->ta_context, pending);
206 
207 		mtx_lock(&queue->tq_mutex);
208 	}
209 	mtx_unlock(&queue->tq_mutex);
210 }
211 
212 static void
213 taskqueue_swi_enqueue(void *context)
214 {
215 	swi_sched(taskqueue_ih, 0);
216 }
217 
218 static void
219 taskqueue_swi_run(void *dummy)
220 {
221 	taskqueue_run(taskqueue_swi);
222 }
223 
224 static void
225 taskqueue_swi_giant_enqueue(void *context)
226 {
227 	swi_sched(taskqueue_giant_ih, 0);
228 }
229 
230 static void
231 taskqueue_swi_giant_run(void *dummy)
232 {
233 	taskqueue_run(taskqueue_swi_giant);
234 }
235 
236 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
237 		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
238 		     INTR_MPSAFE, &taskqueue_ih));
239 
240 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
241 		 swi_add(NULL, "Giant task queue", taskqueue_swi_giant_run,
242 		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
243