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 * 26c3aac50fSPeter Wemm * $FreeBSD$ 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 35959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records"); 36fcb893a8SMike Smith 37fcb893a8SMike Smith /* List of 'slow' lists */ 38e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list) eventhandler_lists; 39fcb893a8SMike Smith static int eventhandler_lists_initted = 0; 40d543796fSJohn Baldwin static struct mtx eventhandler_mutex; 41fcb893a8SMike Smith 42fcb893a8SMike Smith struct eventhandler_entry_generic 43fcb893a8SMike Smith { 44fcb893a8SMike Smith struct eventhandler_entry ee; 45fcb893a8SMike Smith void (* func)(void); 46fcb893a8SMike Smith }; 47fcb893a8SMike Smith 48fcb893a8SMike Smith /* 49fcb893a8SMike Smith * Insertion is O(n) due to the priority scan, but optimises to O(1) 50fcb893a8SMike Smith * if all priorities are identical. 51fcb893a8SMike Smith */ 52fcb893a8SMike Smith eventhandler_tag 53fcb893a8SMike Smith eventhandler_register(struct eventhandler_list *list, char *name, 54fcb893a8SMike Smith void *func, void *arg, int priority) 55fcb893a8SMike Smith { 56fcb893a8SMike Smith struct eventhandler_entry_generic *eg; 57fcb893a8SMike Smith struct eventhandler_entry *ep; 58fcb893a8SMike Smith 59fcb893a8SMike Smith /* avoid the need for a SYSINIT just to init the list */ 60fcb893a8SMike Smith if (!eventhandler_lists_initted) { 61fcb893a8SMike Smith TAILQ_INIT(&eventhandler_lists); 626595c331SMike Smith mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF); 63fcb893a8SMike Smith eventhandler_lists_initted = 1; 64fcb893a8SMike Smith } 65fcb893a8SMike Smith 666595c331SMike Smith /* lock the eventhandler lists */ 676595c331SMike Smith mtx_enter(&eventhandler_mutex, MTX_DEF); 686595c331SMike Smith 69fcb893a8SMike Smith /* Do we need to find/create the (slow) list? */ 70fcb893a8SMike Smith if (list == NULL) { 71fcb893a8SMike Smith /* look for a matching, existing list */ 72fcb893a8SMike Smith list = eventhandler_find_list(name); 73fcb893a8SMike Smith 74fcb893a8SMike Smith /* Do we need to create the list? */ 75fcb893a8SMike Smith if (list == NULL) { 76fcb893a8SMike Smith if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1, 776595c331SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) { 786595c331SMike Smith mtx_exit(&eventhandler_mutex, MTX_DEF); 79fcb893a8SMike Smith return(NULL); 806595c331SMike Smith } 81fcb893a8SMike Smith list->el_flags = 0; 82fcb893a8SMike Smith list->el_name = (char *)list + sizeof(struct eventhandler_list); 83fcb893a8SMike Smith strcpy(list->el_name, name); 84fcb893a8SMike Smith TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link); 85fcb893a8SMike Smith } 86fcb893a8SMike Smith } 87fcb893a8SMike Smith if (!(list->el_flags & EHE_INITTED)) { 88fcb893a8SMike Smith TAILQ_INIT(&list->el_entries); 896595c331SMike Smith mtx_init(&list->el_mutex, name, MTX_DEF); 90fcb893a8SMike Smith list->el_flags = EHE_INITTED; 91fcb893a8SMike Smith } 92fcb893a8SMike Smith 93fcb893a8SMike Smith /* allocate an entry for this handler, populate it */ 94fcb893a8SMike Smith if ((eg = malloc(sizeof(struct eventhandler_entry_generic), 956595c331SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) { 966595c331SMike Smith mtx_exit(&eventhandler_mutex, MTX_DEF); 97fcb893a8SMike Smith return(NULL); 986595c331SMike Smith } 99fcb893a8SMike Smith eg->func = func; 100fcb893a8SMike Smith eg->ee.ee_arg = arg; 101fcb893a8SMike Smith eg->ee.ee_priority = priority; 102fcb893a8SMike Smith 103fcb893a8SMike Smith /* sort it into the list */ 1046595c331SMike Smith mtx_enter(&list->el_mutex, MTX_DEF); 105fcb893a8SMike Smith for (ep = TAILQ_FIRST(&list->el_entries); 106fcb893a8SMike Smith ep != NULL; 107fcb893a8SMike Smith ep = TAILQ_NEXT(ep, ee_link)) { 108fcb893a8SMike Smith if (eg->ee.ee_priority < ep->ee_priority) { 109fcb893a8SMike Smith TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link); 110fcb893a8SMike Smith break; 111fcb893a8SMike Smith } 112fcb893a8SMike Smith } 113fcb893a8SMike Smith if (ep == NULL) 114fcb893a8SMike Smith TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link); 1156595c331SMike Smith mtx_exit(&list->el_mutex, MTX_DEF); 1166595c331SMike Smith mtx_exit(&eventhandler_mutex, MTX_DEF); 117fcb893a8SMike Smith return(&eg->ee); 118fcb893a8SMike Smith } 119fcb893a8SMike Smith 120fcb893a8SMike Smith void 121fcb893a8SMike Smith eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) 122fcb893a8SMike Smith { 123fcb893a8SMike Smith struct eventhandler_entry *ep = tag; 124fcb893a8SMike Smith 125fcb893a8SMike Smith /* XXX insert diagnostic check here? */ 1266595c331SMike Smith mtx_enter(&list->el_mutex, MTX_DEF); 127fcb893a8SMike Smith if (ep != NULL) { 128fcb893a8SMike Smith /* remove just this entry */ 129fcb893a8SMike Smith TAILQ_REMOVE(&list->el_entries, ep, ee_link); 130fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 131fcb893a8SMike Smith } else { 132fcb893a8SMike Smith /* remove entire list */ 133fcb893a8SMike Smith while (!TAILQ_EMPTY(&list->el_entries)) { 134fcb893a8SMike Smith ep = TAILQ_FIRST(&list->el_entries); 1351b727751SPoul-Henning Kamp TAILQ_REMOVE(&list->el_entries, ep, ee_link); 136fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 137fcb893a8SMike Smith } 138fcb893a8SMike Smith } 1396595c331SMike Smith mtx_exit(&list->el_mutex, MTX_DEF); 140fcb893a8SMike Smith } 141fcb893a8SMike Smith 142fcb893a8SMike Smith struct eventhandler_list * 143fcb893a8SMike Smith eventhandler_find_list(char *name) 144fcb893a8SMike Smith { 145fcb893a8SMike Smith struct eventhandler_list *list; 146fcb893a8SMike Smith 147fcb893a8SMike Smith /* scan looking for the requested list */ 1486595c331SMike Smith mtx_enter(&eventhandler_mutex, MTX_DEF); 149fcb893a8SMike Smith for (list = TAILQ_FIRST(&eventhandler_lists); 150fcb893a8SMike Smith list != NULL; 151fcb893a8SMike Smith list = TAILQ_NEXT(list, el_link)) { 152fcb893a8SMike Smith if (!strcmp(name, list->el_name)) 153fcb893a8SMike Smith break; 154fcb893a8SMike Smith } 1556595c331SMike Smith mtx_exit(&eventhandler_mutex, MTX_DEF); 1566595c331SMike Smith 157fcb893a8SMike Smith return(list); 158fcb893a8SMike Smith } 1596595c331SMike Smith 160