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