xref: /freebsd/sys/kern/subr_eventhandler.c (revision bd80847a854e6a69c9fe3b714b3885c2f5f414d0)
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 #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 
38 static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
39 
40 /* List of 'slow' lists */
41 static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
42 static int				eventhandler_lists_initted = 0;
43 static struct mtx			eventhandler_mutex;
44 
45 struct eventhandler_entry_generic
46 {
47     struct eventhandler_entry	ee;
48     void			(* func)(void);
49 };
50 
51 /*
52  * Initialize the eventhandler mutex and list.
53  */
54 static void
55 eventhandler_init(void *dummy __unused)
56 {
57     TAILQ_INIT(&eventhandler_lists);
58     mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF | MTX_RECURSE);
59     eventhandler_lists_initted = 1;
60 }
61 SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
62     NULL)
63 
64 /*
65  * Insertion is O(n) due to the priority scan, but optimises to O(1)
66  * if all priorities are identical.
67  */
68 eventhandler_tag
69 eventhandler_register(struct eventhandler_list *list, char *name,
70 		      void *func, void *arg, int priority)
71 {
72     struct eventhandler_entry_generic	*eg;
73     struct eventhandler_entry		*ep;
74 
75     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
76 
77     /* lock the eventhandler lists */
78     mtx_lock(&eventhandler_mutex);
79 
80     /* Do we need to find/create the (slow) list? */
81     if (list == NULL) {
82 	/* look for a matching, existing list */
83 	list = eventhandler_find_list(name);
84 
85 	/* Do we need to create the list? */
86 	if (list == NULL) {
87 	    if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
88 			       M_EVENTHANDLER, M_NOWAIT)) == NULL) {
89 		mtx_unlock(&eventhandler_mutex);
90 		return(NULL);
91 	    }
92 	    list->el_flags = 0;
93 	    bzero(&list->el_lock, sizeof(list->el_lock));
94 	    list->el_name = (char *)list + sizeof(struct eventhandler_list);
95 	    strcpy(list->el_name, name);
96 	    TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
97 	}
98     }
99     if (!(list->el_flags & EHE_INITTED)) {
100 	TAILQ_INIT(&list->el_entries);
101 	sx_init(&list->el_lock, name);
102 	list->el_flags = EHE_INITTED;
103     }
104 
105     /* allocate an entry for this handler, populate it */
106     if ((eg = malloc(sizeof(struct eventhandler_entry_generic),
107 		     M_EVENTHANDLER, M_NOWAIT)) == NULL) {
108 	mtx_unlock(&eventhandler_mutex);
109 	return(NULL);
110     }
111     eg->func = func;
112     eg->ee.ee_arg = arg;
113     eg->ee.ee_priority = priority;
114 
115     /* sort it into the list */
116     EHE_LOCK(list);
117     for (ep = TAILQ_FIRST(&list->el_entries);
118 	 ep != NULL;
119 	 ep = TAILQ_NEXT(ep, ee_link)) {
120 	if (eg->ee.ee_priority < ep->ee_priority) {
121 	    TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
122 	    break;
123 	}
124     }
125     if (ep == NULL)
126 	TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
127     EHE_UNLOCK(list);
128     mtx_unlock(&eventhandler_mutex);
129     return(&eg->ee);
130 }
131 
132 void
133 eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
134 {
135     struct eventhandler_entry	*ep = tag;
136 
137     /* XXX insert diagnostic check here? */
138     EHE_LOCK(list);
139     if (ep != NULL) {
140 	/* remove just this entry */
141 	TAILQ_REMOVE(&list->el_entries, ep, ee_link);
142 	free(ep, M_EVENTHANDLER);
143     } else {
144 	/* remove entire list */
145 	while (!TAILQ_EMPTY(&list->el_entries)) {
146 	    ep = TAILQ_FIRST(&list->el_entries);
147 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
148 	    free(ep, M_EVENTHANDLER);
149 	}
150     }
151     EHE_UNLOCK(list);
152 }
153 
154 struct eventhandler_list *
155 eventhandler_find_list(char *name)
156 {
157     struct eventhandler_list	*list;
158 
159     if (!eventhandler_lists_initted)
160 	return(NULL);
161 
162     /* scan looking for the requested list */
163     mtx_lock(&eventhandler_mutex);
164     for (list = TAILQ_FIRST(&eventhandler_lists);
165 	 list != NULL;
166 	 list = TAILQ_NEXT(list, el_link)) {
167 	if (!strcmp(name, list->el_name))
168 	    break;
169     }
170     mtx_unlock(&eventhandler_mutex);
171 
172     return(list);
173 }
174 
175