9ed346bab0
mtx_enter(lock, type) becomes: mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks) mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized) similarily, for releasing a lock, we now have: mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN. We change the caller interface for the two different types of locks because the semantics are entirely different for each case, and this makes it explicitly clear and, at the same time, it rids us of the extra `type' argument. The enter->lock and exit->unlock change has been made with the idea that we're "locking data" and not "entering locked code" in mind. Further, remove all additional "flags" previously passed to the lock acquire/release routines with the exception of two: MTX_QUIET and MTX_NOSWITCH The functionality of these flags is preserved and they can be passed to the lock/unlock routines by calling the corresponding wrappers: mtx_{lock, unlock}_flags(lock, flag(s)) and mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN locks, respectively. Re-inline some lock acq/rel code; in the sleep lock case, we only inline the _obtain_lock()s in order to ensure that the inlined code fits into a cache line. In the spin lock case, we inline recursion and actually only perform a function call if we need to spin. This change has been made with the idea that we generally tend to avoid spin locks and that also the spin locks that we do have and are heavily used (i.e. sched_lock) do recurse, and therefore in an effort to reduce function call overhead for some architectures (such as alpha), we inline recursion for this case. Create a new malloc type for the witness code and retire from using the M_DEV type. The new type is called M_WITNESS and is only declared if WITNESS is enabled. Begin cleaning up some machdep/mutex.h code - specifically updated the "optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently need those. Finally, caught up to the interface changes in all sys code. Contributors: jake, jhb, jasone (in no particular order)
169 lines
5.2 KiB
C
169 lines
5.2 KiB
C
/*-
|
|
* 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.
|
|
*
|
|
* $FreeBSD$
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/kernel.h>
|
|
#include <sys/malloc.h>
|
|
#include <sys/mutex.h>
|
|
#include <sys/systm.h>
|
|
#include <sys/eventhandler.h>
|
|
|
|
static MALLOC_DEFINE(M_EVENTHANDLER, "eventhandler", "Event handler records");
|
|
|
|
/* List of 'slow' lists */
|
|
static TAILQ_HEAD(, eventhandler_list) eventhandler_lists;
|
|
static int eventhandler_lists_initted = 0;
|
|
static struct mtx eventhandler_mutex;
|
|
|
|
struct eventhandler_entry_generic
|
|
{
|
|
struct eventhandler_entry ee;
|
|
void (* func)(void);
|
|
};
|
|
|
|
/*
|
|
* Initialize the eventhandler mutex and list.
|
|
*/
|
|
static void
|
|
eventhandler_init(void *dummy __unused)
|
|
{
|
|
TAILQ_INIT(&eventhandler_lists);
|
|
mtx_init(&eventhandler_mutex, "eventhandler", MTX_DEF | MTX_RECURSE);
|
|
eventhandler_lists_initted = 1;
|
|
}
|
|
SYSINIT(eventhandlers, SI_SUB_EVENTHANDLER, SI_ORDER_FIRST, eventhandler_init,
|
|
NULL)
|
|
|
|
/*
|
|
* 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;
|
|
|
|
KASSERT(eventhandler_lists_initted, ("eventhandler registered too early"));
|
|
|
|
/* lock the eventhandler lists */
|
|
mtx_lock(&eventhandler_mutex);
|
|
|
|
/* 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,
|
|
M_EVENTHANDLER, M_NOWAIT)) == NULL) {
|
|
mtx_unlock(&eventhandler_mutex);
|
|
return(NULL);
|
|
}
|
|
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);
|
|
lockinit(&list->el_lock, PZERO, name, 0, 0);
|
|
list->el_flags = EHE_INITTED;
|
|
}
|
|
|
|
/* allocate an entry for this handler, populate it */
|
|
if ((eg = malloc(sizeof(struct eventhandler_entry_generic),
|
|
M_EVENTHANDLER, M_NOWAIT)) == NULL) {
|
|
mtx_unlock(&eventhandler_mutex);
|
|
return(NULL);
|
|
}
|
|
eg->func = func;
|
|
eg->ee.ee_arg = arg;
|
|
eg->ee.ee_priority = priority;
|
|
|
|
/* sort it into the list */
|
|
lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC);
|
|
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);
|
|
lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC);
|
|
mtx_unlock(&eventhandler_mutex);
|
|
return(&eg->ee);
|
|
}
|
|
|
|
void
|
|
eventhandler_deregister(struct eventhandler_list *list, eventhandler_tag tag)
|
|
{
|
|
struct eventhandler_entry *ep = tag;
|
|
|
|
/* XXX insert diagnostic check here? */
|
|
lockmgr(&list->el_lock, LK_EXCLUSIVE, NULL, CURPROC);
|
|
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);
|
|
TAILQ_REMOVE(&list->el_entries, ep, ee_link);
|
|
free(ep, M_EVENTHANDLER);
|
|
}
|
|
}
|
|
lockmgr(&list->el_lock, LK_RELEASE, NULL, CURPROC);
|
|
}
|
|
|
|
struct eventhandler_list *
|
|
eventhandler_find_list(char *name)
|
|
{
|
|
struct eventhandler_list *list;
|
|
|
|
/* scan looking for the requested list */
|
|
mtx_lock(&eventhandler_mutex);
|
|
for (list = TAILQ_FIRST(&eventhandler_lists);
|
|
list != NULL;
|
|
list = TAILQ_NEXT(list, el_link)) {
|
|
if (!strcmp(name, list->el_name))
|
|
break;
|
|
}
|
|
mtx_unlock(&eventhandler_mutex);
|
|
|
|
return(list);
|
|
}
|
|
|