xref: /dragonfly/sys/kern/subr_eventhandler.c (revision 73610d44)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/kern/subr_eventhandler.c,v 1.3 1999/11/16 16:28:57 phk Exp $
29  */
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
35 #include <sys/eventhandler.h>
36 
37 #include <sys/mplock2.h>
38 
39 MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
40 
41 /* List of 'slow' lists */
42 static TAILQ_HEAD(, eventhandler_list)	eventhandler_lists = TAILQ_HEAD_INITIALIZER(eventhandler_lists);
43 static struct lwkt_token evlist_token = LWKT_TOKEN_INITIALIZER(evlist_token);
44 
45 struct eventhandler_entry_generic
46 {
47     struct eventhandler_entry	ee;
48     void			(* func)(void);
49 };
50 
51 /*
52  * Insertion is O(n) due to the priority scan, but optimises to O(1)
53  * if all priorities are identical.
54  *
55  * MPSAFE
56  */
57 eventhandler_tag
58 eventhandler_register(struct eventhandler_list *list, const char *name,
59 		      void *func, void *arg, int priority)
60 {
61     struct eventhandler_entry_generic	*eg;
62     struct eventhandler_entry		*ep;
63 
64     lwkt_gettoken(&evlist_token);
65 
66     /*
67      * find/create the list as needed
68      */
69     while (list == NULL) {
70 	list = eventhandler_find_list(name);
71 	if (list)
72 		break;
73 	list = kmalloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
74 		       M_EVENTHANDLER, M_INTWAIT);
75 	if (eventhandler_find_list(name)) {
76 	    kfree(list, M_EVENTHANDLER);
77 	    list = NULL;
78 	} else {
79 	    list->el_flags = 0;
80 	    list->el_name = (char *)list + sizeof(struct eventhandler_list);
81 	    strcpy(list->el_name, name);
82 	    TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
83 	}
84     }
85 
86     if (!(list->el_flags & EHE_INITTED)) {
87 	TAILQ_INIT(&list->el_entries);
88 	list->el_flags = EHE_INITTED;
89     }
90 
91     /* allocate an entry for this handler, populate it */
92     eg = kmalloc(sizeof(struct eventhandler_entry_generic),
93 		M_EVENTHANDLER, M_INTWAIT);
94     eg->func = func;
95     eg->ee.ee_arg = arg;
96     eg->ee.ee_priority = priority;
97 
98     /* sort it into the list */
99     for (ep = TAILQ_FIRST(&list->el_entries);
100 	 ep != NULL;
101 	 ep = TAILQ_NEXT(ep, ee_link)) {
102 	if (eg->ee.ee_priority < ep->ee_priority) {
103 	    TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
104 	    break;
105 	}
106     }
107     if (ep == NULL)
108 	TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
109     lwkt_reltoken(&evlist_token);
110 
111     return(&eg->ee);
112 }
113 
114 /*
115  * MPSAFE
116  */
117 void
118 eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
119 {
120     struct eventhandler_entry	*ep = tag;
121 
122     lwkt_gettoken(&evlist_token);
123     /* XXX insert diagnostic check here? */
124 
125     if (ep != NULL) {
126 	/* remove just this entry */
127 	TAILQ_REMOVE(&list->el_entries, ep, ee_link);
128 	kfree(ep, M_EVENTHANDLER);
129     } else {
130 	/* remove entire list */
131 	while (!TAILQ_EMPTY(&list->el_entries)) {
132 	    ep = TAILQ_FIRST(&list->el_entries);
133 	    TAILQ_REMOVE(&list->el_entries, ep, ee_link);
134 	    kfree(ep, M_EVENTHANDLER);
135 	}
136     }
137     lwkt_reltoken(&evlist_token);
138 }
139 
140 /*
141  * Locate the requested list
142  */
143 struct eventhandler_list *
144 eventhandler_find_list(const char *name)
145 {
146     struct eventhandler_list	*list;
147 
148     lwkt_gettoken(&evlist_token);
149     for (list = TAILQ_FIRST(&eventhandler_lists);
150 	 list != NULL;
151 	 list = TAILQ_NEXT(list, el_link)) {
152 	if (!strcmp(name, list->el_name))
153 	    break;
154     }
155     lwkt_reltoken(&evlist_token);
156     return(list);
157 }
158