1fcb893a8SMike Smith /*- 2fcb893a8SMike Smith * Copyright (c) 1999 Michael Smith <msmith@freebsd.org> 3fcb893a8SMike Smith * All rights reserved. 4fcb893a8SMike Smith * 5fcb893a8SMike Smith * Redistribution and use in source and binary forms, with or without 6fcb893a8SMike Smith * modification, are permitted provided that the following conditions 7fcb893a8SMike Smith * are met: 8fcb893a8SMike Smith * 1. Redistributions of source code must retain the above copyright 9fcb893a8SMike Smith * notice, this list of conditions and the following disclaimer. 10fcb893a8SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 11fcb893a8SMike Smith * notice, this list of conditions and the following disclaimer in the 12fcb893a8SMike Smith * documentation and/or other materials provided with the distribution. 13fcb893a8SMike Smith * 14fcb893a8SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15fcb893a8SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16fcb893a8SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17fcb893a8SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18fcb893a8SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19fcb893a8SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20fcb893a8SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21fcb893a8SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22fcb893a8SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23fcb893a8SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24fcb893a8SMike Smith * SUCH DAMAGE. 25fcb893a8SMike Smith * 26fcb893a8SMike Smith * $Id$ 27fcb893a8SMike Smith */ 28fcb893a8SMike Smith 29fcb893a8SMike Smith #include <sys/param.h> 30fcb893a8SMike Smith #include <sys/kernel.h> 31fcb893a8SMike Smith #include <sys/malloc.h> 32fcb893a8SMike Smith #include <sys/systm.h> 33fcb893a8SMike Smith #include <sys/eventhandler.h> 34fcb893a8SMike Smith 35fcb893a8SMike Smith MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records"); 36fcb893a8SMike Smith 37fcb893a8SMike Smith /* List of 'slow' lists */ 38fcb893a8SMike Smith static TAILQ_HEAD(, eventhandler_list) eventhandler_lists; 39fcb893a8SMike Smith static int eventhandler_lists_initted = 0; 40fcb893a8SMike Smith 41fcb893a8SMike Smith struct eventhandler_entry_generic 42fcb893a8SMike Smith { 43fcb893a8SMike Smith struct eventhandler_entry ee; 44fcb893a8SMike Smith void (* func)(void); 45fcb893a8SMike Smith }; 46fcb893a8SMike Smith 47fcb893a8SMike Smith /* 48fcb893a8SMike Smith * Insertion is O(n) due to the priority scan, but optimises to O(1) 49fcb893a8SMike Smith * if all priorities are identical. 50fcb893a8SMike Smith */ 51fcb893a8SMike Smith eventhandler_tag 52fcb893a8SMike Smith eventhandler_register(struct eventhandler_list *list, char *name, 53fcb893a8SMike Smith void *func, void *arg, int priority) 54fcb893a8SMike Smith { 55fcb893a8SMike Smith struct eventhandler_entry_generic *eg; 56fcb893a8SMike Smith struct eventhandler_entry *ep; 57fcb893a8SMike Smith 58fcb893a8SMike Smith /* avoid the need for a SYSINIT just to init the list */ 59fcb893a8SMike Smith if (!eventhandler_lists_initted) { 60fcb893a8SMike Smith TAILQ_INIT(&eventhandler_lists); 61fcb893a8SMike Smith eventhandler_lists_initted = 1; 62fcb893a8SMike Smith } 63fcb893a8SMike Smith 64fcb893a8SMike Smith /* Do we need to find/create the (slow) list? */ 65fcb893a8SMike Smith if (list == NULL) { 66fcb893a8SMike Smith /* look for a matching, existing list */ 67fcb893a8SMike Smith list = eventhandler_find_list(name); 68fcb893a8SMike Smith 69fcb893a8SMike Smith /* Do we need to create the list? */ 70fcb893a8SMike Smith if (list == NULL) { 71fcb893a8SMike Smith if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1, 72fcb893a8SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) 73fcb893a8SMike Smith return(NULL); 74fcb893a8SMike Smith list->el_flags = 0; 75fcb893a8SMike Smith list->el_name = (char *)list + sizeof(struct eventhandler_list); 76fcb893a8SMike Smith strcpy(list->el_name, name); 77fcb893a8SMike Smith TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link); 78fcb893a8SMike Smith } 79fcb893a8SMike Smith } 80fcb893a8SMike Smith if (!(list->el_flags & EHE_INITTED)) { 81fcb893a8SMike Smith TAILQ_INIT(&list->el_entries); 82fcb893a8SMike Smith list->el_flags = EHE_INITTED; 83fcb893a8SMike Smith } 84fcb893a8SMike Smith 85fcb893a8SMike Smith /* allocate an entry for this handler, populate it */ 86fcb893a8SMike Smith if ((eg = malloc(sizeof(struct eventhandler_entry_generic), 87fcb893a8SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) 88fcb893a8SMike Smith return(NULL); 89fcb893a8SMike Smith eg->func = func; 90fcb893a8SMike Smith eg->ee.ee_arg = arg; 91fcb893a8SMike Smith eg->ee.ee_priority = priority; 92fcb893a8SMike Smith 93fcb893a8SMike Smith /* sort it into the list */ 94fcb893a8SMike Smith for (ep = TAILQ_FIRST(&list->el_entries); 95fcb893a8SMike Smith ep != NULL; 96fcb893a8SMike Smith ep = TAILQ_NEXT(ep, ee_link)) { 97fcb893a8SMike Smith if (eg->ee.ee_priority < ep->ee_priority) { 98fcb893a8SMike Smith TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link); 99fcb893a8SMike Smith break; 100fcb893a8SMike Smith } 101fcb893a8SMike Smith } 102fcb893a8SMike Smith if (ep == NULL) 103fcb893a8SMike Smith TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link); 104fcb893a8SMike Smith return(&eg->ee); 105fcb893a8SMike Smith } 106fcb893a8SMike Smith 107fcb893a8SMike Smith void 108fcb893a8SMike Smith eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) 109fcb893a8SMike Smith { 110fcb893a8SMike Smith struct eventhandler_entry *ep = tag; 111fcb893a8SMike Smith 112fcb893a8SMike Smith /* XXX insert diagnostic check here? */ 113fcb893a8SMike Smith if (ep != NULL) { 114fcb893a8SMike Smith /* remove just this entry */ 115fcb893a8SMike Smith TAILQ_REMOVE(&list->el_entries, ep, ee_link); 116fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 117fcb893a8SMike Smith } else { 118fcb893a8SMike Smith /* remove entire list */ 119fcb893a8SMike Smith while (!TAILQ_EMPTY(&list->el_entries)) { 120fcb893a8SMike Smith ep = TAILQ_FIRST(&list->el_entries); 121fcb893a8SMike Smith TAILQ_REMOVE(&list->el_entries, list->el_entries.tqh_first, ee_link); 122fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 123fcb893a8SMike Smith } 124fcb893a8SMike Smith } 125fcb893a8SMike Smith } 126fcb893a8SMike Smith 127fcb893a8SMike Smith struct eventhandler_list * 128fcb893a8SMike Smith eventhandler_find_list(char *name) 129fcb893a8SMike Smith { 130fcb893a8SMike Smith struct eventhandler_list *list; 131fcb893a8SMike Smith 132fcb893a8SMike Smith /* scan looking for the requested list */ 133fcb893a8SMike Smith for (list = TAILQ_FIRST(&eventhandler_lists); 134fcb893a8SMike Smith list != NULL; 135fcb893a8SMike Smith list = TAILQ_NEXT(list, el_link)) { 136fcb893a8SMike Smith if (!strcmp(name, list->el_name)) 137fcb893a8SMike Smith break; 138fcb893a8SMike Smith } 139fcb893a8SMike Smith return(list); 140fcb893a8SMike Smith } 141