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: releng/5.2/sys/sys/eventhandler.h 112565 2003-03-24 21:24:37Z jhb $
27 */
28
29 #ifndef SYS_EVENTHANDLER_H
30 #define SYS_EVENTHANDLER_H
31
32 #include <sys/lock.h>
33 #include <sys/ktr.h>
34 #include <sys/mutex.h>
35 #include <sys/queue.h>
36
37 struct eventhandler_entry {
38 TAILQ_ENTRY(eventhandler_entry) ee_link;
39 int ee_priority;
40 #define EHE_DEAD_PRIORITY (-1)
41 void *ee_arg;
42 };
43
44 struct eventhandler_list {
45 char *el_name;
46 int el_flags;
47 #define EHL_INITTED (1<<0)
48 u_int el_runcount;
49 struct mtx el_lock;
50 TAILQ_ENTRY(eventhandler_list) el_link;
51 TAILQ_HEAD(,eventhandler_entry) el_entries;
52 };
53
54 typedef struct eventhandler_entry *eventhandler_tag;
55
56 #define EHL_LOCK(p) mtx_lock(&(p)->el_lock)
57 #define EHL_UNLOCK(p) mtx_unlock(&(p)->el_lock)
58 #define EHL_LOCK_ASSERT(p, x) mtx_assert(&(p)->el_lock, x)
59
60 /*
61 * Macro to invoke the handlers for a given event.
62 */
63 #define _EVENTHANDLER_INVOKE(name, list, ...) do { \
64 struct eventhandler_entry *_ep; \
65 struct eventhandler_entry_ ## name *_t; \
66 \
67 KASSERT((list)->el_flags & EHL_INITTED, \
68 ("eventhandler_invoke: running non-inited list")); \
69 EHL_LOCK_ASSERT((list), MA_OWNED); \
70 (list)->el_runcount++; \
71 KASSERT((list)->el_runcount > 0, \
72 ("eventhandler_invoke: runcount overflow")); \
73 CTR0(KTR_EVH, "eventhandler_invoke(\"" __STRING(name) "\")"); \
74 TAILQ_FOREACH(_ep, &((list)->el_entries), ee_link) { \
75 if (_ep->ee_priority != EHE_DEAD_PRIORITY) { \
76 EHL_UNLOCK((list)); \
77 _t = (struct eventhandler_entry_ ## name *)_ep; \
78 CTR1(KTR_EVH, "eventhandler_invoke: executing %p", \
79 (void *)_t->eh_func); \
80 _t->eh_func(_ep->ee_arg , ## __VA_ARGS__); \
81 EHL_LOCK((list)); \
82 } \
83 } \
84 KASSERT((list)->el_runcount > 0, \
85 ("eventhandler_invoke: runcount underflow")); \
86 (list)->el_runcount--; \
87 if ((list)->el_runcount == 0) \
88 eventhandler_prune_list(list); \
89 EHL_UNLOCK((list)); \
90 } while (0)
91
92
93 /*
94 * Fast handler lists require the eventhandler list be present
95 * at link time. They don't allow addition of entries to
96 * unknown eventhandler lists, ie. each list must have an
97 * "owner".
98 *
99 * Fast handler lists must be defined once by the owner
100 * of the eventhandler list, and the declaration must be in
101 * scope at any point the list is manipulated.
102 */
103 #define EVENTHANDLER_FAST_DECLARE(name, type) \
104 extern struct eventhandler_list Xeventhandler_list_ ## name ; \
105 struct eventhandler_entry_ ## name { \
106 struct eventhandler_entry ee; \
107 type eh_func; \
108 }; \
109 struct __hack
110
111 #define EVENTHANDLER_FAST_DEFINE(name, type) \
112 struct eventhandler_list Xeventhandler_list_ ## name = { #name }; \
113 struct __hack
114
115 #define EVENTHANDLER_FAST_INVOKE(name, ...) do { \
116 struct eventhandler_list *_el = &Xeventhandler_list_ ## name ; \
117 \
118 if (_el->el_flags & EHL_INITTED) { \
119 EHL_LOCK(_el); \
120 _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \
121 } \
122 } while (0)
123
124 #define EVENTHANDLER_FAST_REGISTER(name, func, arg, priority) \
125 eventhandler_register(&Xeventhandler_list_ ## name, \
126 #name, func, arg, priority)
127
128 #define EVENTHANDLER_FAST_DEREGISTER(name, tag) do { \
129 struct eventhandler_list *_el = &Xeventhandler_list_ ## name ; \
130 \
131 KASSERT(_el->el_flags & EHL_INITTED, \
132 ("eventhandler_fast_deregister on un-inited list %s", ## name)); \
133 EHL_LOCK(_el); \
134 eventhandler_deregister(_el, tag); \
135 } while (0)
136
137 /*
138 * Slow handlers are entirely dynamic; lists are created
139 * when entries are added to them, and thus have no concept of "owner",
140 *
141 * Slow handlers need to be declared, but do not need to be defined. The
142 * declaration must be in scope wherever the handler is to be invoked.
143 */
144 #define EVENTHANDLER_DECLARE(name, type) \
145 struct eventhandler_entry_ ## name \
146 { \
147 struct eventhandler_entry ee; \
148 type eh_func; \
149 }; \
150 struct __hack
151
152 #define EVENTHANDLER_INVOKE(name, ...) \
153 do { \
154 struct eventhandler_list *_el; \
155 \
156 if ((_el = eventhandler_find_list(#name)) != NULL) \
157 _EVENTHANDLER_INVOKE(name, _el , ## __VA_ARGS__); \
158 } while (0)
159
160 #define EVENTHANDLER_REGISTER(name, func, arg, priority) \
161 eventhandler_register(NULL, #name, func, arg, priority)
162
163 #define EVENTHANDLER_DEREGISTER(name, tag) \
164 do { \
165 struct eventhandler_list *_el; \
166 \
167 if ((_el = eventhandler_find_list(#name)) != NULL) \
168 eventhandler_deregister(_el, tag); \
169 } while(0)
170
171
172 eventhandler_tag eventhandler_register(struct eventhandler_list *list,
173 char *name, void *func, void *arg, int priority);
174 void eventhandler_deregister(struct eventhandler_list *list,
175 eventhandler_tag tag);
176 struct eventhandler_list *eventhandler_find_list(char *name);
177 void eventhandler_prune_list(struct eventhandler_list *list);
178
179 /*
180 * Standard system event queues.
181 */
182
183 /* Generic priority levels */
184 #define EVENTHANDLER_PRI_FIRST 0
185 #define EVENTHANDLER_PRI_ANY 10000
186 #define EVENTHANDLER_PRI_LAST 20000
187
188 /* Shutdown events */
189 typedef void (*shutdown_fn)(void *, int);
190
191 #define SHUTDOWN_PRI_FIRST EVENTHANDLER_PRI_FIRST
192 #define SHUTDOWN_PRI_DEFAULT EVENTHANDLER_PRI_ANY
193 #define SHUTDOWN_PRI_LAST EVENTHANDLER_PRI_LAST
194
195 EVENTHANDLER_DECLARE(shutdown_pre_sync, shutdown_fn); /* before fs sync */
196 EVENTHANDLER_DECLARE(shutdown_post_sync, shutdown_fn); /* after fs sync */
197 EVENTHANDLER_DECLARE(shutdown_final, shutdown_fn);
198
199 /* Idle process event */
200 typedef void (*idle_eventhandler_t)(void *, int);
201
202 #define IDLE_PRI_FIRST EVENTHANDLER_PRI_FIRST
203 #define IDLE_PRI_LAST EVENTHANDLER_PRI_LAST
204 EVENTHANDLER_FAST_DECLARE(idle_event, idle_eventhandler_t);
205
206 /* Low memory event */
207 typedef void (*vm_lowmem_handler_t)(void *, int);
208 #define LOWMEM_PRI_DEFAULT EVENTHANDLER_PRI_FIRST
209 EVENTHANDLER_DECLARE(vm_lowmem, vm_lowmem_handler_t);
210
211 /* Process events */
212 struct proc;
213
214 typedef void (*exitlist_fn)(void *, struct proc *);
215 typedef void (*forklist_fn)(void *, struct proc *, struct proc *, int);
216 typedef void (*execlist_fn)(void *, struct proc *);
217
218 EVENTHANDLER_DECLARE(process_exit, exitlist_fn);
219 EVENTHANDLER_DECLARE(process_fork, forklist_fn);
220 EVENTHANDLER_DECLARE(process_exec, execlist_fn);
221
222 #endif /* SYS_EVENTHANDLER_H */
Cache object: c64ee74610ec020c420f8f5e3960754d
|