xref: /freebsd/sys/kern/subr_eventhandler.c (revision 237fdd78)
1fcb893a8SMike Smith /*-
2fcb893a8SMike Smith  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3fcb893a8SMike Smith  * All rights reserved.
4fcb893a8SMike Smith  *
5fcb893a8SMike Smith  * Redistribution and use in source and binary forms, with or without
6fcb893a8SMike Smith  * modification, are permitted provided that the following conditions
7fcb893a8SMike Smith  * are met:
8fcb893a8SMike Smith  * 1. Redistributions of source code must retain the above copyright
9fcb893a8SMike Smith  *    notice, this list of conditions and the following disclaimer.
10fcb893a8SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
11fcb893a8SMike Smith  *    notice, this list of conditions and the following disclaimer in the
12fcb893a8SMike Smith  *    documentation and/or other materials provided with the distribution.
13fcb893a8SMike Smith  *
14fcb893a8SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fcb893a8SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fcb893a8SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fcb893a8SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fcb893a8SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fcb893a8SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fcb893a8SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fcb893a8SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fcb893a8SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fcb893a8SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fcb893a8SMike Smith  * SUCH DAMAGE.
25fcb893a8SMike Smith  */
26fcb893a8SMike Smith 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
30fcb893a8SMike Smith #include <sys/param.h>
31fcb893a8SMike Smith #include <sys/kernel.h>
32f34fa851SJohn Baldwin #include <sys/lock.h>
33fcb893a8SMike Smith #include <sys/malloc.h>
3406592dd1SJohn Baldwin #include <sys/mutex.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36fcb893a8SMike Smith #include <sys/systm.h>
37fcb893a8SMike Smith #include <sys/eventhandler.h>
38fcb893a8SMike Smith 
39959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
40fcb893a8SMike Smith 
41fcb893a8SMike Smith /* List of 'slow' lists */
42e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
43fcb893a8SMike Smith static int				eventhandler_lists_initted = 0;
44d543796fSJohn Baldwin static struct mtx			eventhandler_mutex;
45fcb893a8SMike Smith 
46fcb893a8SMike Smith struct eventhandler_entry_generic
47fcb893a8SMike Smith {
48fcb893a8SMike Smith     struct eventhandler_entry	ee;
49fcb893a8SMike Smith     void			(* func)(void);
50fcb893a8SMike Smith };
51fcb893a8SMike Smith 
52fdf20233SJoseph Koshy static struct eventhandler_list *_eventhandler_find_list(const char *name);
53ecdf4409SJohn Baldwin 
54fcb893a8SMike Smith /*
5506592dd1SJohn Baldwin  * Initialize the eventhandler mutex and list.
5606592dd1SJohn Baldwin  */
5706592dd1SJohn Baldwin static void
5806592dd1SJohn Baldwin eventhandler_init(void *dummy __unused)
5906592dd1SJohn Baldwin {
6006592dd1SJohn Baldwin     TAILQ_INIT(&eventhandler_lists);
61ecdf4409SJohn Baldwin     mtx_init(&eventhandler_mutex, "eventhandler", NULL, MTX_DEF);
62ecdf4409SJohn Baldwin     atomic_store_rel_int(&eventhandler_lists_initted, 1);
6306592dd1SJohn Baldwin }
6406592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
65237fdd78SRobert Watson     NULL);
6606592dd1SJohn Baldwin 
6706592dd1SJohn Baldwin /*
68fcb893a8SMike Smith  * Insertion is O(n) due to the priority scan, but optimises to O(1)
69fcb893a8SMike Smith  * if all priorities are identical.
70fcb893a8SMike Smith  */
71fcb893a8SMike Smith eventhandler_tag
72fdf20233SJoseph Koshy eventhandler_register(struct eventhandler_list *list, const char *name,
73fcb893a8SMike Smith 		      void *func, void *arg, int priority)
74fcb893a8SMike Smith {
75ecdf4409SJohn Baldwin     struct eventhandler_list		*new_list;
76fcb893a8SMike Smith     struct eventhandler_entry_generic	*eg;
77fcb893a8SMike Smith     struct eventhandler_entry		*ep;
78fcb893a8SMike Smith 
7906592dd1SJohn Baldwin     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
80fcb893a8SMike Smith 
816595c331SMike Smith     /* lock the eventhandler lists */
829ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
836595c331SMike Smith 
84fcb893a8SMike Smith     /* Do we need to find/create the (slow) list? */
85fcb893a8SMike Smith     if (list == NULL) {
86fcb893a8SMike Smith 	/* look for a matching, existing list */
87ecdf4409SJohn Baldwin 	list = _eventhandler_find_list(name);
88fcb893a8SMike Smith 
89fcb893a8SMike Smith 	/* Do we need to create the list? */
90fcb893a8SMike Smith 	if (list == NULL) {
919ed346baSBosko Milekic 	    mtx_unlock(&eventhandler_mutex);
92ecdf4409SJohn Baldwin 
93ecdf4409SJohn Baldwin 	    new_list = malloc(sizeof(struct eventhandler_list) +
94ecdf4409SJohn Baldwin 		strlen(name) + 1, M_EVENTHANDLER, M_WAITOK);
95ecdf4409SJohn Baldwin 
96ecdf4409SJohn Baldwin 	    /* If someone else created it already, then use that one. */
97ecdf4409SJohn Baldwin 	    mtx_lock(&eventhandler_mutex);
98ecdf4409SJohn Baldwin 	    list = _eventhandler_find_list(name);
99ecdf4409SJohn Baldwin 	    if (list != NULL) {
100ecdf4409SJohn Baldwin 		free(new_list, M_EVENTHANDLER);
101ecdf4409SJohn Baldwin 	    } else {
102ecdf4409SJohn Baldwin 		CTR2(KTR_EVH, "%s: creating list \"%s\"", __func__, name);
103ecdf4409SJohn Baldwin 		list = new_list;
104fcb893a8SMike Smith 		list->el_flags = 0;
105ecdf4409SJohn Baldwin 		list->el_runcount = 0;
10628fe1a71SAlfred Perlstein 		bzero(&list->el_lock, sizeof(list->el_lock));
107fcb893a8SMike Smith 		list->el_name = (char *)list + sizeof(struct eventhandler_list);
108fcb893a8SMike Smith 		strcpy(list->el_name, name);
109fcb893a8SMike Smith 		TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
110fcb893a8SMike Smith 	    }
111fcb893a8SMike Smith 	}
112ecdf4409SJohn Baldwin     }
113ecdf4409SJohn Baldwin     if (!(list->el_flags & EHL_INITTED)) {
114fcb893a8SMike Smith 	TAILQ_INIT(&list->el_entries);
115ecdf4409SJohn Baldwin 	mtx_init(&list->el_lock, name, "eventhandler list", MTX_DEF);
116ecdf4409SJohn Baldwin 	atomic_store_rel_int(&list->el_flags, EHL_INITTED);
117fcb893a8SMike Smith     }
11819a0f7e1SAlfred Perlstein     mtx_unlock(&eventhandler_mutex);
119fcb893a8SMike Smith 
120fcb893a8SMike Smith     /* allocate an entry for this handler, populate it */
121ecdf4409SJohn Baldwin     eg = malloc(sizeof(struct eventhandler_entry_generic), M_EVENTHANDLER,
122ecdf4409SJohn Baldwin 	M_WAITOK | M_ZERO);
123fcb893a8SMike Smith     eg->func = func;
124fcb893a8SMike Smith     eg->ee.ee_arg = arg;
125fcb893a8SMike Smith     eg->ee.ee_priority = priority;
126ecdf4409SJohn Baldwin     KASSERT(priority != EHE_DEAD_PRIORITY,
127ecdf4409SJohn Baldwin 	("%s: handler for %s registered with dead priority", __func__, name));
128fcb893a8SMike Smith 
129fcb893a8SMike Smith     /* sort it into the list */
130ecdf4409SJohn Baldwin     CTR4(KTR_EVH, "%s: adding item %p (function %p) to \"%s\"", __func__, eg,
131ecdf4409SJohn Baldwin 	func, name);
132ecdf4409SJohn Baldwin     EHL_LOCK(list);
133ecdf4409SJohn Baldwin     TAILQ_FOREACH(ep, &list->el_entries, ee_link) {
134ecdf4409SJohn Baldwin 	if (ep->ee_priority != EHE_DEAD_PRIORITY &&
135ecdf4409SJohn Baldwin 	    eg->ee.ee_priority < ep->ee_priority) {
136fcb893a8SMike Smith 	    TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
137fcb893a8SMike Smith 	    break;
138fcb893a8SMike Smith 	}
139fcb893a8SMike Smith     }
140fcb893a8SMike Smith     if (ep == NULL)
141fcb893a8SMike Smith 	TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
142ecdf4409SJohn Baldwin     EHL_UNLOCK(list);
143fcb893a8SMike Smith     return(&eg->ee);
144fcb893a8SMike Smith }
145fcb893a8SMike Smith 
146fcb893a8SMike Smith void
147fcb893a8SMike Smith eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
148fcb893a8SMike Smith {
149fcb893a8SMike Smith     struct eventhandler_entry	*ep = tag;
150fcb893a8SMike Smith 
151ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
152fcb893a8SMike Smith     if (ep != NULL) {
153fcb893a8SMike Smith 	/* remove just this entry */
154ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
155ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: removing item %p from \"%s\"", __func__, ep,
156ecdf4409SJohn Baldwin 		list->el_name);
157fcb893a8SMike Smith 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
158fcb893a8SMike Smith 	    free(ep, M_EVENTHANDLER);
159fcb893a8SMike Smith 	} else {
160ecdf4409SJohn Baldwin 	    CTR3(KTR_EVH, "%s: marking item %p from \"%s\" as dead", __func__,
161ecdf4409SJohn Baldwin 		ep, list->el_name);
162ecdf4409SJohn Baldwin 	    ep->ee_priority = EHE_DEAD_PRIORITY;
163ecdf4409SJohn Baldwin 	}
164ecdf4409SJohn Baldwin     } else {
165fcb893a8SMike Smith 	/* remove entire list */
166ecdf4409SJohn Baldwin 	if (list->el_runcount == 0) {
167ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: removing all items from \"%s\"", __func__,
168ecdf4409SJohn Baldwin 		list->el_name);
169fcb893a8SMike Smith 	    while (!TAILQ_EMPTY(&list->el_entries)) {
170fcb893a8SMike Smith 		ep = TAILQ_FIRST(&list->el_entries);
1711b727751SPoul-Henning Kamp 		TAILQ_REMOVE(&list->el_entries, ep, ee_link);
172fcb893a8SMike Smith 		free(ep, M_EVENTHANDLER);
173fcb893a8SMike Smith 	    }
174ecdf4409SJohn Baldwin 	} else {
175ecdf4409SJohn Baldwin 	    CTR2(KTR_EVH, "%s: marking all items from \"%s\" as dead",
176ecdf4409SJohn Baldwin 		__func__, list->el_name);
177ecdf4409SJohn Baldwin 	    TAILQ_FOREACH(ep, &list->el_entries, ee_link)
178ecdf4409SJohn Baldwin 		ep->ee_priority = EHE_DEAD_PRIORITY;
179fcb893a8SMike Smith 	}
180ecdf4409SJohn Baldwin     }
181ecdf4409SJohn Baldwin     EHL_UNLOCK(list);
182fcb893a8SMike Smith }
183fcb893a8SMike Smith 
184ecdf4409SJohn Baldwin /*
185ecdf4409SJohn Baldwin  * Internal version for use when eventhandler list is already locked.
186ecdf4409SJohn Baldwin  */
187ecdf4409SJohn Baldwin static struct eventhandler_list *
188fdf20233SJoseph Koshy _eventhandler_find_list(const char *name)
189ecdf4409SJohn Baldwin {
190ecdf4409SJohn Baldwin     struct eventhandler_list	*list;
191ecdf4409SJohn Baldwin 
192ecdf4409SJohn Baldwin     mtx_assert(&eventhandler_mutex, MA_OWNED);
193ecdf4409SJohn Baldwin     TAILQ_FOREACH(list, &eventhandler_lists, el_link) {
194ecdf4409SJohn Baldwin 	if (!strcmp(name, list->el_name))
195ecdf4409SJohn Baldwin 	    break;
196ecdf4409SJohn Baldwin     }
197ecdf4409SJohn Baldwin     return (list);
198ecdf4409SJohn Baldwin }
199ecdf4409SJohn Baldwin 
200ecdf4409SJohn Baldwin /*
201ecdf4409SJohn Baldwin  * Lookup a "slow" list by name.  Returns with the list locked.
202ecdf4409SJohn Baldwin  */
203fcb893a8SMike Smith struct eventhandler_list *
204fdf20233SJoseph Koshy eventhandler_find_list(const char *name)
205fcb893a8SMike Smith {
206fcb893a8SMike Smith     struct eventhandler_list	*list;
207146be906SJake Burkholder 
208146be906SJake Burkholder     if (!eventhandler_lists_initted)
209146be906SJake Burkholder 	return(NULL);
210fcb893a8SMike Smith 
211fcb893a8SMike Smith     /* scan looking for the requested list */
2129ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
213ecdf4409SJohn Baldwin     list = _eventhandler_find_list(name);
214ecdf4409SJohn Baldwin     if (list != NULL)
215ecdf4409SJohn Baldwin 	EHL_LOCK(list);
2169ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
2176595c331SMike Smith 
218fcb893a8SMike Smith     return(list);
219fcb893a8SMike Smith }
2206595c331SMike Smith 
221ecdf4409SJohn Baldwin /*
222ecdf4409SJohn Baldwin  * Prune "dead" entries from an eventhandler list.
223ecdf4409SJohn Baldwin  */
224ecdf4409SJohn Baldwin void
225ecdf4409SJohn Baldwin eventhandler_prune_list(struct eventhandler_list *list)
226ecdf4409SJohn Baldwin {
227ecdf4409SJohn Baldwin     struct eventhandler_entry *ep, *en;
228ecdf4409SJohn Baldwin 
229ecdf4409SJohn Baldwin     CTR2(KTR_EVH, "%s: pruning list \"%s\"", __func__, list->el_name);
230ecdf4409SJohn Baldwin     EHL_LOCK_ASSERT(list, MA_OWNED);
231ecdf4409SJohn Baldwin     ep = TAILQ_FIRST(&list->el_entries);
232ecdf4409SJohn Baldwin     while (ep != NULL) {
233ecdf4409SJohn Baldwin 	en = TAILQ_NEXT(ep, ee_link);
234ecdf4409SJohn Baldwin 	if (ep->ee_priority == EHE_DEAD_PRIORITY) {
235ecdf4409SJohn Baldwin 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
236ecdf4409SJohn Baldwin 	    free(ep, M_EVENTHANDLER);
237ecdf4409SJohn Baldwin 	}
238ecdf4409SJohn Baldwin 	ep = en;
239ecdf4409SJohn Baldwin     }
240ecdf4409SJohn Baldwin }
241