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: stable/10/sys/sys/taskqueue.h 337042 2018-08-01 13:13:43Z hselasky $
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>
38221059Skib#include <sys/_callout.h>
3961086Sdfr
4061033Sdfrstruct taskqueue;
41196295Spjdstruct thread;
4261033Sdfr
43221059Skibstruct timeout_task {
44221059Skib	struct taskqueue *q;
45221059Skib	struct task t;
46221059Skib	struct callout c;
47221059Skib	int    f;
48221059Skib};
49221059Skib
50248649Swillenum taskqueue_callback_type {
51248649Swill	TASKQUEUE_CALLBACK_TYPE_INIT,
52248649Swill	TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
53248649Swill};
54248649Swill#define	TASKQUEUE_CALLBACK_TYPE_MIN	TASKQUEUE_CALLBACK_TYPE_INIT
55248649Swill#define	TASKQUEUE_CALLBACK_TYPE_MAX	TASKQUEUE_CALLBACK_TYPE_SHUTDOWN
56248649Swill#define	TASKQUEUE_NUM_CALLBACKS		TASKQUEUE_CALLBACK_TYPE_MAX + 1
57248649Swill
58248649Swilltypedef void (*taskqueue_callback_fn)(void *context);
59248649Swill
6061033Sdfr/*
6161033Sdfr * A notification callback function which is called from
6261086Sdfr * taskqueue_enqueue().  The context argument is given in the call to
6361086Sdfr * taskqueue_create().  This function would normally be used to allow the
6461086Sdfr * queue to arrange to run itself later (e.g., by scheduling a software
6561033Sdfr * interrupt or waking a kernel thread).
6661033Sdfr */
6761033Sdfrtypedef void (*taskqueue_enqueue_fn)(void *context);
6861033Sdfr
6961086Sdfrstruct taskqueue *taskqueue_create(const char *name, int mflags,
7061086Sdfr				    taskqueue_enqueue_fn enqueue,
71154333Sscottl				    void *context);
72154333Sscottlint	taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
73154333Sscottl				const char *name, ...) __printflike(4, 5);
7461086Sdfrint	taskqueue_enqueue(struct taskqueue *queue, struct task *task);
75221059Skibint	taskqueue_enqueue_timeout(struct taskqueue *queue,
76221059Skib	    struct timeout_task *timeout_task, int ticks);
77315268Shselaskyint	taskqueue_poll_is_busy(struct taskqueue *queue, struct task *task);
78215011Smdfint	taskqueue_cancel(struct taskqueue *queue, struct task *task,
79215011Smdf	    u_int *pendp);
80221059Skibint	taskqueue_cancel_timeout(struct taskqueue *queue,
81221059Skib	    struct timeout_task *timeout_task, u_int *pendp);
82136131Simpvoid	taskqueue_drain(struct taskqueue *queue, struct task *task);
83221059Skibvoid	taskqueue_drain_timeout(struct taskqueue *queue,
84221059Skib	    struct timeout_task *timeout_task);
85262065Savgvoid	taskqueue_drain_all(struct taskqueue *queue);
8661086Sdfrvoid	taskqueue_free(struct taskqueue *queue);
87213813Smdfvoid	taskqueue_run(struct taskqueue *queue);
88177621Sscottlvoid	taskqueue_block(struct taskqueue *queue);
89177621Sscottlvoid	taskqueue_unblock(struct taskqueue *queue);
90196295Spjdint	taskqueue_member(struct taskqueue *queue, struct thread *td);
91248649Swillvoid	taskqueue_set_callback(struct taskqueue *queue,
92248649Swill	    enum taskqueue_callback_type cb_type,
93248649Swill	    taskqueue_callback_fn callback, void *context);
9461033Sdfr
95228715Sjhb#define TASK_INITIALIZER(priority, func, context)	\
96228715Sjhb	{ .ta_pending = 0,				\
97228715Sjhb	  .ta_priority = (priority),			\
98228715Sjhb	  .ta_func = (func),				\
99228715Sjhb	  .ta_context = (context) }
100228715Sjhb
10161033Sdfr/*
102133305Sjmg * Functions for dedicated thread taskqueues
103133305Sjmg */
104133305Sjmgvoid	taskqueue_thread_loop(void *arg);
105133305Sjmgvoid	taskqueue_thread_enqueue(void *context);
106133305Sjmg
107133305Sjmg/*
10861033Sdfr * Initialise a task structure.
10961033Sdfr */
11085560Sjhb#define TASK_INIT(task, priority, func, context) do {	\
11185560Sjhb	(task)->ta_pending = 0;				\
11285560Sjhb	(task)->ta_priority = (priority);		\
11385560Sjhb	(task)->ta_func = (func);			\
11485560Sjhb	(task)->ta_context = (context);			\
11585560Sjhb} while (0)
11661033Sdfr
117221059Skibvoid _timeout_task_init(struct taskqueue *queue,
118221059Skib	    struct timeout_task *timeout_task, int priority, task_fn_t func,
119221059Skib	    void *context);
120221059Skib#define	TIMEOUT_TASK_INIT(queue, timeout_task, priority, func, context) \
121221059Skib	_timeout_task_init(queue, timeout_task, priority, func, context);
122221059Skib
12361033Sdfr/*
12461033Sdfr * Declare a reference to a taskqueue.
12561033Sdfr */
12661033Sdfr#define TASKQUEUE_DECLARE(name)			\
12761033Sdfrextern struct taskqueue *taskqueue_##name
12861033Sdfr
12961033Sdfr/*
130154167Sscottl * Define and initialise a global taskqueue that uses sleep mutexes.
13161033Sdfr */
13261086Sdfr#define TASKQUEUE_DEFINE(name, enqueue, context, init)			\
13361033Sdfr									\
13461033Sdfrstruct taskqueue *taskqueue_##name;					\
13561033Sdfr									\
13661033Sdfrstatic void								\
13761033Sdfrtaskqueue_define_##name(void *arg)					\
13861033Sdfr{									\
13961033Sdfr	taskqueue_##name =						\
140218270Sjhb	    taskqueue_create(#name, M_WAITOK, (enqueue), (context));	\
14161033Sdfr	init;								\
14261033Sdfr}									\
14361033Sdfr									\
144337042ShselaskySYSINIT(taskqueue_##name, SI_SUB_TASKQ, SI_ORDER_SECOND,		\
145177253Srwatson	taskqueue_define_##name, NULL);					\
14661086Sdfr									\
14761086Sdfrstruct __hack
148133305Sjmg#define TASKQUEUE_DEFINE_THREAD(name)					\
149133305SjmgTASKQUEUE_DEFINE(name, taskqueue_thread_enqueue, &taskqueue_##name,	\
150154333Sscottl	taskqueue_start_threads(&taskqueue_##name, 1, PWAIT,		\
151154333Sscottl	"%s taskq", #name))
15261033Sdfr
15361033Sdfr/*
154154167Sscottl * Define and initialise a global taskqueue that uses spin mutexes.
155154167Sscottl */
156154167Sscottl#define TASKQUEUE_FAST_DEFINE(name, enqueue, context, init)		\
157154167Sscottl									\
158154167Sscottlstruct taskqueue *taskqueue_##name;					\
159154167Sscottl									\
160154167Sscottlstatic void								\
161154167Sscottltaskqueue_define_##name(void *arg)					\
162154167Sscottl{									\
163154167Sscottl	taskqueue_##name =						\
164218270Sjhb	    taskqueue_create_fast(#name, M_WAITOK, (enqueue),		\
165154333Sscottl	    (context));							\
166154167Sscottl	init;								\
167154167Sscottl}									\
168154167Sscottl									\
169337042ShselaskySYSINIT(taskqueue_##name, SI_SUB_TASKQ, SI_ORDER_SECOND,		\
170177253Srwatson	taskqueue_define_##name, NULL);					\
171154167Sscottl									\
172154167Sscottlstruct __hack
173154167Sscottl#define TASKQUEUE_FAST_DEFINE_THREAD(name)				\
174154167SscottlTASKQUEUE_FAST_DEFINE(name, taskqueue_thread_enqueue,			\
175154333Sscottl	&taskqueue_##name, taskqueue_start_threads(&taskqueue_##name	\
176154333Sscottl	1, PWAIT, "%s taskq", #name))
177154167Sscottl
178154167Sscottl/*
179119708Sken * These queues are serviced by software interrupt handlers.  To enqueue
180119708Sken * a task, call taskqueue_enqueue(taskqueue_swi, &task) or
181119708Sken * taskqueue_enqueue(taskqueue_swi_giant, &task).
18261033Sdfr */
183111528SscottlTASKQUEUE_DECLARE(swi_giant);
18461033SdfrTASKQUEUE_DECLARE(swi);
18561033Sdfr
186119708Sken/*
187119708Sken * This queue is serviced by a kernel thread.  To enqueue a task, call
188119708Sken * taskqueue_enqueue(taskqueue_thread, &task).
189119708Sken */
190119708SkenTASKQUEUE_DECLARE(thread);
191119708Sken
192119789Ssam/*
193119789Ssam * Queue for swi handlers dispatched from fast interrupt handlers.
194119789Ssam * These are necessarily different from the above because the queue
195119789Ssam * must be locked with spinlocks since sleep mutex's cannot be used
196119789Ssam * from a fast interrupt handler context.
197119789Ssam */
198119789SsamTASKQUEUE_DECLARE(fast);
199119789Ssamint	taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task);
200154167Sscottlstruct taskqueue *taskqueue_create_fast(const char *name, int mflags,
201154167Sscottl				    taskqueue_enqueue_fn enqueue,
202154333Sscottl				    void *context);
203119789Ssam
20461033Sdfr#endif /* !_SYS_TASKQUEUE_H_ */
205