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> 31f34fa851SJohn Baldwin #include <sys/lock.h> 32fcb893a8SMike Smith #include <sys/malloc.h> 3306592dd1SJohn Baldwin #include <sys/mutex.h> 34fcb893a8SMike Smith #include <sys/systm.h> 35fcb893a8SMike Smith #include <sys/eventhandler.h> 36fcb893a8SMike Smith 37959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records"); 38fcb893a8SMike Smith 39fcb893a8SMike Smith /* List of 'slow' lists */ 40e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list) eventhandler_lists; 41fcb893a8SMike Smith static int eventhandler_lists_initted = 0; 42d543796fSJohn Baldwin static struct mtx eventhandler_mutex; 43fcb893a8SMike Smith 44fcb893a8SMike Smith struct eventhandler_entry_generic 45fcb893a8SMike Smith { 46fcb893a8SMike Smith struct eventhandler_entry ee; 47fcb893a8SMike Smith void (* func)(void); 48fcb893a8SMike Smith }; 49fcb893a8SMike Smith 50fcb893a8SMike Smith /* 5106592dd1SJohn Baldwin * Initialize the eventhandler mutex and list. 5206592dd1SJohn Baldwin */ 5306592dd1SJohn Baldwin static void 5406592dd1SJohn Baldwin eventhandler_init(void *dummy __unused) 5506592dd1SJohn Baldwin { 5606592dd1SJohn Baldwin TAILQ_INIT(&eventhandler_lists); 5708812b39SBosko Milekic mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF | MTX_RECURSE); 5806592dd1SJohn Baldwin eventhandler_lists_initted = 1; 5906592dd1SJohn Baldwin } 6006592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init, 6106592dd1SJohn Baldwin NULL) 6206592dd1SJohn Baldwin 6306592dd1SJohn Baldwin /* 64fcb893a8SMike Smith * Insertion is O(n) due to the priority scan, but optimises to O(1) 65fcb893a8SMike Smith * if all priorities are identical. 66fcb893a8SMike Smith */ 67fcb893a8SMike Smith eventhandler_tag 68fcb893a8SMike Smith eventhandler_register(struct eventhandler_list *list, char *name, 69fcb893a8SMike Smith void *func, void *arg, int priority) 70fcb893a8SMike Smith { 71fcb893a8SMike Smith struct eventhandler_entry_generic *eg; 72fcb893a8SMike Smith struct eventhandler_entry *ep; 73fcb893a8SMike Smith 7406592dd1SJohn Baldwin KASSERT(eventhandler_lists_initted, ("eventhandler registered too early")); 75fcb893a8SMike Smith 766595c331SMike Smith /* lock the eventhandler lists */ 779ed346baSBosko Milekic mtx_lock(&eventhandler_mutex); 786595c331SMike Smith 79fcb893a8SMike Smith /* Do we need to find/create the (slow) list? */ 80fcb893a8SMike Smith if (list == NULL) { 81fcb893a8SMike Smith /* look for a matching, existing list */ 82fcb893a8SMike Smith list = eventhandler_find_list(name); 83fcb893a8SMike Smith 84fcb893a8SMike Smith /* Do we need to create the list? */ 85fcb893a8SMike Smith if (list == NULL) { 86fcb893a8SMike Smith if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1, 876595c331SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) { 889ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 89fcb893a8SMike Smith return(NULL); 906595c331SMike Smith } 91fcb893a8SMike Smith list->el_flags = 0; 92fcb893a8SMike Smith list->el_name = (char *)list + sizeof(struct eventhandler_list); 93fcb893a8SMike Smith strcpy(list->el_name, name); 94fcb893a8SMike Smith TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link); 95fcb893a8SMike Smith } 96fcb893a8SMike Smith } 97fcb893a8SMike Smith if (!(list->el_flags & EHE_INITTED)) { 98fcb893a8SMike Smith TAILQ_INIT(&list->el_entries); 9906592dd1SJohn Baldwin lockinit(&list->el_lock, PZERO, name, 0, 0); 100fcb893a8SMike Smith list->el_flags = EHE_INITTED; 101fcb893a8SMike Smith } 102fcb893a8SMike Smith 103fcb893a8SMike Smith /* allocate an entry for this handler, populate it */ 104fcb893a8SMike Smith if ((eg = malloc(sizeof(struct eventhandler_entry_generic), 1056595c331SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) { 1069ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 107fcb893a8SMike Smith return(NULL); 1086595c331SMike Smith } 109fcb893a8SMike Smith eg->func = func; 110fcb893a8SMike Smith eg->ee.ee_arg = arg; 111fcb893a8SMike Smith eg->ee.ee_priority = priority; 112fcb893a8SMike Smith 113fcb893a8SMike Smith /* sort it into the list */ 11406592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC); 115fcb893a8SMike Smith for (ep = TAILQ_FIRST(&list->el_entries); 116fcb893a8SMike Smith ep != NULL; 117fcb893a8SMike Smith ep = TAILQ_NEXT(ep, ee_link)) { 118fcb893a8SMike Smith if (eg->ee.ee_priority < ep->ee_priority) { 119fcb893a8SMike Smith TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link); 120fcb893a8SMike Smith break; 121fcb893a8SMike Smith } 122fcb893a8SMike Smith } 123fcb893a8SMike Smith if (ep == NULL) 124fcb893a8SMike Smith TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link); 12506592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC); 1269ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 127fcb893a8SMike Smith return(&eg->ee); 128fcb893a8SMike Smith } 129fcb893a8SMike Smith 130fcb893a8SMike Smith void 131fcb893a8SMike Smith eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) 132fcb893a8SMike Smith { 133fcb893a8SMike Smith struct eventhandler_entry *ep = tag; 134fcb893a8SMike Smith 135fcb893a8SMike Smith /* XXX insert diagnostic check here? */ 13606592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC); 137fcb893a8SMike Smith if (ep != NULL) { 138fcb893a8SMike Smith /* remove just this entry */ 139fcb893a8SMike Smith TAILQ_REMOVE(&list->el_entries, ep, ee_link); 140fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 141fcb893a8SMike Smith } else { 142fcb893a8SMike Smith /* remove entire list */ 143fcb893a8SMike Smith while (!TAILQ_EMPTY(&list->el_entries)) { 144fcb893a8SMike Smith ep = TAILQ_FIRST(&list->el_entries); 1451b727751SPoul-Henning Kamp TAILQ_REMOVE(&list->el_entries, ep, ee_link); 146fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 147fcb893a8SMike Smith } 148fcb893a8SMike Smith } 14906592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC); 150fcb893a8SMike Smith } 151fcb893a8SMike Smith 152fcb893a8SMike Smith struct eventhandler_list * 153fcb893a8SMike Smith eventhandler_find_list(char *name) 154fcb893a8SMike Smith { 155fcb893a8SMike Smith struct eventhandler_list *list; 156fcb893a8SMike Smith 157fcb893a8SMike Smith /* scan looking for the requested list */ 1589ed346baSBosko Milekic mtx_lock(&eventhandler_mutex); 159fcb893a8SMike Smith for (list = TAILQ_FIRST(&eventhandler_lists); 160fcb893a8SMike Smith list != NULL; 161fcb893a8SMike Smith list = TAILQ_NEXT(list, el_link)) { 162fcb893a8SMike Smith if (!strcmp(name, list->el_name)) 163fcb893a8SMike Smith break; 164fcb893a8SMike Smith } 1659ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 1666595c331SMike Smith 167fcb893a8SMike Smith return(list); 168fcb893a8SMike Smith } 1696595c331SMike Smith 170