taskqueue.h revision 215011
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 215011 2010-11-08 20:56:31Z mdf $
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>
37124882Srwatson#include <sys/_task.h>
3861086Sdfr
3961033Sdfrstruct taskqueue;
40196295Spjdstruct thread;
4161033Sdfr
4261033Sdfr/*
4361033Sdfr * A notification callback function which is called from
4461086Sdfr * taskqueue_enqueue().  The context argument is given in the call to
4561086Sdfr * taskqueue_create().  This function would normally be used to allow the
4661086Sdfr * queue to arrange to run itself later (e.g., by scheduling a software
4761033Sdfr * interrupt or waking a kernel thread).
4861033Sdfr */
4961033Sdfrtypedef void (*taskqueue_enqueue_fn)(void *context);
5061033Sdfr
5161086Sdfrstruct taskqueue *taskqueue_create(const char *name, int mflags,
5261086Sdfr				    taskqueue_enqueue_fn enqueue,
53154333Sscottl				    void *context);
54154333Sscottlint	taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
55154333Sscottl				const char *name, ...) __printflike(4, 5);
5661086Sdfrint	taskqueue_enqueue(struct taskqueue *queue, struct task *task);
57215011Smdfint	taskqueue_cancel(struct taskqueue *queue, struct task *task,
58215011Smdf	    u_int *pendp);
59136131Simpvoid	taskqueue_drain(struct taskqueue *queue, struct task *task);
6061086Sdfrvoid	taskqueue_free(struct taskqueue *queue);
61213813Smdfvoid	taskqueue_run(struct taskqueue *queue);
62177621Sscottlvoid	taskqueue_block(struct taskqueue *queue);
63177621Sscottlvoid	taskqueue_unblock(struct taskqueue *queue);
64196295Spjdint	taskqueue_member(struct taskqueue *queue, struct thread *td);
6561033Sdfr
6661033Sdfr/*
67133305Sjmg * Functions for dedicated thread taskqueues
68133305Sjmg */
69133305Sjmgvoid	taskqueue_thread_loop(void *arg);
70133305Sjmgvoid	taskqueue_thread_enqueue(void *context);
71133305Sjmg
72133305Sjmg/*
7361033Sdfr * Initialise a task structure.
7461033Sdfr */
7585560Sjhb#define TASK_INIT(task, priority, func, context) do {	\
7685560Sjhb	(task)->ta_pending = 0;				\
7785560Sjhb	(task)->ta_priority = (priority);		\
7885560Sjhb	(task)->ta_func = (func);			\
7985560Sjhb	(task)->ta_context = (context);			\
8085560Sjhb} while (0)
8161033Sdfr
8261033Sdfr/*
8361033Sdfr * Declare a reference to a taskqueue.
8461033Sdfr */
8561033Sdfr#define TASKQUEUE_DECLARE(name)			\
8661033Sdfrextern struct taskqueue *taskqueue_##name
8761033Sdfr
8861033Sdfr/*
89154167Sscottl * Define and initialise a global taskqueue that uses sleep mutexes.
9061033Sdfr */
9161086Sdfr#define TASKQUEUE_DEFINE(name, enqueue, context, init)			\
9261033Sdfr									\
9361033Sdfrstruct taskqueue *taskqueue_##name;					\
9461033Sdfr									\
9561033Sdfrstatic void								\
9661033Sdfrtaskqueue_define_##name(void *arg)					\
9761033Sdfr{									\
9861033Sdfr	taskqueue_##name =						\
99154333Sscottl	    taskqueue_create(#name, M_NOWAIT, (enqueue), (context));	\
10061033Sdfr	init;								\
10161033Sdfr}									\
10261033Sdfr									\
10361033SdfrSYSINIT(taskqueue_##name, SI_SUB_CONFIGURE, SI_ORDER_SECOND,		\
104177253Srwatson	taskqueue_define_##name, NULL);					\
10561086Sdfr									\
10661086Sdfrstruct __hack
107133305Sjmg#define TASKQUEUE_DEFINE_THREAD(name)					\
108133305SjmgTASKQUEUE_DEFINE(name, taskqueue_thread_enqueue, &taskqueue_##name,	\
109154333Sscottl	taskqueue_start_threads(&taskqueue_##name, 1, PWAIT,		\
110154333Sscottl	"%s taskq", #name))
11161033Sdfr
11261033Sdfr/*
113154167Sscottl * Define and initialise a global taskqueue that uses spin mutexes.
114154167Sscottl */
115154167Sscottl#define TASKQUEUE_FAST_DEFINE(name, enqueue, context, init)		\
116154167Sscottl									\
117154167Sscottlstruct taskqueue *taskqueue_##name;					\
118154167Sscottl									\
119154167Sscottlstatic void								\
120154167Sscottltaskqueue_define_##name(void *arg)					\
121154167Sscottl{									\
122154167Sscottl	taskqueue_##name =						\
123154333Sscottl	    taskqueue_create_fast(#name, M_NOWAIT, (enqueue),		\
124154333Sscottl	    (context));							\
125154167Sscottl	init;								\
126154167Sscottl}									\
127154167Sscottl									\
128154167SscottlSYSINIT(taskqueue_##name, SI_SUB_CONFIGURE, SI_ORDER_SECOND,		\
129177253Srwatson	taskqueue_define_##name, NULL);					\
130154167Sscottl									\
131154167Sscottlstruct __hack
132154167Sscottl#define TASKQUEUE_FAST_DEFINE_THREAD(name)				\
133154167SscottlTASKQUEUE_FAST_DEFINE(name, taskqueue_thread_enqueue,			\
134154333Sscottl	&taskqueue_##name, taskqueue_start_threads(&taskqueue_##name	\
135154333Sscottl	1, PWAIT, "%s taskq", #name))
136154167Sscottl
137154167Sscottl/*
138119708Sken * These queues are serviced by software interrupt handlers.  To enqueue
139119708Sken * a task, call taskqueue_enqueue(taskqueue_swi, &task) or
140119708Sken * taskqueue_enqueue(taskqueue_swi_giant, &task).
14161033Sdfr */
142111528SscottlTASKQUEUE_DECLARE(swi_giant);
14361033SdfrTASKQUEUE_DECLARE(swi);
14461033Sdfr
145119708Sken/*
146119708Sken * This queue is serviced by a kernel thread.  To enqueue a task, call
147119708Sken * taskqueue_enqueue(taskqueue_thread, &task).
148119708Sken */
149119708SkenTASKQUEUE_DECLARE(thread);
150119708Sken
151119789Ssam/*
152119789Ssam * Queue for swi handlers dispatched from fast interrupt handlers.
153119789Ssam * These are necessarily different from the above because the queue
154119789Ssam * must be locked with spinlocks since sleep mutex's cannot be used
155119789Ssam * from a fast interrupt handler context.
156119789Ssam */
157119789SsamTASKQUEUE_DECLARE(fast);
158119789Ssamint	taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task);
159154167Sscottlstruct taskqueue *taskqueue_create_fast(const char *name, int mflags,
160154167Sscottl				    taskqueue_enqueue_fn enqueue,
161154333Sscottl				    void *context);
162119789Ssam
16361033Sdfr#endif /* !_SYS_TASKQUEUE_H_ */
164