Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / kern / subr_eventhandler.c
CommitLineData
984263bc
MD
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: src/sys/kern/subr_eventhandler.c,v 1.3 1999/11/16 16:28:57 phk Exp $
27 */
28
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/malloc.h>
32#include <sys/systm.h>
33#include <sys/eventhandler.h>
34
35MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
36
37/* List of 'slow' lists */
38static TAILQ_HEAD(, eventhandler_list) eventhandler_lists;
39static int eventhandler_lists_initted = 0;
40
41struct eventhandler_entry_generic
42{
43 struct eventhandler_entry ee;
44 void (* func)(void);
45};
46
47/*
48 * Insertion is O(n) due to the priority scan, but optimises to O(1)
49 * if all priorities are identical.
50 */
51eventhandler_tag
52eventhandler_register(struct eventhandler_list *list, char *name,
53 void *func, void *arg, int priority)
54{
55 struct eventhandler_entry_generic *eg;
56 struct eventhandler_entry *ep;
57
58 /* avoid the need for a SYSINIT just to init the list */
59 if (!eventhandler_lists_initted) {
60 TAILQ_INIT(&eventhandler_lists);
61 eventhandler_lists_initted = 1;
62 }
63
64 /* Do we need to find/create the (slow) list? */
65 if (list == NULL) {
66 /* look for a matching, existing list */
67 list = eventhandler_find_list(name);
68
69 /* Do we need to create the list? */
70 if (list == NULL) {
71 if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
72 M_EVENTHANDLER, M_NOWAIT)) == NULL)
73 return(NULL);
74 list->el_flags = 0;
75 list->el_name = (char *)list + sizeof(struct eventhandler_list);
76 strcpy(list->el_name, name);
77 TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
78 }
79 }
80 if (!(list->el_flags & EHE_INITTED)) {
81 TAILQ_INIT(&list->el_entries);
82 list->el_flags = EHE_INITTED;
83 }
84
85 /* allocate an entry for this handler, populate it */
86 if ((eg = malloc(sizeof(struct eventhandler_entry_generic),
87 M_EVENTHANDLER, M_NOWAIT)) == NULL)
88 return(NULL);
89 eg->func = func;
90 eg->ee.ee_arg = arg;
91 eg->ee.ee_priority = priority;
92
93 /* sort it into the list */
94 for (ep = TAILQ_FIRST(&list->el_entries);
95 ep != NULL;
96 ep = TAILQ_NEXT(ep, ee_link)) {
97 if (eg->ee.ee_priority < ep->ee_priority) {
98 TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
99 break;
100 }
101 }
102 if (ep == NULL)
103 TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
104 return(&eg->ee);
105}
106
107void
108eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
109{
110 struct eventhandler_entry *ep = tag;
111
112 /* XXX insert diagnostic check here? */
113 if (ep != NULL) {
114 /* remove just this entry */
115 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
116 free(ep, M_EVENTHANDLER);
117 } else {
118 /* remove entire list */
119 while (!TAILQ_EMPTY(&list->el_entries)) {
120 ep = TAILQ_FIRST(&list->el_entries);
121 TAILQ_REMOVE(&list->el_entries, ep, ee_link);
122 free(ep, M_EVENTHANDLER);
123 }
124 }
125}
126
127struct eventhandler_list *
128eventhandler_find_list(char *name)
129{
130 struct eventhandler_list *list;
131
132 /* scan looking for the requested list */
133 for (list = TAILQ_FIRST(&eventhandler_lists);
134 list != NULL;
135 list = TAILQ_NEXT(list, el_link)) {
136 if (!strcmp(name, list->el_name))
137 break;
138 }
139 return(list);
140}