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