xref: /freebsd/sys/sys/_eventhandler.h (revision e2e050c8ef733138fc6a9e514e4b856fefbc3ff1)
1*e2e050c8SConrad Meyer /*-
2*e2e050c8SConrad Meyer  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*e2e050c8SConrad Meyer  *
4*e2e050c8SConrad Meyer  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5*e2e050c8SConrad Meyer  * All rights reserved.
6*e2e050c8SConrad Meyer  *
7*e2e050c8SConrad Meyer  * Redistribution and use in source and binary forms, with or without
8*e2e050c8SConrad Meyer  * modification, are permitted provided that the following conditions
9*e2e050c8SConrad Meyer  * are met:
10*e2e050c8SConrad Meyer  * 1. Redistributions of source code must retain the above copyright
11*e2e050c8SConrad Meyer  *    notice, this list of conditions and the following disclaimer.
12*e2e050c8SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
13*e2e050c8SConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
14*e2e050c8SConrad Meyer  *    documentation and/or other materials provided with the distribution.
15*e2e050c8SConrad Meyer  *
16*e2e050c8SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*e2e050c8SConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*e2e050c8SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*e2e050c8SConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*e2e050c8SConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*e2e050c8SConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*e2e050c8SConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*e2e050c8SConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*e2e050c8SConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*e2e050c8SConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*e2e050c8SConrad Meyer  * SUCH DAMAGE.
27*e2e050c8SConrad Meyer  *
28*e2e050c8SConrad Meyer  * $FreeBSD$
29*e2e050c8SConrad Meyer  */
30*e2e050c8SConrad Meyer 
31*e2e050c8SConrad Meyer #ifndef _SYS__EVENTHANDLER_H_
32*e2e050c8SConrad Meyer #define _SYS__EVENTHANDLER_H_
33*e2e050c8SConrad Meyer 
34*e2e050c8SConrad Meyer #include <sys/queue.h>
35*e2e050c8SConrad Meyer 
36*e2e050c8SConrad Meyer struct eventhandler_entry {
37*e2e050c8SConrad Meyer 	TAILQ_ENTRY(eventhandler_entry)	ee_link;
38*e2e050c8SConrad Meyer 	int				ee_priority;
39*e2e050c8SConrad Meyer #define	EHE_DEAD_PRIORITY	(-1)
40*e2e050c8SConrad Meyer 	void				*ee_arg;
41*e2e050c8SConrad Meyer };
42*e2e050c8SConrad Meyer 
43*e2e050c8SConrad Meyer typedef struct eventhandler_entry	*eventhandler_tag;
44*e2e050c8SConrad Meyer 
45*e2e050c8SConrad Meyer /*
46*e2e050c8SConrad Meyer  * You can optionally use the EVENTHANDLER_LIST and EVENTHANDLER_DIRECT macros
47*e2e050c8SConrad Meyer  * to pre-define a symbol for the eventhandler list. This symbol can be used by
48*e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE, which has the advantage of not needing to do a
49*e2e050c8SConrad Meyer  * locked search of the global list of eventhandler lists. At least
50*e2e050c8SConrad Meyer  * EVENTHANDLER_LIST_DEFINE must be be used for EVENTHANDLER_DIRECT_INVOKE to
51*e2e050c8SConrad Meyer  * work. EVENTHANDLER_LIST_DECLARE is only needed if the call to
52*e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE is in a different compilation unit from
53*e2e050c8SConrad Meyer  * EVENTHANDLER_LIST_DEFINE. If the events are even relatively high frequency
54*e2e050c8SConrad Meyer  * it is suggested that you directly define a list for them.
55*e2e050c8SConrad Meyer  */
56*e2e050c8SConrad Meyer struct eventhandler_list;
57*e2e050c8SConrad Meyer #define	EVENTHANDLER_LIST_DECLARE(name)					\
58*e2e050c8SConrad Meyer extern struct eventhandler_list *_eventhandler_list_ ## name		\
59*e2e050c8SConrad Meyer 
60*e2e050c8SConrad Meyer /*
61*e2e050c8SConrad Meyer  * Event handlers need to be declared, but do not need to be defined. The
62*e2e050c8SConrad Meyer  * declaration must be in scope wherever the handler is to be invoked.
63*e2e050c8SConrad Meyer  */
64*e2e050c8SConrad Meyer #define EVENTHANDLER_DECLARE(name, type)				\
65*e2e050c8SConrad Meyer struct eventhandler_entry_ ## name 					\
66*e2e050c8SConrad Meyer {									\
67*e2e050c8SConrad Meyer 	struct eventhandler_entry	ee;				\
68*e2e050c8SConrad Meyer 	type				eh_func;			\
69*e2e050c8SConrad Meyer };									\
70*e2e050c8SConrad Meyer struct __hack
71*e2e050c8SConrad Meyer 
72*e2e050c8SConrad Meyer #endif
73*e2e050c8SConrad Meyer /*-
74*e2e050c8SConrad Meyer  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
75*e2e050c8SConrad Meyer  *
76*e2e050c8SConrad Meyer  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
77*e2e050c8SConrad Meyer  * All rights reserved.
78*e2e050c8SConrad Meyer  *
79*e2e050c8SConrad Meyer  * Redistribution and use in source and binary forms, with or without
80*e2e050c8SConrad Meyer  * modification, are permitted provided that the following conditions
81*e2e050c8SConrad Meyer  * are met:
82*e2e050c8SConrad Meyer  * 1. Redistributions of source code must retain the above copyright
83*e2e050c8SConrad Meyer  *    notice, this list of conditions and the following disclaimer.
84*e2e050c8SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
85*e2e050c8SConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
86*e2e050c8SConrad Meyer  *    documentation and/or other materials provided with the distribution.
87*e2e050c8SConrad Meyer  *
88*e2e050c8SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
89*e2e050c8SConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90*e2e050c8SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91*e2e050c8SConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
92*e2e050c8SConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93*e2e050c8SConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94*e2e050c8SConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95*e2e050c8SConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96*e2e050c8SConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97*e2e050c8SConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98*e2e050c8SConrad Meyer  * SUCH DAMAGE.
99*e2e050c8SConrad Meyer  *
100*e2e050c8SConrad Meyer  * $FreeBSD$
101*e2e050c8SConrad Meyer  */
102*e2e050c8SConrad Meyer 
103*e2e050c8SConrad Meyer #ifndef _SYS__EVENTHANDLER_H_
104*e2e050c8SConrad Meyer #define _SYS__EVENTHANDLER_H_
105*e2e050c8SConrad Meyer 
106*e2e050c8SConrad Meyer #include <sys/queue.h>
107*e2e050c8SConrad Meyer 
108*e2e050c8SConrad Meyer struct eventhandler_entry {
109*e2e050c8SConrad Meyer 	TAILQ_ENTRY(eventhandler_entry)	ee_link;
110*e2e050c8SConrad Meyer 	int				ee_priority;
111*e2e050c8SConrad Meyer #define	EHE_DEAD_PRIORITY	(-1)
112*e2e050c8SConrad Meyer 	void				*ee_arg;
113*e2e050c8SConrad Meyer };
114*e2e050c8SConrad Meyer 
115*e2e050c8SConrad Meyer typedef struct eventhandler_entry	*eventhandler_tag;
116*e2e050c8SConrad Meyer 
117*e2e050c8SConrad Meyer /*
118*e2e050c8SConrad Meyer  * You can optionally use the EVENTHANDLER_LIST and EVENTHANDLER_DIRECT macros
119*e2e050c8SConrad Meyer  * to pre-define a symbol for the eventhandler list. This symbol can be used by
120*e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE, which has the advantage of not needing to do a
121*e2e050c8SConrad Meyer  * locked search of the global list of eventhandler lists. At least
122*e2e050c8SConrad Meyer  * EVENTHANDLER_LIST_DEFINE must be be used for EVENTHANDLER_DIRECT_INVOKE to
123*e2e050c8SConrad Meyer  * work. EVENTHANDLER_LIST_DECLARE is only needed if the call to
124*e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE is in a different compilation unit from
125*e2e050c8SConrad Meyer  * EVENTHANDLER_LIST_DEFINE. If the events are even relatively high frequency
126*e2e050c8SConrad Meyer  * it is suggested that you directly define a list for them.
127*e2e050c8SConrad Meyer  */
128*e2e050c8SConrad Meyer struct eventhandler_list;
129*e2e050c8SConrad Meyer #define	EVENTHANDLER_LIST_DECLARE(name)					\
130*e2e050c8SConrad Meyer extern struct eventhandler_list *_eventhandler_list_ ## name		\
131*e2e050c8SConrad Meyer 
132*e2e050c8SConrad Meyer /*
133*e2e050c8SConrad Meyer  * Event handlers need to be declared, but do not need to be defined. The
134*e2e050c8SConrad Meyer  * declaration must be in scope wherever the handler is to be invoked.
135*e2e050c8SConrad Meyer  */
136*e2e050c8SConrad Meyer #define EVENTHANDLER_DECLARE(name, type)				\
137*e2e050c8SConrad Meyer struct eventhandler_entry_ ## name 					\
138*e2e050c8SConrad Meyer {									\
139*e2e050c8SConrad Meyer 	struct eventhandler_entry	ee;				\
140*e2e050c8SConrad Meyer 	type				eh_func;			\
141*e2e050c8SConrad Meyer };									\
142*e2e050c8SConrad Meyer struct __hack
143*e2e050c8SConrad Meyer 
144*e2e050c8SConrad Meyer #endif
145