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> 3206592dd1SJohn Baldwin #include <sys/mutex.h> 33fcb893a8SMike Smith #include <sys/systm.h> 34fcb893a8SMike Smith #include <sys/eventhandler.h> 35fcb893a8SMike Smith 36959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records"); 37fcb893a8SMike Smith 38fcb893a8SMike Smith /* List of 'slow' lists */ 39e3975643SJake Burkholder static TAILQ_HEAD(, eventhandler_list) eventhandler_lists; 40fcb893a8SMike Smith static int eventhandler_lists_initted = 0; 41d543796fSJohn Baldwin static struct mtx eventhandler_mutex; 42fcb893a8SMike Smith 43fcb893a8SMike Smith struct eventhandler_entry_generic 44fcb893a8SMike Smith { 45fcb893a8SMike Smith struct eventhandler_entry ee; 46fcb893a8SMike Smith void (* func)(void); 47fcb893a8SMike Smith }; 48fcb893a8SMike Smith 49fcb893a8SMike Smith /* 5006592dd1SJohn Baldwin * Initialize the eventhandler mutex and list. 5106592dd1SJohn Baldwin */ 5206592dd1SJohn Baldwin static void 5306592dd1SJohn Baldwin eventhandler_init(void *dummy __unused) 5406592dd1SJohn Baldwin { 5506592dd1SJohn Baldwin TAILQ_INIT(&eventhandler_lists); 5608812b39SBosko Milekic mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF | MTX_RECURSE); 5706592dd1SJohn Baldwin eventhandler_lists_initted = 1; 5806592dd1SJohn Baldwin } 5906592dd1SJohn Baldwin SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init, 6006592dd1SJohn Baldwin NULL) 6106592dd1SJohn Baldwin 6206592dd1SJohn Baldwin /* 63fcb893a8SMike Smith * Insertion is O(n) due to the priority scan, but optimises to O(1) 64fcb893a8SMike Smith * if all priorities are identical. 65fcb893a8SMike Smith */ 66fcb893a8SMike Smith eventhandler_tag 67fcb893a8SMike Smith eventhandler_register(struct eventhandler_list *list, char *name, 68fcb893a8SMike Smith void *func, void *arg, int priority) 69fcb893a8SMike Smith { 70fcb893a8SMike Smith struct eventhandler_entry_generic *eg; 71fcb893a8SMike Smith struct eventhandler_entry *ep; 72fcb893a8SMike Smith 7306592dd1SJohn Baldwin KASSERT(eventhandler_lists_initted, ("eventhandler registered too early")); 74fcb893a8SMike Smith 756595c331SMike Smith /* lock the eventhandler lists */ 769ed346baSBosko Milekic mtx_lock(&eventhandler_mutex); 776595c331SMike Smith 78fcb893a8SMike Smith /* Do we need to find/create the (slow) list? */ 79fcb893a8SMike Smith if (list == NULL) { 80fcb893a8SMike Smith /* look for a matching, existing list */ 81fcb893a8SMike Smith list = eventhandler_find_list(name); 82fcb893a8SMike Smith 83fcb893a8SMike Smith /* Do we need to create the list? */ 84fcb893a8SMike Smith if (list == NULL) { 85fcb893a8SMike Smith if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1, 866595c331SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) { 879ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 88fcb893a8SMike Smith return(NULL); 896595c331SMike Smith } 90fcb893a8SMike Smith list->el_flags = 0; 91fcb893a8SMike Smith list->el_name = (char *)list + sizeof(struct eventhandler_list); 92fcb893a8SMike Smith strcpy(list->el_name, name); 93fcb893a8SMike Smith TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link); 94fcb893a8SMike Smith } 95fcb893a8SMike Smith } 96fcb893a8SMike Smith if (!(list->el_flags & EHE_INITTED)) { 97fcb893a8SMike Smith TAILQ_INIT(&list->el_entries); 9806592dd1SJohn Baldwin lockinit(&list->el_lock, PZERO, name, 0, 0); 99fcb893a8SMike Smith list->el_flags = EHE_INITTED; 100fcb893a8SMike Smith } 101fcb893a8SMike Smith 102fcb893a8SMike Smith /* allocate an entry for this handler, populate it */ 103fcb893a8SMike Smith if ((eg = malloc(sizeof(struct eventhandler_entry_generic), 1046595c331SMike Smith M_EVENTHANDLER, M_NOWAIT)) == NULL) { 1059ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 106fcb893a8SMike Smith return(NULL); 1076595c331SMike Smith } 108fcb893a8SMike Smith eg->func = func; 109fcb893a8SMike Smith eg->ee.ee_arg = arg; 110fcb893a8SMike Smith eg->ee.ee_priority = priority; 111fcb893a8SMike Smith 112fcb893a8SMike Smith /* sort it into the list */ 11306592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC); 114fcb893a8SMike Smith for (ep = TAILQ_FIRST(&list->el_entries); 115fcb893a8SMike Smith ep != NULL; 116fcb893a8SMike Smith ep = TAILQ_NEXT(ep, ee_link)) { 117fcb893a8SMike Smith if (eg->ee.ee_priority < ep->ee_priority) { 118fcb893a8SMike Smith TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link); 119fcb893a8SMike Smith break; 120fcb893a8SMike Smith } 121fcb893a8SMike Smith } 122fcb893a8SMike Smith if (ep == NULL) 123fcb893a8SMike Smith TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link); 12406592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC); 1259ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 126fcb893a8SMike Smith return(&eg->ee); 127fcb893a8SMike Smith } 128fcb893a8SMike Smith 129fcb893a8SMike Smith void 130fcb893a8SMike Smith eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag) 131fcb893a8SMike Smith { 132fcb893a8SMike Smith struct eventhandler_entry *ep = tag; 133fcb893a8SMike Smith 134fcb893a8SMike Smith /* XXX insert diagnostic check here? */ 13506592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC); 136fcb893a8SMike Smith if (ep != NULL) { 137fcb893a8SMike Smith /* remove just this entry */ 138fcb893a8SMike Smith TAILQ_REMOVE(&list->el_entries, ep, ee_link); 139fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 140fcb893a8SMike Smith } else { 141fcb893a8SMike Smith /* remove entire list */ 142fcb893a8SMike Smith while (!TAILQ_EMPTY(&list->el_entries)) { 143fcb893a8SMike Smith ep = TAILQ_FIRST(&list->el_entries); 1441b727751SPoul-Henning Kamp TAILQ_REMOVE(&list->el_entries, ep, ee_link); 145fcb893a8SMike Smith free(ep, M_EVENTHANDLER); 146fcb893a8SMike Smith } 147fcb893a8SMike Smith } 14806592dd1SJohn Baldwin lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC); 149fcb893a8SMike Smith } 150fcb893a8SMike Smith 151fcb893a8SMike Smith struct eventhandler_list * 152fcb893a8SMike Smith eventhandler_find_list(char *name) 153fcb893a8SMike Smith { 154fcb893a8SMike Smith struct eventhandler_list *list; 155fcb893a8SMike Smith 156fcb893a8SMike Smith /* scan looking for the requested list */ 1579ed346baSBosko Milekic mtx_lock(&eventhandler_mutex); 158fcb893a8SMike Smith for (list = TAILQ_FIRST(&eventhandler_lists); 159fcb893a8SMike Smith list != NULL; 160fcb893a8SMike Smith list = TAILQ_NEXT(list, el_link)) { 161fcb893a8SMike Smith if (!strcmp(name, list->el_name)) 162fcb893a8SMike Smith break; 163fcb893a8SMike Smith } 1649ed346baSBosko Milekic mtx_unlock(&eventhandler_mutex); 1656595c331SMike Smith 166fcb893a8SMike Smith return(list); 167fcb893a8SMike Smith } 1686595c331SMike Smith 169