subr_eventhandler.c revision 66205
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: head/sys/kern/subr_eventhandler.c 66205 2000-09-22 03:17:35Z msmith $
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	mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF);
62	eventhandler_lists_initted = 1;
63    }
64
65    /* lock the eventhandler lists */
66    mtx_enter(&eventhandler_mutex, MTX_DEF);
67
68    /* Do we need to find/create the (slow) list? */
69    if (list == NULL) {
70	/* look for a matching, existing list */
71	list = eventhandler_find_list(name);
72
73	/* Do we need to create the list? */
74	if (list == NULL) {
75	    if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
76			       M_EVENTHANDLER, M_NOWAIT)) == NULL) {
77		mtx_exit(&eventhandler_mutex, MTX_DEF);
78		return(NULL);
79	    }
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    if (!(list->el_flags & EHE_INITTED)) {
87	TAILQ_INIT(&list->el_entries);
88	mtx_init(&list->el_mutex, name, MTX_DEF);
89	list->el_flags = EHE_INITTED;
90    }
91
92    /* allocate an entry for this handler, populate it */
93    if ((eg = malloc(sizeof(struct eventhandler_entry_generic),
94		     M_EVENTHANDLER, M_NOWAIT)) == NULL) {
95	mtx_exit(&eventhandler_mutex, MTX_DEF);
96	return(NULL);
97    }
98    eg->func = func;
99    eg->ee.ee_arg = arg;
100    eg->ee.ee_priority = priority;
101
102    /* sort it into the list */
103    mtx_enter(&list->el_mutex, MTX_DEF);
104    for (ep = TAILQ_FIRST(&list->el_entries);
105	 ep != NULL;
106	 ep = TAILQ_NEXT(ep, ee_link)) {
107	if (eg->ee.ee_priority < ep->ee_priority) {
108	    TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
109	    break;
110	}
111    }
112    if (ep == NULL)
113	TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
114    mtx_exit(&list->el_mutex, MTX_DEF);
115    mtx_exit(&eventhandler_mutex, MTX_DEF);
116    return(&eg->ee);
117}
118
119void
120eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
121{
122    struct eventhandler_entry	*ep = tag;
123
124    /* XXX insert diagnostic check here? */
125    mtx_enter(&list->el_mutex, MTX_DEF);
126    if (ep != NULL) {
127	/* remove just this entry */
128	TAILQ_REMOVE(&list->el_entries, ep, ee_link);
129	free(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	    free(ep, M_EVENTHANDLER);
136	}
137    }
138    mtx_exit(&list->el_mutex, MTX_DEF);
139}
140
141struct eventhandler_list *
142eventhandler_find_list(char *name)
143{
144    struct eventhandler_list	*list;
145
146    /* scan looking for the requested list */
147    mtx_enter(&eventhandler_mutex, MTX_DEF);
148    for (list = TAILQ_FIRST(&eventhandler_lists);
149	 list != NULL;
150	 list = TAILQ_NEXT(list, el_link)) {
151	if (!strcmp(name, list->el_name))
152	    break;
153    }
154    mtx_exit(&eventhandler_mutex, MTX_DEF);
155
156    return(list);
157}
158
159