taskqueue.h revision 111528
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 *
2661086Sdfr * $FreeBSD: head/sys/sys/taskqueue.h 111528 2003-02-26 03:15:42Z scottl $
2761033Sdfr */
2861033Sdfr
2961033Sdfr#ifndef _SYS_TASKQUEUE_H_
3061033Sdfr#define _SYS_TASKQUEUE_H_
3161033Sdfr
3261086Sdfr#ifndef _KERNEL
3361086Sdfr#error "no user-servicable parts inside"
3461086Sdfr#endif
3561033Sdfr
3661086Sdfr#include <sys/queue.h>
3761086Sdfr
3861033Sdfrstruct taskqueue;
3961033Sdfr
4061033Sdfr/*
4161033Sdfr * Each task includes a function which is called from
4261086Sdfr * taskqueue_run().  The first argument is taken from the 'ta_context'
4361033Sdfr * field of struct task and the second argument is a count of how many
4461033Sdfr * times the task was enqueued before the call to taskqueue_run().
4561033Sdfr */
4661086Sdfrtypedef void task_fn_t(void *context, int pending);
4761033Sdfr
4861033Sdfr/*
4961033Sdfr * A notification callback function which is called from
5061086Sdfr * taskqueue_enqueue().  The context argument is given in the call to
5161086Sdfr * taskqueue_create().  This function would normally be used to allow the
5261086Sdfr * queue to arrange to run itself later (e.g., by scheduling a software
5361033Sdfr * interrupt or waking a kernel thread).
5461033Sdfr */
5561033Sdfrtypedef void (*taskqueue_enqueue_fn)(void *context);
5661033Sdfr
5761033Sdfrstruct task {
5861086Sdfr	STAILQ_ENTRY(task) ta_link;	/* link for queue */
5985628Sjhb	int	ta_pending;		/* count times queued */
6085628Sjhb	int	ta_priority;		/* priority of task in queue */
6185628Sjhb	task_fn_t *ta_func;		/* task handler */
6285628Sjhb	void	*ta_context;		/* argument for handler */
6361033Sdfr};
6461033Sdfr
6561086Sdfrstruct taskqueue *taskqueue_create(const char *name, int mflags,
6661086Sdfr				    taskqueue_enqueue_fn enqueue,
6761086Sdfr				    void *context);
6861086Sdfrint	taskqueue_enqueue(struct taskqueue *queue, struct task *task);
6961086Sdfrstruct taskqueue *taskqueue_find(const char *name);
7061086Sdfrvoid	taskqueue_free(struct taskqueue *queue);
7161086Sdfrvoid	taskqueue_run(struct taskqueue *queue);
7261033Sdfr
7361033Sdfr/*
7461033Sdfr * Initialise a task structure.
7561033Sdfr */
7685560Sjhb#define TASK_INIT(task, priority, func, context) do {	\
7785560Sjhb	(task)->ta_pending = 0;				\
7885560Sjhb	(task)->ta_priority = (priority);		\
7985560Sjhb	(task)->ta_func = (func);			\
8085560Sjhb	(task)->ta_context = (context);			\
8185560Sjhb} while (0)
8261033Sdfr
8361033Sdfr/*
8461033Sdfr * Declare a reference to a taskqueue.
8561033Sdfr */
8661033Sdfr#define TASKQUEUE_DECLARE(name)			\
8761033Sdfrextern struct taskqueue *taskqueue_##name
8861033Sdfr
8961033Sdfr/*
9061033Sdfr * Define and initialise a taskqueue.
9161033Sdfr */
9261086Sdfr#define TASKQUEUE_DEFINE(name, enqueue, context, init)			\
9361033Sdfr									\
9461033Sdfrstruct taskqueue *taskqueue_##name;					\
9561033Sdfr									\
9661033Sdfrstatic void								\
9761033Sdfrtaskqueue_define_##name(void *arg)					\
9861033Sdfr{									\
9961033Sdfr	taskqueue_##name =						\
10061086Sdfr	    taskqueue_create(#name, M_NOWAIT, (enqueue), (context));	\
10161033Sdfr	init;								\
10261033Sdfr}									\
10361033Sdfr									\
10461033SdfrSYSINIT(taskqueue_##name, SI_SUB_CONFIGURE, SI_ORDER_SECOND,		\
10561086Sdfr	taskqueue_define_##name, NULL)					\
10661086Sdfr									\
10761086Sdfrstruct __hack
10861033Sdfr
10961033Sdfr/*
11061086Sdfr * This queue is serviced by a software interrupt handler.  To enqueue
11161466Smsmith * a task, call taskqueue_enqueue(taskqueue_swi, &task).
11261033Sdfr */
113111528SscottlTASKQUEUE_DECLARE(swi_giant);
11461033SdfrTASKQUEUE_DECLARE(swi);
11561033Sdfr
11661033Sdfr#endif /* !_SYS_TASKQUEUE_H_ */
117