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