xref: /freebsd/sys/kern/subr_eventhandler.c (revision fb919e4d)
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  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27fcb893a8SMike Smith  */
28fcb893a8SMike Smith 
29fcb893a8SMike Smith #include <sys/param.h>
30fcb893a8SMike Smith #include <sys/kernel.h>
31f34fa851SJohn Baldwin #include <sys/lock.h>
32fcb893a8SMike Smith #include <sys/malloc.h>
3306592dd1SJohn Baldwin #include <sys/mutex.h>
34fb919e4dSMark Murray #include <sys/proc.h>
35fcb893a8SMike Smith #include <sys/systm.h>
36fcb893a8SMike Smith #include <sys/eventhandler.h>
37fcb893a8SMike Smith 
38959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
39fcb893a8SMike Smith 
40fcb893a8SMike Smith /* List of 'slow' lists */
41e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists;
42fcb893a8SMike Smith static int				eventhandler_lists_initted = 0;
43d543796fSJohn Baldwin static struct mtx			eventhandler_mutex;
44fcb893a8SMike Smith 
45fcb893a8SMike Smith struct eventhandler_entry_generic
46fcb893a8SMike Smith {
47fcb893a8SMike Smith     struct eventhandler_entry	ee;
48fcb893a8SMike Smith     void			(* func)(void);
49fcb893a8SMike Smith };
50fcb893a8SMike Smith 
51fcb893a8SMike Smith /*
5206592dd1SJohn Baldwin  * Initialize the eventhandler mutex and list.
5306592dd1SJohn Baldwin  */
5406592dd1SJohn Baldwin static void
5506592dd1SJohn Baldwin eventhandler_init(void *dummy __unused)
5606592dd1SJohn Baldwin {
5706592dd1SJohn Baldwin     TAILQ_INIT(&eventhandler_lists);
5808812b39SBosko Milekic     mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF | MTX_RECURSE);
5906592dd1SJohn Baldwin     eventhandler_lists_initted = 1;
6006592dd1SJohn Baldwin }
6106592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
6206592dd1SJohn Baldwin     NULL)
6306592dd1SJohn Baldwin 
6406592dd1SJohn Baldwin /*
65fcb893a8SMike Smith  * Insertion is O(n) due to the priority scan, but optimises to O(1)
66fcb893a8SMike Smith  * if all priorities are identical.
67fcb893a8SMike Smith  */
68fcb893a8SMike Smith eventhandler_tag
69fcb893a8SMike Smith eventhandler_register(struct eventhandler_list *list, char *name,
70fcb893a8SMike Smith 		      void *func, void *arg, int priority)
71fcb893a8SMike Smith {
72fcb893a8SMike Smith     struct eventhandler_entry_generic	*eg;
73fcb893a8SMike Smith     struct eventhandler_entry		*ep;
74fcb893a8SMike Smith 
7506592dd1SJohn Baldwin     KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
76fcb893a8SMike Smith 
776595c331SMike Smith     /* lock the eventhandler lists */
789ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
796595c331SMike Smith 
80fcb893a8SMike Smith     /* Do we need to find/create the (slow) list? */
81fcb893a8SMike Smith     if (list == NULL) {
82fcb893a8SMike Smith 	/* look for a matching, existing list */
83fcb893a8SMike Smith 	list = eventhandler_find_list(name);
84fcb893a8SMike Smith 
85fcb893a8SMike Smith 	/* Do we need to create the list? */
86fcb893a8SMike Smith 	if (list == NULL) {
87fcb893a8SMike Smith 	    if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
886595c331SMike Smith 			       M_EVENTHANDLER, M_NOWAIT)) == NULL) {
899ed346baSBosko Milekic 		mtx_unlock(&eventhandler_mutex);
90fcb893a8SMike Smith 		return(NULL);
916595c331SMike Smith 	    }
92fcb893a8SMike Smith 	    list->el_flags = 0;
93fcb893a8SMike Smith 	    list->el_name = (char *)list + sizeof(struct eventhandler_list);
94fcb893a8SMike Smith 	    strcpy(list->el_name, name);
95fcb893a8SMike Smith 	    TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
96fcb893a8SMike Smith 	}
97fcb893a8SMike Smith     }
98fcb893a8SMike Smith     if (!(list->el_flags & EHE_INITTED)) {
99fcb893a8SMike Smith 	TAILQ_INIT(&list->el_entries);
10006592dd1SJohn Baldwin 	lockinit(&list->el_lock, PZERO, name, 0, 0);
101fcb893a8SMike Smith 	list->el_flags = EHE_INITTED;
102fcb893a8SMike Smith     }
103fcb893a8SMike Smith 
104fcb893a8SMike Smith     /* allocate an entry for this handler, populate it */
105fcb893a8SMike Smith     if ((eg = malloc(sizeof(struct eventhandler_entry_generic),
1066595c331SMike Smith 		     M_EVENTHANDLER, M_NOWAIT)) == NULL) {
1079ed346baSBosko Milekic 	mtx_unlock(&eventhandler_mutex);
108fcb893a8SMike Smith 	return(NULL);
1096595c331SMike Smith     }
110fcb893a8SMike Smith     eg->func = func;
111fcb893a8SMike Smith     eg->ee.ee_arg = arg;
112fcb893a8SMike Smith     eg->ee.ee_priority = priority;
113fcb893a8SMike Smith 
114fcb893a8SMike Smith     /* sort it into the list */
11506592dd1SJohn Baldwin     lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC);
116fcb893a8SMike Smith     for (ep = TAILQ_FIRST(&list->el_entries);
117fcb893a8SMike Smith 	 ep != NULL;
118fcb893a8SMike Smith 	 ep = TAILQ_NEXT(ep, ee_link)) {
119fcb893a8SMike Smith 	if (eg->ee.ee_priority < ep->ee_priority) {
120fcb893a8SMike Smith 	    TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
121fcb893a8SMike Smith 	    break;
122fcb893a8SMike Smith 	}
123fcb893a8SMike Smith     }
124fcb893a8SMike Smith     if (ep == NULL)
125fcb893a8SMike Smith 	TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
12606592dd1SJohn Baldwin     lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC);
1279ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
128fcb893a8SMike Smith     return(&eg->ee);
129fcb893a8SMike Smith }
130fcb893a8SMike Smith 
131fcb893a8SMike Smith void
132fcb893a8SMike Smith eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
133fcb893a8SMike Smith {
134fcb893a8SMike Smith     struct eventhandler_entry	*ep = tag;
135fcb893a8SMike Smith 
136fcb893a8SMike Smith     /* XXX insert diagnostic check here? */
13706592dd1SJohn Baldwin     lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC);
138fcb893a8SMike Smith     if (ep != NULL) {
139fcb893a8SMike Smith 	/* remove just this entry */
140fcb893a8SMike Smith 	TAILQ_REMOVE(&list->el_entries, ep, ee_link);
141fcb893a8SMike Smith 	free(ep, M_EVENTHANDLER);
142fcb893a8SMike Smith     } else {
143fcb893a8SMike Smith 	/* remove entire list */
144fcb893a8SMike Smith 	while (!TAILQ_EMPTY(&list->el_entries)) {
145fcb893a8SMike Smith 	    ep = TAILQ_FIRST(&list->el_entries);
1461b727751SPoul-Henning Kamp 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
147fcb893a8SMike Smith 	    free(ep, M_EVENTHANDLER);
148fcb893a8SMike Smith 	}
149fcb893a8SMike Smith     }
15006592dd1SJohn Baldwin     lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC);
151fcb893a8SMike Smith }
152fcb893a8SMike Smith 
153fcb893a8SMike Smith struct eventhandler_list *
154fcb893a8SMike Smith eventhandler_find_list(char *name)
155fcb893a8SMike Smith {
156fcb893a8SMike Smith     struct eventhandler_list	*list;
157fcb893a8SMike Smith 
158fcb893a8SMike Smith     /* scan looking for the requested list */
1599ed346baSBosko Milekic     mtx_lock(&eventhandler_mutex);
160fcb893a8SMike Smith     for (list = TAILQ_FIRST(&eventhandler_lists);
161fcb893a8SMike Smith 	 list != NULL;
162fcb893a8SMike Smith 	 list = TAILQ_NEXT(list, el_link)) {
163fcb893a8SMike Smith 	if (!strcmp(name, list->el_name))
164fcb893a8SMike Smith 	    break;
165fcb893a8SMike Smith     }
1669ed346baSBosko Milekic     mtx_unlock(&eventhandler_mutex);
1676595c331SMike Smith 
168fcb893a8SMike Smith     return(list);
169fcb893a8SMike Smith }
1706595c331SMike Smith 
171