subr_taskqueue.c revision 65822
161033Sdfr/*-
261033Sdfr * Copyright (c) 2000 Doug Rabson
361033Sdfr * All rights reserved.
461033Sdfr *
561033Sdfr * Redistribution and use in source and binary forms, with or without
661033Sdfr * modification, are permitted provided that the following conditions
761033Sdfr * are met:
861033Sdfr * 1. Redistributions of source code must retain the above copyright
961033Sdfr *    notice, this list of conditions and the following disclaimer.
1061033Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1161033Sdfr *    notice, this list of conditions and the following disclaimer in the
1261033Sdfr *    documentation and/or other materials provided with the distribution.
1361033Sdfr *
1461033Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1561033Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1661033Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1761033Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1861033Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1961033Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2061033Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2161033Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2261033Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2361033Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2461033Sdfr * SUCH DAMAGE.
2561033Sdfr *
2661033Sdfr *	$FreeBSD: head/sys/kern/subr_taskqueue.c 65822 2000-09-13 18:33:25Z jhb $
2761033Sdfr */
2861033Sdfr
2961033Sdfr#include <sys/param.h>
3065822Sjhb#include <sys/bus.h>
3161033Sdfr#include <sys/queue.h>
3261033Sdfr#include <sys/systm.h>
3361033Sdfr#include <sys/kernel.h>
3461033Sdfr#include <sys/taskqueue.h>
3561033Sdfr#include <sys/interrupt.h>
3661033Sdfr#include <sys/malloc.h>
3761033Sdfr#include <machine/ipl.h>
3861033Sdfr
3961033SdfrMALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
4061033Sdfr
4161033Sdfrstatic STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
4261033Sdfr
4361033Sdfrstruct taskqueue {
4461033Sdfr	STAILQ_ENTRY(taskqueue)	tq_link;
4561033Sdfr	STAILQ_HEAD(, task)	tq_queue;
4661033Sdfr	const char		*tq_name;
4761033Sdfr	taskqueue_enqueue_fn	tq_enqueue;
4861033Sdfr	void			*tq_context;
4961033Sdfr	int			tq_draining;
5061033Sdfr};
5161033Sdfr
5261033Sdfrstruct taskqueue *
5361033Sdfrtaskqueue_create(const char *name, int mflags,
5461033Sdfr		 taskqueue_enqueue_fn enqueue, void *context)
5561033Sdfr{
5661033Sdfr	struct taskqueue *queue;
5761033Sdfr	static int once = 1;
5861033Sdfr	int s;
5961033Sdfr
6061033Sdfr	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags);
6161033Sdfr	if (!queue)
6261033Sdfr		return 0;
6361033Sdfr	STAILQ_INIT(&queue->tq_queue);
6461033Sdfr	queue->tq_name = name;
6561033Sdfr	queue->tq_enqueue = enqueue;
6661033Sdfr	queue->tq_context = context;
6761033Sdfr	queue->tq_draining = 0;
6861033Sdfr
6961033Sdfr	s = splhigh();
7061033Sdfr	if (once) {
7161033Sdfr		STAILQ_INIT(&taskqueue_queues);
7261033Sdfr		once = 0;
7361033Sdfr	}
7461033Sdfr	STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
7561033Sdfr	splx(s);
7661033Sdfr
7761033Sdfr	return queue;
7861033Sdfr}
7961033Sdfr
8061033Sdfrvoid
8161033Sdfrtaskqueue_free(struct taskqueue *queue)
8261033Sdfr{
8361033Sdfr	int s = splhigh();
8461033Sdfr	queue->tq_draining = 1;
8561033Sdfr	splx(s);
8661033Sdfr
8761033Sdfr	taskqueue_run(queue);
8861033Sdfr
8961033Sdfr	s = splhigh();
9061033Sdfr	STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
9161033Sdfr	splx(s);
9261033Sdfr
9361033Sdfr	free(queue, M_TASKQUEUE);
9461033Sdfr}
9561033Sdfr
9661033Sdfrstruct taskqueue *
9761033Sdfrtaskqueue_find(const char *name)
9861033Sdfr{
9961033Sdfr	struct taskqueue *queue;
10061033Sdfr	int s;
10161033Sdfr
10261033Sdfr	s = splhigh();
10361033Sdfr	STAILQ_FOREACH(queue, &taskqueue_queues, tq_link)
10461033Sdfr		if (!strcmp(queue->tq_name, name)) {
10561033Sdfr			splx(s);
10661033Sdfr			return queue;
10761033Sdfr		}
10861033Sdfr	splx(s);
10961033Sdfr	return 0;
11061033Sdfr}
11161033Sdfr
11261033Sdfrint
11361033Sdfrtaskqueue_enqueue(struct taskqueue *queue, struct task *task)
11461033Sdfr{
11561033Sdfr	struct task *ins;
11661033Sdfr	struct task *prev;
11761033Sdfr
11861033Sdfr	int s = splhigh();
11961033Sdfr
12061033Sdfr	/*
12161033Sdfr	 * Don't allow new tasks on a queue which is being freed.
12261033Sdfr	 */
12361033Sdfr	if (queue->tq_draining) {
12461033Sdfr		splx(s);
12561033Sdfr		return EPIPE;
12661033Sdfr	}
12761033Sdfr
12861033Sdfr	/*
12961033Sdfr	 * Count multiple enqueues.
13061033Sdfr	 */
13161033Sdfr	if (task->ta_pending) {
13261033Sdfr		task->ta_pending++;
13361033Sdfr		splx(s);
13461033Sdfr		return 0;
13561033Sdfr	}
13661033Sdfr
13761033Sdfr	/*
13861033Sdfr	 * Optimise the case when all tasks have the same priority.
13961033Sdfr	 */
14064199Shsu	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
14161033Sdfr	if (!prev || prev->ta_priority >= task->ta_priority) {
14261033Sdfr		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
14361033Sdfr	} else {
14461033Sdfr		prev = 0;
14561033Sdfr		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
14661033Sdfr		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
14761033Sdfr			if (ins->ta_priority < task->ta_priority)
14861033Sdfr				break;
14961033Sdfr
15061033Sdfr		if (prev)
15161033Sdfr			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
15261033Sdfr		else
15361033Sdfr			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
15461033Sdfr	}
15561033Sdfr
15661033Sdfr	task->ta_pending = 1;
15761033Sdfr	if (queue->tq_enqueue)
15861033Sdfr		queue->tq_enqueue(queue->tq_context);
15961033Sdfr
16061033Sdfr	splx(s);
16161033Sdfr
16261033Sdfr	return 0;
16361033Sdfr}
16461033Sdfr
16561033Sdfrvoid
16661033Sdfrtaskqueue_run(struct taskqueue *queue)
16761033Sdfr{
16861033Sdfr	int s;
16961033Sdfr	struct task *task;
17061033Sdfr	int pending;
17161033Sdfr
17261033Sdfr	s = splhigh();
17361033Sdfr	while (STAILQ_FIRST(&queue->tq_queue)) {
17461033Sdfr		/*
17561033Sdfr		 * Carefully remove the first task from the queue and
17661033Sdfr		 * zero its pending count.
17761033Sdfr		 */
17861033Sdfr		task = STAILQ_FIRST(&queue->tq_queue);
17961033Sdfr		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
18061033Sdfr		pending = task->ta_pending;
18161033Sdfr		task->ta_pending = 0;
18261033Sdfr		splx(s);
18361033Sdfr
18461033Sdfr		task->ta_func(task->ta_context, pending);
18561033Sdfr
18661033Sdfr		s = splhigh();
18761033Sdfr	}
18861033Sdfr	splx(s);
18961033Sdfr}
19061033Sdfr
19161033Sdfrstatic void
19261033Sdfrtaskqueue_swi_enqueue(void *context)
19361033Sdfr{
19461033Sdfr	setsofttq();
19561033Sdfr}
19661033Sdfr
19761033Sdfrstatic void
19861033Sdfrtaskqueue_swi_run(void)
19961033Sdfr{
20061033Sdfr	taskqueue_run(taskqueue_swi);
20161033Sdfr}
20261033Sdfr
20361033SdfrTASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
20461033Sdfr		 register_swi(SWI_TQ, taskqueue_swi_run));
205