nrelease: Minor style improvements to the Makefile
[dragonfly.git] / sys / kern / subr_eventhandler.c
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 MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
38
39 /* List of 'slow' lists */
40 static TAILQ_HEAD(, eventhandler_list)  eventhandler_lists = TAILQ_HEAD_INITIALIZER(eventhandler_lists);
41 static struct lwkt_token evlist_token = LWKT_TOKEN_INITIALIZER(evlist_token);
42
43 struct eventhandler_entry_generic 
44 {
45     struct eventhandler_entry   ee;
46     void                        (* func)(void);
47 };
48
49 /* 
50  * Insertion is O(n) due to the priority scan, but optimises to O(1)
51  * if all priorities are identical.
52  *
53  * MPSAFE
54  */
55 eventhandler_tag
56 eventhandler_register(struct eventhandler_list *list, const char *name, 
57                       void *func, void *arg, int priority)
58 {
59     struct eventhandler_entry_generic   *eg;
60     struct eventhandler_entry           *ep;
61     
62     lwkt_gettoken(&evlist_token);
63
64     /*
65      * find/create the list as needed
66      */
67     while (list == NULL) {
68         list = eventhandler_find_list(name);
69         if (list)
70                 break;
71         list = kmalloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
72                        M_EVENTHANDLER, M_INTWAIT);
73         if (eventhandler_find_list(name)) {
74             kfree(list, M_EVENTHANDLER);
75             list = NULL;
76         } else {
77             list->el_flags = 0;
78             list->el_name = (char *)list + sizeof(struct eventhandler_list);
79             strcpy(list->el_name, name);
80             TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
81         }
82     }
83
84     if (!(list->el_flags & EHE_INITTED)) {
85         TAILQ_INIT(&list->el_entries);
86         list->el_flags = EHE_INITTED;
87     }
88     
89     /* allocate an entry for this handler, populate it */
90     eg = kmalloc(sizeof(struct eventhandler_entry_generic),
91                 M_EVENTHANDLER, M_INTWAIT);
92     eg->func = func;
93     eg->ee.ee_arg = arg;
94     eg->ee.ee_priority = priority;
95     
96     /* sort it into the list */
97     for (ep = TAILQ_FIRST(&list->el_entries);
98          ep != NULL; 
99          ep = TAILQ_NEXT(ep, ee_link)) {
100         if (eg->ee.ee_priority < ep->ee_priority) {
101             TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
102             break;
103         }
104     }
105     if (ep == NULL)
106         TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
107     lwkt_reltoken(&evlist_token);
108
109     return(&eg->ee);
110 }
111
112 /*
113  * MPSAFE
114  */
115 void
116 eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
117 {
118     struct eventhandler_entry   *ep = tag;
119
120     lwkt_gettoken(&evlist_token);
121     /* XXX insert diagnostic check here? */
122
123     if (ep != NULL) {
124         /* remove just this entry */
125         TAILQ_REMOVE(&list->el_entries, ep, ee_link);
126         kfree(ep, M_EVENTHANDLER);
127     } else {
128         /* remove entire list */
129         while (!TAILQ_EMPTY(&list->el_entries)) {
130             ep = TAILQ_FIRST(&list->el_entries);
131             TAILQ_REMOVE(&list->el_entries, ep, ee_link);
132             kfree(ep, M_EVENTHANDLER);
133         }
134     }
135     lwkt_reltoken(&evlist_token);
136 }
137
138 /*
139  * Locate the requested list
140  */
141 struct eventhandler_list *
142 eventhandler_find_list(const char *name)
143 {
144     struct eventhandler_list    *list;
145
146     lwkt_gettoken(&evlist_token);
147     for (list = TAILQ_FIRST(&eventhandler_lists); 
148          list != NULL; 
149          list = TAILQ_NEXT(list, el_link)) {
150         if (!strcmp(name, list->el_name))
151             break;
152     }
153     lwkt_reltoken(&evlist_token);
154     return(list);
155 }