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