Deleted Added
full compact
subr_eventhandler.c (112111) subr_eventhandler.c (116182)
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.
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: head/sys/kern/subr_eventhandler.c 112111 2003-03-11 20:17:00Z jhb $
27 */
28
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/subr_eventhandler.c 116182 2003-06-11 00:56:59Z obrien $");
29
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/lock.h>
32#include <sys/malloc.h>
33#include <sys/mutex.h>
34#include <sys/proc.h>
35#include <sys/systm.h>
36#include <sys/eventhandler.h>
37
38static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
39
40/* List of 'slow' lists */
41static TAILQ_HEAD(, eventhandler_list) eventhandler_lists;
42static int eventhandler_lists_initted = 0;
43static struct mtx eventhandler_mutex;
44
45struct eventhandler_entry_generic
46{
47 struct eventhandler_entry ee;
48 void (* func)(void);
49};
50
51static struct eventhandler_list *_eventhandler_find_list(char *name);
52
53/*
54 * Initialize the eventhandler mutex and list.
55 */
56static void
57eventhandler_init(void *dummy __unused)
58{
59 TAILQ_INIT(&eventhandler_lists);
60 mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
61 atomic_store_rel_int(&eventhandler_lists_initted, 1);
62}
63SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
64 NULL)
65
66/*
67 * Insertion is O(n) due to the priority scan, but optimises to O(1)
68 * if all priorities are identical.
69 */
70eventhandler_tag
71eventhandler_register(struct eventhandler_list *list, char *name,
72 void *func, void *arg, int priority)
73{
74 struct eventhandler_list *new_list;
75 struct eventhandler_entry_generic *eg;
76 struct eventhandler_entry *ep;
77
78 KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
79
80 /* lock the eventhandler lists */
81 mtx_lock(&eventhandler_mutex);
82
83 /* Do we need to find/create the (slow) list? */
84 if (list == NULL) {
85 /* look for a matching, existing list */
86 list = _eventhandler_find_list(name);
87
88 /* Do we need to create the list? */
89 if (list == NULL) {
90 mtx_unlock(&eventhandler_mutex);
91
92 new_list = malloc(sizeof(struct eventhandler_list) +
93 strlen(name) + 1, M_EVENTHANDLER, M_WAITOK);
94
95 /* If someone else created it already, then use that one. */
96 mtx_lock(&eventhandler_mutex);
97 list = _eventhandler_find_list(name);
98 if (list != NULL) {
99 free(new_list, M_EVENTHANDLER);
100 } else {
101 CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
102 list = new_list;
103 list->el_flags = 0;
104 list->el_runcount = 0;
105 bzero(&list->el_lock, sizeof(list->el_lock));
106 list->el_name = (char *)list + sizeof(struct eventhandler_list);
107 strcpy(list->el_name, name);
108 TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
109 }
110 }
111 }
112 if (!(list->el_flags & EHL_INITTED)) {
113 TAILQ_INIT(&list->el_entries);
114 mtx_init(&list->el_lock, name, "eventhandler list", MTX_DEF);
115 atomic_store_rel_int(&list->el_flags, EHL_INITTED);
116 }
117 mtx_unlock(&eventhandler_mutex);
118
119 /* allocate an entry for this handler, populate it */
120 eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
121 M_WAITOK | M_ZERO);
122 eg->func = func;
123 eg->ee.ee_arg = arg;
124 eg->ee.ee_priority = priority;
125 KASSERT(priority != EHE_DEAD_PRIORITY,
126 ("%s: handler for %s registered with dead priority", __func__, name));
127
128 /* sort it into the list */
129 CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, eg,
130 func, name);
131 EHL_LOCK(list);
132 TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
133 if (ep->ee_priority != EHE_DEAD_PRIORITY &&
134 eg->ee.ee_priority < ep->ee_priority) {
135 TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
136 break;
137 }
138 }
139 if (ep == NULL)
140 TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
141 EHL_UNLOCK(list);
142 return(&eg->ee);
143}
144
145void
146eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
147{
148 struct eventhandler_entry *ep = tag;
149
150 EHL_LOCK_ASSERT(list, MA_OWNED);
151 if (ep != NULL) {
152 /* remove just this entry */
153 if (list->el_runcount == 0) {
154 CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
155 list->el_name);
156 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
157 free(ep, M_EVENTHANDLER);
158 } else {
159 CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
160 ep, list->el_name);
161 ep->ee_priority = EHE_DEAD_PRIORITY;
162 }
163 } else {
164 /* remove entire list */
165 if (list->el_runcount == 0) {
166 CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
167 list->el_name);
168 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
169 while (!TAILQ_EMPTY(&list->el_entries)) {
170 ep = TAILQ_FIRST(&list->el_entries);
171 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
172 free(ep, M_EVENTHANDLER);
173 }
174 } else {
175 CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
176 __func__, list->el_name);
177 TAILQ_FOREACH(ep, &list->el_entries, ee_link)
178 ep->ee_priority = EHE_DEAD_PRIORITY;
179 }
180 }
181 EHL_UNLOCK(list);
182}
183
184/*
185 * Internal version for use when eventhandler list is already locked.
186 */
187static struct eventhandler_list *
188_eventhandler_find_list(char *name)
189{
190 struct eventhandler_list *list;
191
192 mtx_assert(&eventhandler_mutex, MA_OWNED);
193 TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
194 if (!strcmp(name, list->el_name))
195 break;
196 }
197 return (list);
198}
199
200/*
201 * Lookup a "slow" list by name. Returns with the list locked.
202 */
203struct eventhandler_list *
204eventhandler_find_list(char *name)
205{
206 struct eventhandler_list *list;
207
208 if (!eventhandler_lists_initted)
209 return(NULL);
210
211 /* scan looking for the requested list */
212 mtx_lock(&eventhandler_mutex);
213 list = _eventhandler_find_list(name);
214 if (list != NULL)
215 EHL_LOCK(list);
216 mtx_unlock(&eventhandler_mutex);
217
218 return(list);
219}
220
221/*
222 * Prune "dead" entries from an eventhandler list.
223 */
224void
225eventhandler_prune_list(struct eventhandler_list *list)
226{
227 struct eventhandler_entry *ep, *en;
228
229 CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
230 EHL_LOCK_ASSERT(list, MA_OWNED);
231 ep = TAILQ_FIRST(&list->el_entries);
232 while (ep != NULL) {
233 en = TAILQ_NEXT(ep, ee_link);
234 if (ep->ee_priority == EHE_DEAD_PRIORITY) {
235 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
236 free(ep, M_EVENTHANDLER);
237 }
238 ep = en;
239 }
240}
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/lock.h>
33#include <sys/malloc.h>
34#include <sys/mutex.h>
35#include <sys/proc.h>
36#include <sys/systm.h>
37#include <sys/eventhandler.h>
38
39static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
40
41/* List of 'slow' lists */
42static TAILQ_HEAD(, eventhandler_list) eventhandler_lists;
43static int eventhandler_lists_initted = 0;
44static struct mtx eventhandler_mutex;
45
46struct eventhandler_entry_generic
47{
48 struct eventhandler_entry ee;
49 void (* func)(void);
50};
51
52static struct eventhandler_list *_eventhandler_find_list(char *name);
53
54/*
55 * Initialize the eventhandler mutex and list.
56 */
57static void
58eventhandler_init(void *dummy __unused)
59{
60 TAILQ_INIT(&eventhandler_lists);
61 mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
62 atomic_store_rel_int(&eventhandler_lists_initted, 1);
63}
64SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
65 NULL)
66
67/*
68 * Insertion is O(n) due to the priority scan, but optimises to O(1)
69 * if all priorities are identical.
70 */
71eventhandler_tag
72eventhandler_register(struct eventhandler_list *list, char *name,
73 void *func, void *arg, int priority)
74{
75 struct eventhandler_list *new_list;
76 struct eventhandler_entry_generic *eg;
77 struct eventhandler_entry *ep;
78
79 KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
80
81 /* lock the eventhandler lists */
82 mtx_lock(&eventhandler_mutex);
83
84 /* Do we need to find/create the (slow) list? */
85 if (list == NULL) {
86 /* look for a matching, existing list */
87 list = _eventhandler_find_list(name);
88
89 /* Do we need to create the list? */
90 if (list == NULL) {
91 mtx_unlock(&eventhandler_mutex);
92
93 new_list = malloc(sizeof(struct eventhandler_list) +
94 strlen(name) + 1, M_EVENTHANDLER, M_WAITOK);
95
96 /* If someone else created it already, then use that one. */
97 mtx_lock(&eventhandler_mutex);
98 list = _eventhandler_find_list(name);
99 if (list != NULL) {
100 free(new_list, M_EVENTHANDLER);
101 } else {
102 CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
103 list = new_list;
104 list->el_flags = 0;
105 list->el_runcount = 0;
106 bzero(&list->el_lock, sizeof(list->el_lock));
107 list->el_name = (char *)list + sizeof(struct eventhandler_list);
108 strcpy(list->el_name, name);
109 TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
110 }
111 }
112 }
113 if (!(list->el_flags & EHL_INITTED)) {
114 TAILQ_INIT(&list->el_entries);
115 mtx_init(&list->el_lock, name, "eventhandler list", MTX_DEF);
116 atomic_store_rel_int(&list->el_flags, EHL_INITTED);
117 }
118 mtx_unlock(&eventhandler_mutex);
119
120 /* allocate an entry for this handler, populate it */
121 eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
122 M_WAITOK | M_ZERO);
123 eg->func = func;
124 eg->ee.ee_arg = arg;
125 eg->ee.ee_priority = priority;
126 KASSERT(priority != EHE_DEAD_PRIORITY,
127 ("%s: handler for %s registered with dead priority", __func__, name));
128
129 /* sort it into the list */
130 CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, eg,
131 func, name);
132 EHL_LOCK(list);
133 TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
134 if (ep->ee_priority != EHE_DEAD_PRIORITY &&
135 eg->ee.ee_priority < ep->ee_priority) {
136 TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
137 break;
138 }
139 }
140 if (ep == NULL)
141 TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
142 EHL_UNLOCK(list);
143 return(&eg->ee);
144}
145
146void
147eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
148{
149 struct eventhandler_entry *ep = tag;
150
151 EHL_LOCK_ASSERT(list, MA_OWNED);
152 if (ep != NULL) {
153 /* remove just this entry */
154 if (list->el_runcount == 0) {
155 CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
156 list->el_name);
157 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
158 free(ep, M_EVENTHANDLER);
159 } else {
160 CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
161 ep, list->el_name);
162 ep->ee_priority = EHE_DEAD_PRIORITY;
163 }
164 } else {
165 /* remove entire list */
166 if (list->el_runcount == 0) {
167 CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
168 list->el_name);
169 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
170 while (!TAILQ_EMPTY(&list->el_entries)) {
171 ep = TAILQ_FIRST(&list->el_entries);
172 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
173 free(ep, M_EVENTHANDLER);
174 }
175 } else {
176 CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
177 __func__, list->el_name);
178 TAILQ_FOREACH(ep, &list->el_entries, ee_link)
179 ep->ee_priority = EHE_DEAD_PRIORITY;
180 }
181 }
182 EHL_UNLOCK(list);
183}
184
185/*
186 * Internal version for use when eventhandler list is already locked.
187 */
188static struct eventhandler_list *
189_eventhandler_find_list(char *name)
190{
191 struct eventhandler_list *list;
192
193 mtx_assert(&eventhandler_mutex, MA_OWNED);
194 TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
195 if (!strcmp(name, list->el_name))
196 break;
197 }
198 return (list);
199}
200
201/*
202 * Lookup a "slow" list by name. Returns with the list locked.
203 */
204struct eventhandler_list *
205eventhandler_find_list(char *name)
206{
207 struct eventhandler_list *list;
208
209 if (!eventhandler_lists_initted)
210 return(NULL);
211
212 /* scan looking for the requested list */
213 mtx_lock(&eventhandler_mutex);
214 list = _eventhandler_find_list(name);
215 if (list != NULL)
216 EHL_LOCK(list);
217 mtx_unlock(&eventhandler_mutex);
218
219 return(list);
220}
221
222/*
223 * Prune "dead" entries from an eventhandler list.
224 */
225void
226eventhandler_prune_list(struct eventhandler_list *list)
227{
228 struct eventhandler_entry *ep, *en;
229
230 CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
231 EHL_LOCK_ASSERT(list, MA_OWNED);
232 ep = TAILQ_FIRST(&list->el_entries);
233 while (ep != NULL) {
234 en = TAILQ_NEXT(ep, ee_link);
235 if (ep->ee_priority == EHE_DEAD_PRIORITY) {
236 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
237 free(ep, M_EVENTHANDLER);
238 }
239 ep = en;
240 }
241}