xref: /freebsd/sys/sys/_eventhandler.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1e2e050c8SConrad Meyer /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3e2e050c8SConrad Meyer  *
4e2e050c8SConrad Meyer  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5e2e050c8SConrad Meyer  * All rights reserved.
6e2e050c8SConrad Meyer  *
7e2e050c8SConrad Meyer  * Redistribution and use in source and binary forms, with or without
8e2e050c8SConrad Meyer  * modification, are permitted provided that the following conditions
9e2e050c8SConrad Meyer  * are met:
10e2e050c8SConrad Meyer  * 1. Redistributions of source code must retain the above copyright
11e2e050c8SConrad Meyer  *    notice, this list of conditions and the following disclaimer.
12e2e050c8SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
13e2e050c8SConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
14e2e050c8SConrad Meyer  *    documentation and/or other materials provided with the distribution.
15e2e050c8SConrad Meyer  *
16e2e050c8SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17e2e050c8SConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e2e050c8SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e2e050c8SConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20e2e050c8SConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e2e050c8SConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e2e050c8SConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e2e050c8SConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e2e050c8SConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e2e050c8SConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e2e050c8SConrad Meyer  * SUCH DAMAGE.
27e2e050c8SConrad Meyer  */
28e2e050c8SConrad Meyer 
29e2e050c8SConrad Meyer #ifndef _SYS__EVENTHANDLER_H_
30e2e050c8SConrad Meyer #define _SYS__EVENTHANDLER_H_
31e2e050c8SConrad Meyer 
32e2e050c8SConrad Meyer #include <sys/queue.h>
33e2e050c8SConrad Meyer 
34e2e050c8SConrad Meyer struct eventhandler_entry {
35e2e050c8SConrad Meyer 	TAILQ_ENTRY(eventhandler_entry)	ee_link;
36e2e050c8SConrad Meyer 	int				ee_priority;
37e2e050c8SConrad Meyer #define	EHE_DEAD_PRIORITY	(-1)
38e2e050c8SConrad Meyer 	void				*ee_arg;
39e2e050c8SConrad Meyer };
40e2e050c8SConrad Meyer 
41e2e050c8SConrad Meyer typedef struct eventhandler_entry	*eventhandler_tag;
42e2e050c8SConrad Meyer 
43e2e050c8SConrad Meyer /*
44e2e050c8SConrad Meyer  * You can optionally use the EVENTHANDLER_LIST and EVENTHANDLER_DIRECT macros
45e2e050c8SConrad Meyer  * to pre-define a symbol for the eventhandler list. This symbol can be used by
46e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE, which has the advantage of not needing to do a
47e2e050c8SConrad Meyer  * locked search of the global list of eventhandler lists. At least
48e0bdc8c5SGordon Bergling  * EVENTHANDLER_LIST_DEFINE must be used for EVENTHANDLER_DIRECT_INVOKE to
49e2e050c8SConrad Meyer  * work. EVENTHANDLER_LIST_DECLARE is only needed if the call to
50e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE is in a different compilation unit from
51e2e050c8SConrad Meyer  * EVENTHANDLER_LIST_DEFINE. If the events are even relatively high frequency
52e2e050c8SConrad Meyer  * it is suggested that you directly define a list for them.
53e2e050c8SConrad Meyer  */
54e2e050c8SConrad Meyer struct eventhandler_list;
55e2e050c8SConrad Meyer #define	EVENTHANDLER_LIST_DECLARE(name)					\
56e2e050c8SConrad Meyer extern struct eventhandler_list *_eventhandler_list_ ## name		\
57e2e050c8SConrad Meyer 
58e2e050c8SConrad Meyer /*
59e2e050c8SConrad Meyer  * Event handlers need to be declared, but do not need to be defined. The
60e2e050c8SConrad Meyer  * declaration must be in scope wherever the handler is to be invoked.
61e2e050c8SConrad Meyer  */
62e2e050c8SConrad Meyer #define EVENTHANDLER_DECLARE(name, type)				\
63e2e050c8SConrad Meyer struct eventhandler_entry_ ## name 					\
64e2e050c8SConrad Meyer {									\
65e2e050c8SConrad Meyer 	struct eventhandler_entry	ee;				\
66e2e050c8SConrad Meyer 	type				eh_func;			\
67e2e050c8SConrad Meyer };									\
68e2e050c8SConrad Meyer struct __hack
69e2e050c8SConrad Meyer 
70e2e050c8SConrad Meyer #endif
71