]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/eventhandler.h
This commit was generated by cvs2svn to compensate for changes in r72571,
[FreeBSD/FreeBSD.git] / sys / sys / eventhandler.h
1 /*-
2  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #ifndef SYS_EVENTHANDLER_H
30 #define SYS_EVENTHANDLER_H
31
32 #include <sys/queue.h>
33 #include <sys/lock.h>
34
35 struct eventhandler_entry 
36 {
37     TAILQ_ENTRY(eventhandler_entry)     ee_link;
38     int                                 ee_priority;
39     void                                *ee_arg;
40 };
41
42 struct eventhandler_list 
43 {
44     char                                *el_name;
45     int                                 el_flags;
46 #define EHE_INITTED     (1<<0)
47     struct lock                         el_lock;
48     TAILQ_ENTRY(eventhandler_list)      el_link;
49     TAILQ_HEAD(,eventhandler_entry)     el_entries;
50 };
51
52 typedef struct eventhandler_entry       *eventhandler_tag;
53
54 /* 
55  * Fast handler lists require the eventhandler list be present
56  * at link time.  They don't allow addition of entries to
57  * unknown eventhandler lists, ie. each list must have an 
58  * "owner".
59  *
60  * Fast handler lists must be defined once by the owner 
61  * of the eventhandler list, and the declaration must be in 
62  * scope at any point the list is manipulated.
63  */
64 #define EVENTHANDLER_FAST_DECLARE(name, type)                   \
65 extern struct eventhandler_list Xeventhandler_list_ ## name ;   \
66 struct eventhandler_entry_ ## name                              \
67 {                                                               \
68     struct eventhandler_entry   ee;                             \
69     type                eh_func;                                \
70 };                                                              \
71 struct __hack
72
73 #define EVENTHANDLER_FAST_DEFINE(name, type)                            \
74 struct eventhandler_list Xeventhandler_list_ ## name = { #name };       \
75 struct __hack
76
77 #define EVENTHANDLER_FAST_INVOKE(name, args...)                                 \
78 do {                                                                            \
79     struct eventhandler_list *_el = &Xeventhandler_list_ ## name ;              \
80     struct eventhandler_entry *_ep, *_en;                                       \
81                                                                                 \
82     if (_el->el_flags & EHE_INITTED) {                                          \
83         lockmgr(&_el->el_lock, LK_EXCLUSIVE, NULL, CURPROC);                    \
84         _ep = TAILQ_FIRST(&(_el->el_entries));                                  \
85         while (_ep != NULL) {                                                   \
86             _en = TAILQ_NEXT(_ep, ee_link);                                     \
87             ((struct eventhandler_entry_ ## name *)_ep)->eh_func(_ep->ee_arg ,  \
88                                                                  ## args);      \
89             _ep = _en;                                                          \
90         }                                                                       \
91         lockmgr(&_el->el_lock, LK_RELEASE, NULL, CURPROC);                      \
92     }                                                                           \
93 } while (0)
94
95 #define EVENTHANDLER_FAST_REGISTER(name, func, arg, priority) \
96     eventhandler_register(&Xeventhandler_list_ ## name, #name, func, arg, priority)
97
98 #define EVENTHANDLER_FAST_DEREGISTER(name, tag) \
99     eventhandler_deregister(&Xeventhandler_list_ ## name, tag)
100
101 /*
102  * Slow handlers are entirely dynamic; lists are created
103  * when entries are added to them, and thus have no concept of "owner",
104  *
105  * Slow handlers need to be declared, but do not need to be defined. The
106  * declaration must be in scope wherever the handler is to be invoked.
107  */
108 #define EVENTHANDLER_DECLARE(name, type)        \
109 struct eventhandler_entry_ ## name              \
110 {                                               \
111     struct eventhandler_entry   ee;             \
112     type                eh_func;                \
113 };                                              \
114 struct __hack
115
116 #define EVENTHANDLER_INVOKE(name, args...)                                      \
117 do {                                                                            \
118     struct eventhandler_list *_el;                                              \
119     struct eventhandler_entry *_ep, *_en;                                       \
120                                                                                 \
121     if (((_el = eventhandler_find_list(#name)) != NULL) &&                      \
122         (_el->el_flags & EHE_INITTED)) {                                        \
123         lockmgr(&_el->el_lock, LK_EXCLUSIVE, NULL, CURPROC);                    \
124         _ep = TAILQ_FIRST(&(_el->el_entries));                                  \
125         while (_ep != NULL) {                                                   \
126             _en = TAILQ_NEXT(_ep, ee_link);                                     \
127             ((struct eventhandler_entry_ ## name *)_ep)->eh_func(_ep->ee_arg ,  \
128                                                                  ## args);      \
129             _ep = _en;                                                          \
130         }                                                                       \
131         lockmgr(&_el->el_lock, LK_RELEASE, NULL, CURPROC);                      \
132     }                                                                           \
133 } while (0)
134
135 #define EVENTHANDLER_REGISTER(name, func, arg, priority) \
136     eventhandler_register(NULL, #name, func, arg, priority)
137
138 #define EVENTHANDLER_DEREGISTER(name, tag)              \
139 do {                                                    \
140     struct eventhandler_list *_el;                      \
141                                                         \
142     if ((_el = eventhandler_find_list(#name)) != NULL)  \
143         eventhandler_deregister(_el, tag);              \
144 } while(0)
145         
146
147 extern eventhandler_tag eventhandler_register(struct eventhandler_list *list, 
148                                               char *name,
149                                               void *func, 
150                                               void *arg, 
151                                               int priority);
152 extern void             eventhandler_deregister(struct eventhandler_list *list,
153                                                 eventhandler_tag tag);
154 extern struct eventhandler_list *eventhandler_find_list(char *name);
155
156 /*
157  * Standard system event queues.
158  */
159
160 /* Shutdown events */
161 typedef void (*shutdown_fn) __P((void *, int));
162
163 #define SHUTDOWN_PRI_FIRST      0
164 #define SHUTDOWN_PRI_DEFAULT    10000
165 #define SHUTDOWN_PRI_LAST       20000
166
167 EVENTHANDLER_DECLARE(shutdown_pre_sync, shutdown_fn);   /* before fs sync */
168 EVENTHANDLER_DECLARE(shutdown_post_sync, shutdown_fn);  /* after fs sync */
169 EVENTHANDLER_DECLARE(shutdown_final, shutdown_fn);
170
171 /* Idle process event */
172 typedef void (*idle_eventhandler_t) __P((void *, int));
173
174 #define IDLE_PRI_FIRST          10000
175 #define IDLE_PRI_LAST           20000
176 EVENTHANDLER_FAST_DECLARE(idle_event, idle_eventhandler_t);
177
178
179 #endif /* SYS_EVENTHANDLER_H */