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