xref: /haiku/src/libs/compat/freebsd_iflib/compat/sys/gtaskqueue.h (revision 97f11716bfaa0f385eb0e28a52bf56a5023b9e99)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 Jeffrey Roberson <jeff@freebsd.org>
5  * Copyright (c) 2016 Matthew Macy <mmacy@mattmacy.io>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_GTASKQUEUE_H_
33 #define _SYS_GTASKQUEUE_H_
34 
35 #ifndef _KERNEL
36 #error "no user-serviceable parts inside"
37 #endif
38 
39 #include <sys/_task.h>
40 #include <sys/bus.h>
41 #include <sys/taskqueue.h>
42 #include <sys/types.h>
43 
44 struct gtaskqueue;
45 struct taskqgroup;
46 
47 /*
48  * Taskqueue groups.  Manages dynamic thread groups and irq binding for
49  * device and other tasks.
50  */
51 
52 struct grouptask {
53 	struct gtask		gt_task;
54 	void			*gt_taskqueue;
55 	LIST_ENTRY(grouptask)	gt_list;
56 	void			*gt_uniq;
57 #define	GROUPTASK_NAMELEN	32
58 	char			gt_name[GROUPTASK_NAMELEN];
59 	device_t		gt_dev;
60 	struct resource		*gt_irq;
61 	int			gt_cpu;
62 };
63 
64 void	gtaskqueue_block(struct gtaskqueue *queue);
65 void	gtaskqueue_unblock(struct gtaskqueue *queue);
66 
67 int	gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask);
68 void	gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *task);
69 void	gtaskqueue_drain_all(struct gtaskqueue *queue);
70 
71 void	grouptask_block(struct grouptask *grouptask);
72 void	grouptask_unblock(struct grouptask *grouptask);
73 int	grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *task);
74 
75 void	taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *grptask,
76 	    void *uniq, device_t dev, struct resource *irq, const char *name);
77 int	taskqgroup_attach_cpu(struct taskqgroup *qgroup,
78 	    struct grouptask *grptask, void *uniq, int cpu, device_t dev,
79 	    struct resource *irq, const char *name);
80 void	taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask);
81 struct taskqgroup *taskqgroup_create(const char *name);
82 void	taskqgroup_destroy(struct taskqgroup *qgroup);
83 int	taskqgroup_adjust(struct taskqgroup *qgroup, int cnt, int stride);
84 void	taskqgroup_config_gtask_init(void *ctx, struct grouptask *gtask,
85 	    gtask_fn_t *fn, const char *name);
86 void	taskqgroup_config_gtask_deinit(struct grouptask *gtask);
87 
88 #define	GTASK_INIT(gtask, flags, priority, func, context) do {	\
89 	(gtask)->ta_flags = flags;				\
90 	(gtask)->ta_priority = (priority);			\
91 	(gtask)->ta_func = (func);				\
92 	(gtask)->ta_context = (context);			\
93 } while (0)
94 
95 #define	GROUPTASK_INIT(gtask, priority, func, context)	\
96 	GTASK_INIT(&(gtask)->gt_task, TASK_SKIP_WAKEUP, priority, func, context)
97 #define NET_GROUPTASK_INIT GROUPTASK_INIT
98 
99 #define	GROUPTASK_ENQUEUE(gtask)			\
100 	grouptaskqueue_enqueue((gtask)->gt_taskqueue, &(gtask)->gt_task)
101 
102 #define TASKQGROUP_DECLARE(name)			\
103 extern struct taskqgroup *qgroup_##name
104 
105 #define TASKQGROUP_DEFINE(name, cnt, stride)				\
106 									\
107 struct taskqgroup *qgroup_##name;					\
108 									\
109 static void								\
110 taskqgroup_define_##name(void *arg)					\
111 {									\
112 	qgroup_##name = taskqgroup_create(#name);			\
113 }									\
114 									\
115 SYSINIT(taskqgroup_##name, SI_SUB_TASKQ, SI_ORDER_FIRST,		\
116 	taskqgroup_define_##name, NULL);				\
117 									\
118 static void								\
119 taskqgroup_adjust_##name(void *arg)					\
120 {									\
121 	taskqgroup_adjust(qgroup_##name, (cnt), (stride));		\
122 }									\
123 									\
124 SYSINIT(taskqgroup_adj_##name, SI_SUB_SMP, SI_ORDER_ANY,		\
125 	taskqgroup_adjust_##name, NULL)
126 
127 TASKQGROUP_DECLARE(net);
128 TASKQGROUP_DECLARE(softirq);
129 
130 #endif /* !_SYS_GTASKQUEUE_H_ */
131