2007-12-05 10:06:50 +03:00
|
|
|
/* $NetBSD: subr_callback.c,v 1.6 2007/12/05 07:06:54 ad Exp $ */
|
2006-05-25 18:27:28 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c)2006 YAMAMOTO Takashi,
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2007-12-05 10:06:50 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_callback.c,v 1.6 2007/12/05 07:06:54 ad Exp $");
|
2006-05-25 18:27:28 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/callback.h>
|
|
|
|
|
|
|
|
void
|
2007-07-10 00:51:58 +04:00
|
|
|
callback_head_init(struct callback_head *ch, int ipl)
|
2006-05-25 18:27:28 +04:00
|
|
|
{
|
|
|
|
|
2006-06-21 21:16:00 +04:00
|
|
|
memset(ch, 0, sizeof(struct callback_head));
|
2007-12-05 10:06:50 +03:00
|
|
|
mutex_init(&ch->ch_lock, MUTEX_DEFAULT, ipl);
|
2007-07-10 00:51:58 +04:00
|
|
|
cv_init(&ch->ch_cv, "callback");
|
2006-05-25 18:27:28 +04:00
|
|
|
TAILQ_INIT(&ch->ch_q);
|
2006-06-23 20:17:23 +04:00
|
|
|
#if 0 /* already zero-initialized */
|
|
|
|
ch->ch_next = NULL;
|
|
|
|
ch->ch_nentries = 0;
|
|
|
|
ch->ch_running = 0;
|
|
|
|
ch->ch_flags = 0;
|
|
|
|
#endif
|
2006-05-25 18:27:28 +04:00
|
|
|
}
|
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
void
|
|
|
|
callback_head_destroy(struct callback_head *ch)
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex_destroy(&ch->ch_lock);
|
|
|
|
cv_destroy(&ch->ch_cv);
|
|
|
|
}
|
|
|
|
|
2006-05-25 18:27:28 +04:00
|
|
|
void
|
|
|
|
callback_register(struct callback_head *ch, struct callback_entry *ce,
|
|
|
|
void *obj, int (*fn)(struct callback_entry *, void *, void *))
|
|
|
|
{
|
|
|
|
|
|
|
|
ce->ce_func = fn;
|
|
|
|
ce->ce_obj = obj;
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_enter(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
TAILQ_INSERT_TAIL(&ch->ch_q, ce, ce_q);
|
|
|
|
ch->ch_nentries++;
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_exit(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
callback_unregister(struct callback_head *ch, struct callback_entry *ce)
|
|
|
|
{
|
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_enter(&ch->ch_lock);
|
|
|
|
while (ch->ch_running > 0)
|
|
|
|
cv_wait(&ch->ch_cv, &ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
if (__predict_false(ch->ch_next == ce)) {
|
|
|
|
ch->ch_next = TAILQ_NEXT(ce, ce_q);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&ch->ch_q, ce, ce_q);
|
|
|
|
ch->ch_nentries--;
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_exit(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
callback_runone(struct callback_head *ch, void *arg)
|
|
|
|
{
|
|
|
|
struct callback_entry *ce;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
KASSERT(ch->ch_nentries > 0);
|
|
|
|
KASSERT(ch->ch_running > 0);
|
|
|
|
|
|
|
|
ce = ch->ch_next;
|
|
|
|
if (ce == NULL) {
|
|
|
|
ce = TAILQ_FIRST(&ch->ch_q);
|
|
|
|
}
|
|
|
|
KASSERT(ce != NULL);
|
|
|
|
result = (*ce->ce_func)(ce, ce->ce_obj, arg);
|
|
|
|
ch->ch_next = TAILQ_NEXT(ce, ce_q);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
callback_run_enter(struct callback_head *ch)
|
|
|
|
{
|
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_enter(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
ch->ch_running++;
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_exit(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
callback_run_leave(struct callback_head *ch)
|
|
|
|
{
|
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
mutex_enter(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
KASSERT(ch->ch_running > 0);
|
|
|
|
ch->ch_running--;
|
2007-07-10 00:51:58 +04:00
|
|
|
if (ch->ch_running == 0)
|
|
|
|
cv_broadcast(&ch->ch_cv);
|
|
|
|
mutex_exit(&ch->ch_lock);
|
2006-05-25 18:27:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
callback_run_roundrobin(struct callback_head *ch, void *arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
callback_run_enter(ch);
|
|
|
|
n = ch->ch_nentries;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
result = callback_runone(ch, arg);
|
|
|
|
if (result != CALLBACK_CHAIN_CONTINUE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback_run_leave(ch);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|