1 /* $NetBSD: subr_callback.c,v 1.4 2006/06/23 16:17:23 drochner 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.4 2006/06/23 16:17:23 drochner 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 memset(ch, 0, sizeof(struct callback_head)); 44 simple_lock_init(&ch->ch_lock); 45 TAILQ_INIT(&ch->ch_q); 46 #if 0 /* already zero-initialized */ 47 ch->ch_next = NULL; 48 ch->ch_nentries = 0; 49 ch->ch_running = 0; 50 ch->ch_flags = 0; 51 #endif 52 } 53 54 void 55 callback_register(struct callback_head *ch, struct callback_entry *ce, 56 void *obj, int (*fn)(struct callback_entry *, void *, void *)) 57 { 58 59 ce->ce_func = fn; 60 ce->ce_obj = obj; 61 simple_lock(&ch->ch_lock); 62 TAILQ_INSERT_TAIL(&ch->ch_q, ce, ce_q); 63 ch->ch_nentries++; 64 simple_unlock(&ch->ch_lock); 65 } 66 67 void 68 callback_unregister(struct callback_head *ch, struct callback_entry *ce) 69 { 70 71 simple_lock(&ch->ch_lock); 72 while (ch->ch_running > 0) { 73 ch->ch_flags |= CH_WANT; 74 ltsleep(&ch->ch_running, PVM, "recunreg", 0, &ch->ch_lock); 75 } 76 if (__predict_false(ch->ch_next == ce)) { 77 ch->ch_next = TAILQ_NEXT(ce, ce_q); 78 } 79 TAILQ_REMOVE(&ch->ch_q, ce, ce_q); 80 ch->ch_nentries--; 81 simple_unlock(&ch->ch_lock); 82 } 83 84 static int 85 callback_runone(struct callback_head *ch, void *arg) 86 { 87 struct callback_entry *ce; 88 int result; 89 90 KASSERT(ch->ch_nentries > 0); 91 KASSERT(ch->ch_running > 0); 92 93 ce = ch->ch_next; 94 if (ce == NULL) { 95 ce = TAILQ_FIRST(&ch->ch_q); 96 } 97 KASSERT(ce != NULL); 98 result = (*ce->ce_func)(ce, ce->ce_obj, arg); 99 ch->ch_next = TAILQ_NEXT(ce, ce_q); 100 return result; 101 } 102 103 static void 104 callback_run_enter(struct callback_head *ch) 105 { 106 107 simple_lock(&ch->ch_lock); 108 ch->ch_running++; 109 simple_unlock(&ch->ch_lock); 110 } 111 112 static void 113 callback_run_leave(struct callback_head *ch) 114 { 115 116 simple_lock(&ch->ch_lock); 117 KASSERT(ch->ch_running > 0); 118 ch->ch_running--; 119 if (ch->ch_running == 0 && (ch->ch_flags & CH_WANT) != 0) { 120 ch->ch_flags &= ~CH_WANT; 121 wakeup(&ch->ch_running); 122 } 123 simple_unlock(&ch->ch_lock); 124 } 125 126 int 127 callback_run_roundrobin(struct callback_head *ch, void *arg) 128 { 129 int i; 130 int n; 131 int result = 0; 132 133 callback_run_enter(ch); 134 n = ch->ch_nentries; 135 for (i = 0; i < n; i++) { 136 result = callback_runone(ch, arg); 137 if (result != CALLBACK_CHAIN_CONTINUE) { 138 break; 139 } 140 } 141 callback_run_leave(ch); 142 143 return result; 144 } 145