1999-08-21 06:24:40 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1999-08-21 06:24:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/eventhandler.h>
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
|
|
|
|
|
|
|
|
/* List of 'slow' lists */
|
2000-05-26 02:09:24 +00:00
|
|
|
static TAILQ_HEAD(, eventhandler_list) eventhandler_lists;
|
1999-08-21 06:24:40 +00:00
|
|
|
static int eventhandler_lists_initted = 0;
|
2000-10-25 00:01:39 +00:00
|
|
|
static struct mtx eventhandler_mutex;
|
1999-08-21 06:24:40 +00:00
|
|
|
|
|
|
|
struct eventhandler_entry_generic
|
|
|
|
{
|
|
|
|
struct eventhandler_entry ee;
|
|
|
|
void (* func)(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insertion is O(n) due to the priority scan, but optimises to O(1)
|
|
|
|
* if all priorities are identical.
|
|
|
|
*/
|
|
|
|
eventhandler_tag
|
|
|
|
eventhandler_register(struct eventhandler_list *list, char *name,
|
|
|
|
void *func, void *arg, int priority)
|
|
|
|
{
|
|
|
|
struct eventhandler_entry_generic *eg;
|
|
|
|
struct eventhandler_entry *ep;
|
|
|
|
|
|
|
|
/* avoid the need for a SYSINIT just to init the list */
|
|
|
|
if (!eventhandler_lists_initted) {
|
|
|
|
TAILQ_INIT(&eventhandler_lists);
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
eventhandler_lists_initted = 1;
|
|
|
|
}
|
|
|
|
|
2000-09-22 03:17:35 +00:00
|
|
|
/* lock the eventhandler lists */
|
|
|
|
mtx_enter(&eventhandler_mutex, MTX_DEF);
|
|
|
|
|
1999-08-21 06:24:40 +00:00
|
|
|
/* Do we need to find/create the (slow) list? */
|
|
|
|
if (list == NULL) {
|
|
|
|
/* look for a matching, existing list */
|
|
|
|
list = eventhandler_find_list(name);
|
|
|
|
|
|
|
|
/* Do we need to create the list? */
|
|
|
|
if (list == NULL) {
|
|
|
|
if ((list = malloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
|
2000-09-22 03:17:35 +00:00
|
|
|
M_EVENTHANDLER, M_NOWAIT)) == NULL) {
|
|
|
|
mtx_exit(&eventhandler_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
return(NULL);
|
2000-09-22 03:17:35 +00:00
|
|
|
}
|
1999-08-21 06:24:40 +00:00
|
|
|
list->el_flags = 0;
|
|
|
|
list->el_name = (char *)list + sizeof(struct eventhandler_list);
|
|
|
|
strcpy(list->el_name, name);
|
|
|
|
TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(list->el_flags & EHE_INITTED)) {
|
|
|
|
TAILQ_INIT(&list->el_entries);
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_init(&list->el_mutex, name, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
list->el_flags = EHE_INITTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate an entry for this handler, populate it */
|
|
|
|
if ((eg = malloc(sizeof(struct eventhandler_entry_generic),
|
2000-09-22 03:17:35 +00:00
|
|
|
M_EVENTHANDLER, M_NOWAIT)) == NULL) {
|
|
|
|
mtx_exit(&eventhandler_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
return(NULL);
|
2000-09-22 03:17:35 +00:00
|
|
|
}
|
1999-08-21 06:24:40 +00:00
|
|
|
eg->func = func;
|
|
|
|
eg->ee.ee_arg = arg;
|
|
|
|
eg->ee.ee_priority = priority;
|
|
|
|
|
|
|
|
/* sort it into the list */
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_enter(&list->el_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
for (ep = TAILQ_FIRST(&list->el_entries);
|
|
|
|
ep != NULL;
|
|
|
|
ep = TAILQ_NEXT(ep, ee_link)) {
|
|
|
|
if (eg->ee.ee_priority < ep->ee_priority) {
|
|
|
|
TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ep == NULL)
|
|
|
|
TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_exit(&list->el_mutex, MTX_DEF);
|
|
|
|
mtx_exit(&eventhandler_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
return(&eg->ee);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
|
|
|
|
{
|
|
|
|
struct eventhandler_entry *ep = tag;
|
|
|
|
|
|
|
|
/* XXX insert diagnostic check here? */
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_enter(&list->el_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
if (ep != NULL) {
|
|
|
|
/* remove just this entry */
|
|
|
|
TAILQ_REMOVE(&list->el_entries, ep, ee_link);
|
|
|
|
free(ep, M_EVENTHANDLER);
|
|
|
|
} else {
|
|
|
|
/* remove entire list */
|
|
|
|
while (!TAILQ_EMPTY(&list->el_entries)) {
|
|
|
|
ep = TAILQ_FIRST(&list->el_entries);
|
1999-11-16 16:28:58 +00:00
|
|
|
TAILQ_REMOVE(&list->el_entries, ep, ee_link);
|
1999-08-21 06:24:40 +00:00
|
|
|
free(ep, M_EVENTHANDLER);
|
|
|
|
}
|
|
|
|
}
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_exit(&list->el_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct eventhandler_list *
|
|
|
|
eventhandler_find_list(char *name)
|
|
|
|
{
|
|
|
|
struct eventhandler_list *list;
|
2000-09-22 03:17:35 +00:00
|
|
|
|
1999-08-21 06:24:40 +00:00
|
|
|
/* scan looking for the requested list */
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_enter(&eventhandler_mutex, MTX_DEF);
|
1999-08-21 06:24:40 +00:00
|
|
|
for (list = TAILQ_FIRST(&eventhandler_lists);
|
|
|
|
list != NULL;
|
|
|
|
list = TAILQ_NEXT(list, el_link)) {
|
|
|
|
if (!strcmp(name, list->el_name))
|
|
|
|
break;
|
|
|
|
}
|
2000-09-22 03:17:35 +00:00
|
|
|
mtx_exit(&eventhandler_mutex, MTX_DEF);
|
|
|
|
|
1999-08-21 06:24:40 +00:00
|
|
|
return(list);
|
|
|
|
}
|
2000-09-22 03:17:35 +00:00
|
|
|
|