subr_taskqueue.c revision 154167
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: head/sys/kern/subr_taskqueue.c 154167 2006-01-10 06:31:12Z scottl $");
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/kthread.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/mutex.h>
39#include <sys/proc.h>
40#include <sys/taskqueue.h>
41#include <sys/unistd.h>
42
43static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
44static void	*taskqueue_giant_ih;
45static void	*taskqueue_ih;
46static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
47static struct mtx taskqueue_queues_mutex;
48
49struct taskqueue {
50	STAILQ_ENTRY(taskqueue)	tq_link;
51	STAILQ_HEAD(, task)	tq_queue;
52	const char		*tq_name;
53	taskqueue_enqueue_fn	tq_enqueue;
54	void			*tq_context;
55	struct task		*tq_running;
56	struct mtx		tq_mutex;
57	struct proc		**tq_pproc;
58	int			tq_spin;
59};
60
61static __inline void
62TQ_LOCK(struct taskqueue *tq)
63{
64	if (tq->tq_spin)
65		mtx_lock_spin(&tq->tq_mutex);
66	else
67		mtx_lock(&tq->tq_mutex);
68}
69
70static __inline void
71TQ_UNLOCK(struct taskqueue *tq)
72{
73	if (tq->tq_spin)
74		mtx_unlock_spin(&tq->tq_mutex);
75	else
76		mtx_unlock(&tq->tq_mutex);
77}
78
79static void	init_taskqueue_list(void *data);
80
81static __inline int
82TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
83    int t)
84{
85	if (tq->tq_spin)
86		return (msleep_spin(p, m, wm, t));
87	return (msleep(p, m, pri, wm, t));
88}
89
90static void
91init_taskqueue_list(void *data __unused)
92{
93
94	mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
95	STAILQ_INIT(&taskqueue_queues);
96}
97SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
98    NULL);
99
100static struct taskqueue *
101_taskqueue_create(const char *name, int mflags,
102		 taskqueue_enqueue_fn enqueue, void *context,
103		 struct proc **pp,
104		 int mtxflags, const char *mtxname)
105{
106	struct taskqueue *queue;
107
108	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
109	if (!queue)
110		return 0;
111
112	STAILQ_INIT(&queue->tq_queue);
113	queue->tq_name = name;
114	queue->tq_enqueue = enqueue;
115	queue->tq_context = context;
116	queue->tq_pproc = pp;
117	queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
118	mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
119
120	mtx_lock(&taskqueue_queues_mutex);
121	STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
122	mtx_unlock(&taskqueue_queues_mutex);
123
124	return queue;
125}
126
127struct taskqueue *
128taskqueue_create(const char *name, int mflags,
129		 taskqueue_enqueue_fn enqueue, void *context,
130		 struct proc **pp)
131{
132	return _taskqueue_create(name, mflags, enqueue, context, pp,
133			MTX_DEF, "taskqueue");
134}
135
136/*
137 * Signal a taskqueue thread to terminate.
138 */
139static void
140taskqueue_terminate(struct proc **pp, struct taskqueue *tq)
141{
142	struct proc *p;
143
144	p = *pp;
145	*pp = NULL;
146	if (p) {
147		wakeup_one(tq);
148		PROC_LOCK(p);		/* NB: insure we don't miss wakeup */
149		TQ_UNLOCK(tq);		/* let taskqueue thread run */
150		TQ_SLEEP(tq, p, &p->p_mtx, PWAIT, "taskqueue_destroy", 0);
151		PROC_UNLOCK(p);
152		TQ_LOCK(tq);
153	}
154}
155
156void
157taskqueue_free(struct taskqueue *queue)
158{
159
160	mtx_lock(&taskqueue_queues_mutex);
161	STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
162	mtx_unlock(&taskqueue_queues_mutex);
163
164	TQ_LOCK(queue);
165	taskqueue_run(queue);
166	taskqueue_terminate(queue->tq_pproc, queue);
167	mtx_destroy(&queue->tq_mutex);
168	free(queue, M_TASKQUEUE);
169}
170
171/*
172 * Returns with the taskqueue locked.
173 */
174struct taskqueue *
175taskqueue_find(const char *name)
176{
177	struct taskqueue *queue;
178
179	mtx_lock(&taskqueue_queues_mutex);
180	STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
181		if (strcmp(queue->tq_name, name) == 0) {
182			TQ_LOCK(queue);
183			mtx_unlock(&taskqueue_queues_mutex);
184			return queue;
185		}
186	}
187	mtx_unlock(&taskqueue_queues_mutex);
188	return NULL;
189}
190
191int
192taskqueue_enqueue(struct taskqueue *queue, struct task *task)
193{
194	struct task *ins;
195	struct task *prev;
196
197	TQ_LOCK(queue);
198
199	/*
200	 * Count multiple enqueues.
201	 */
202	if (task->ta_pending) {
203		task->ta_pending++;
204		TQ_UNLOCK(queue);
205		return 0;
206	}
207
208	/*
209	 * Optimise the case when all tasks have the same priority.
210	 */
211	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
212	if (!prev || prev->ta_priority >= task->ta_priority) {
213		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
214	} else {
215		prev = 0;
216		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
217		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
218			if (ins->ta_priority < task->ta_priority)
219				break;
220
221		if (prev)
222			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
223		else
224			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
225	}
226
227	task->ta_pending = 1;
228	queue->tq_enqueue(queue->tq_context);
229
230	TQ_UNLOCK(queue);
231
232	return 0;
233}
234
235void
236taskqueue_run(struct taskqueue *queue)
237{
238	struct task *task;
239	int owned, pending;
240
241	owned = mtx_owned(&queue->tq_mutex);
242	if (!owned)
243		TQ_LOCK(queue);
244	while (STAILQ_FIRST(&queue->tq_queue)) {
245		/*
246		 * Carefully remove the first task from the queue and
247		 * zero its pending count.
248		 */
249		task = STAILQ_FIRST(&queue->tq_queue);
250		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
251		pending = task->ta_pending;
252		task->ta_pending = 0;
253		queue->tq_running = task;
254		TQ_UNLOCK(queue);
255
256		task->ta_func(task->ta_context, pending);
257
258		TQ_LOCK(queue);
259		queue->tq_running = NULL;
260		wakeup(task);
261	}
262
263	/*
264	 * For compatibility, unlock on return if the queue was not locked
265	 * on entry, although this opens a race window.
266	 */
267	if (!owned)
268		TQ_UNLOCK(queue);
269}
270
271void
272taskqueue_drain(struct taskqueue *queue, struct task *task)
273{
274	if (queue->tq_spin) {		/* XXX */
275		mtx_lock_spin(&queue->tq_mutex);
276		while (task->ta_pending != 0 || task == queue->tq_running)
277			msleep_spin(task, &queue->tq_mutex, "-", 0);
278		mtx_unlock_spin(&queue->tq_mutex);
279	} else {
280		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
281
282		mtx_lock(&queue->tq_mutex);
283		while (task->ta_pending != 0 || task == queue->tq_running)
284			msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
285		mtx_unlock(&queue->tq_mutex);
286	}
287}
288
289static void
290taskqueue_swi_enqueue(void *context)
291{
292	swi_sched(taskqueue_ih, 0);
293}
294
295static void
296taskqueue_swi_run(void *dummy)
297{
298	taskqueue_run(taskqueue_swi);
299}
300
301static void
302taskqueue_swi_giant_enqueue(void *context)
303{
304	swi_sched(taskqueue_giant_ih, 0);
305}
306
307static void
308taskqueue_swi_giant_run(void *dummy)
309{
310	taskqueue_run(taskqueue_swi_giant);
311}
312
313void
314taskqueue_thread_loop(void *arg)
315{
316	struct taskqueue **tqp, *tq;
317
318	tqp = arg;
319	tq = *tqp;
320	TQ_LOCK(tq);
321	do {
322		taskqueue_run(tq);
323		TQ_SLEEP(tq, tq, &tq->tq_mutex, curthread->td_priority, "-", 0);
324	} while (*tq->tq_pproc != NULL);
325
326	/* rendezvous with thread that asked us to terminate */
327	wakeup_one(tq);
328	TQ_UNLOCK(tq);
329	kthread_exit(0);
330}
331
332void
333taskqueue_thread_enqueue(void *context)
334{
335	struct taskqueue **tqp, *tq;
336
337	tqp = context;
338	tq = *tqp;
339
340	mtx_assert(&tq->tq_mutex, MA_OWNED);
341	wakeup_one(tq);
342}
343
344TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
345		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
346		     INTR_MPSAFE, &taskqueue_ih));
347
348TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
349		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
350		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
351
352TASKQUEUE_DEFINE_THREAD(thread);
353
354struct taskqueue *
355taskqueue_create_fast(const char *name, int mflags,
356		 taskqueue_enqueue_fn enqueue, void *context,
357		 struct proc **pp)
358{
359	return _taskqueue_create(name, mflags, enqueue, context, pp,
360			MTX_SPIN, "fast_taskqueue");
361}
362
363/* NB: for backwards compatibility */
364int
365taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
366{
367	return taskqueue_enqueue(queue, task);
368}
369
370static void	*taskqueue_fast_ih;
371
372static void
373taskqueue_fast_enqueue(void *context)
374{
375	swi_sched(taskqueue_fast_ih, 0);
376}
377
378static void
379taskqueue_fast_run(void *dummy)
380{
381	taskqueue_run(taskqueue_fast);
382}
383
384TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, 0,
385	swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
386	SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
387