xref: /freebsd/sys/sys/_eventhandler.h (revision e0bdc8c515efd0a586a41cfce9e5615579f91f1f)
1e2e050c8SConrad Meyer /*-
2e2e050c8SConrad Meyer  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29e2e050c8SConrad Meyer  */
30e2e050c8SConrad Meyer 
31e2e050c8SConrad Meyer #ifndef _SYS__EVENTHANDLER_H_
32e2e050c8SConrad Meyer #define _SYS__EVENTHANDLER_H_
33e2e050c8SConrad Meyer 
34e2e050c8SConrad Meyer #include <sys/queue.h>
35e2e050c8SConrad Meyer 
36e2e050c8SConrad Meyer struct eventhandler_entry {
37e2e050c8SConrad Meyer 	TAILQ_ENTRY(eventhandler_entry)	ee_link;
38e2e050c8SConrad Meyer 	int				ee_priority;
39e2e050c8SConrad Meyer #define	EHE_DEAD_PRIORITY	(-1)
40e2e050c8SConrad Meyer 	void				*ee_arg;
41e2e050c8SConrad Meyer };
42e2e050c8SConrad Meyer 
43e2e050c8SConrad Meyer typedef struct eventhandler_entry	*eventhandler_tag;
44e2e050c8SConrad Meyer 
45e2e050c8SConrad Meyer /*
46e2e050c8SConrad Meyer  * You can optionally use the EVENTHANDLER_LIST and EVENTHANDLER_DIRECT macros
47e2e050c8SConrad Meyer  * to pre-define a symbol for the eventhandler list. This symbol can be used by
48e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE, which has the advantage of not needing to do a
49e2e050c8SConrad Meyer  * locked search of the global list of eventhandler lists. At least
50*e0bdc8c5SGordon Bergling  * EVENTHANDLER_LIST_DEFINE must be used for EVENTHANDLER_DIRECT_INVOKE to
51e2e050c8SConrad Meyer  * work. EVENTHANDLER_LIST_DECLARE is only needed if the call to
52e2e050c8SConrad Meyer  * EVENTHANDLER_DIRECT_INVOKE is in a different compilation unit from
53e2e050c8SConrad Meyer  * EVENTHANDLER_LIST_DEFINE. If the events are even relatively high frequency
54e2e050c8SConrad Meyer  * it is suggested that you directly define a list for them.
55e2e050c8SConrad Meyer  */
56e2e050c8SConrad Meyer struct eventhandler_list;
57e2e050c8SConrad Meyer #define	EVENTHANDLER_LIST_DECLARE(name)					\
58e2e050c8SConrad Meyer extern struct eventhandler_list *_eventhandler_list_ ## name		\
59e2e050c8SConrad Meyer 
60e2e050c8SConrad Meyer /*
61e2e050c8SConrad Meyer  * Event handlers need to be declared, but do not need to be defined. The
62e2e050c8SConrad Meyer  * declaration must be in scope wherever the handler is to be invoked.
63e2e050c8SConrad Meyer  */
64e2e050c8SConrad Meyer #define EVENTHANDLER_DECLARE(name, type)				\
65e2e050c8SConrad Meyer struct eventhandler_entry_ ## name 					\
66e2e050c8SConrad Meyer {									\
67e2e050c8SConrad Meyer 	struct eventhandler_entry	ee;				\
68e2e050c8SConrad Meyer 	type				eh_func;			\
69e2e050c8SConrad Meyer };									\
70e2e050c8SConrad Meyer struct __hack
71e2e050c8SConrad Meyer 
72e2e050c8SConrad Meyer #endif
73