Home | History | Annotate | Line # | Download | only in kern
subr_callback.c revision 1.1
      1 /*	$NetBSD: subr_callback.c,v 1.1 2006/05/25 14:27:28 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2006 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: subr_callback.c,v 1.1 2006/05/25 14:27:28 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/proc.h>
     35 #include <sys/callback.h>
     36 
     37 #define	CH_WANT	1
     38 
     39 void
     40 callback_head_init(struct callback_head *ch)
     41 {
     42 
     43 	simple_lock_init(&ch->ch_lock);
     44 	TAILQ_INIT(&ch->ch_q);
     45 	ch->ch_next = NULL;
     46 	ch->ch_nentries = 0;
     47 }
     48 
     49 void
     50 callback_register(struct callback_head *ch, struct callback_entry *ce,
     51     void *obj, int (*fn)(struct callback_entry *, void *, void *))
     52 {
     53 
     54 	ce->ce_func = fn;
     55 	ce->ce_obj = obj;
     56 	simple_lock(&ch->ch_lock);
     57 	TAILQ_INSERT_TAIL(&ch->ch_q, ce, ce_q);
     58 	ch->ch_nentries++;
     59 	simple_unlock(&ch->ch_lock);
     60 }
     61 
     62 void
     63 callback_unregister(struct callback_head *ch, struct callback_entry *ce)
     64 {
     65 
     66 	simple_lock(&ch->ch_lock);
     67 	while (ch->ch_running > 0) {
     68 		ch->ch_flags |= CH_WANT;
     69 		ltsleep(&ch->ch_running, PVM, "recunreg", 0, &ch->ch_lock);
     70 	}
     71 	if (__predict_false(ch->ch_next == ce)) {
     72 		ch->ch_next = TAILQ_NEXT(ce, ce_q);
     73 	}
     74 	TAILQ_REMOVE(&ch->ch_q, ce, ce_q);
     75 	ch->ch_nentries--;
     76 	simple_unlock(&ch->ch_lock);
     77 }
     78 
     79 static int
     80 callback_runone(struct callback_head *ch, void *arg)
     81 {
     82 	struct callback_entry *ce;
     83 	int result;
     84 
     85 	KASSERT(ch->ch_nentries > 0);
     86 	KASSERT(ch->ch_running > 0);
     87 
     88 	ce = ch->ch_next;
     89 	if (ce == NULL) {
     90 		ce = TAILQ_FIRST(&ch->ch_q);
     91 	}
     92 	KASSERT(ce != NULL);
     93 	result = (*ce->ce_func)(ce, ce->ce_obj, arg);
     94 	ch->ch_next = TAILQ_NEXT(ce, ce_q);
     95 	return result;
     96 }
     97 
     98 static void
     99 callback_run_enter(struct callback_head *ch)
    100 {
    101 
    102 	simple_lock(&ch->ch_lock);
    103 	ch->ch_running++;
    104 	simple_unlock(&ch->ch_lock);
    105 }
    106 
    107 static void
    108 callback_run_leave(struct callback_head *ch)
    109 {
    110 
    111 	simple_lock(&ch->ch_lock);
    112 	KASSERT(ch->ch_running > 0);
    113 	ch->ch_running--;
    114 	if (ch->ch_running == 0 && (ch->ch_flags & CH_WANT) != 0) {
    115 		ch->ch_flags &= ~CH_WANT;
    116 		wakeup(&ch->ch_running);
    117 	}
    118 	simple_unlock(&ch->ch_lock);
    119 }
    120 
    121 int
    122 callback_run_roundrobin(struct callback_head *ch, void *arg)
    123 {
    124 	int i;
    125 	int n;
    126 	int result = 0;
    127 
    128 	callback_run_enter(ch);
    129 	n = ch->ch_nentries;
    130 	for (i = 0; i < n; i++) {
    131 		result = callback_runone(ch, arg);
    132 		if (result != CALLBACK_CHAIN_CONTINUE) {
    133 			break;
    134 		}
    135 	}
    136 	callback_run_leave(ch);
    137 
    138 	return result;
    139 }
    140