xref: /netbsd-src/sys/kern/subr_callback.c (revision 606b1d9782fbccbc21b0465f1b59942cd238a6f5)
1*606b1d97Srmind /*	$NetBSD: subr_callback.c,v 1.7 2009/11/24 20:11:50 rmind Exp $	*/
2c24f70bcSyamt 
3c24f70bcSyamt /*-
4c24f70bcSyamt  * Copyright (c)2006 YAMAMOTO Takashi,
5c24f70bcSyamt  * All rights reserved.
6c24f70bcSyamt  *
7c24f70bcSyamt  * Redistribution and use in source and binary forms, with or without
8c24f70bcSyamt  * modification, are permitted provided that the following conditions
9c24f70bcSyamt  * are met:
10c24f70bcSyamt  * 1. Redistributions of source code must retain the above copyright
11c24f70bcSyamt  *    notice, this list of conditions and the following disclaimer.
12c24f70bcSyamt  * 2. Redistributions in binary form must reproduce the above copyright
13c24f70bcSyamt  *    notice, this list of conditions and the following disclaimer in the
14c24f70bcSyamt  *    documentation and/or other materials provided with the distribution.
15c24f70bcSyamt  *
16c24f70bcSyamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c24f70bcSyamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c24f70bcSyamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c24f70bcSyamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c24f70bcSyamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c24f70bcSyamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c24f70bcSyamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c24f70bcSyamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c24f70bcSyamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c24f70bcSyamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c24f70bcSyamt  * SUCH DAMAGE.
27c24f70bcSyamt  */
28c24f70bcSyamt 
29c24f70bcSyamt #include <sys/cdefs.h>
30*606b1d97Srmind __KERNEL_RCSID(0, "$NetBSD: subr_callback.c,v 1.7 2009/11/24 20:11:50 rmind Exp $");
31c24f70bcSyamt 
32c24f70bcSyamt #include <sys/param.h>
33c24f70bcSyamt #include <sys/systm.h>
34c24f70bcSyamt #include <sys/proc.h>
35c24f70bcSyamt #include <sys/callback.h>
36c24f70bcSyamt 
37c24f70bcSyamt void
callback_head_init(struct callback_head * ch,int ipl)3888ab7da9Sad callback_head_init(struct callback_head *ch, int ipl)
39c24f70bcSyamt {
40c24f70bcSyamt 
419b002317Sdrochner 	memset(ch, 0, sizeof(struct callback_head));
42598ab03aSad 	mutex_init(&ch->ch_lock, MUTEX_DEFAULT, ipl);
4388ab7da9Sad 	cv_init(&ch->ch_cv, "callback");
44c24f70bcSyamt 	TAILQ_INIT(&ch->ch_q);
4599f30cd9Sdrochner #if 0 /* already zero-initialized */
4699f30cd9Sdrochner 	ch->ch_next = NULL;
4799f30cd9Sdrochner 	ch->ch_nentries = 0;
4899f30cd9Sdrochner 	ch->ch_running = 0;
4999f30cd9Sdrochner 	ch->ch_flags = 0;
5099f30cd9Sdrochner #endif
51c24f70bcSyamt }
52c24f70bcSyamt 
53c24f70bcSyamt void
callback_head_destroy(struct callback_head * ch)5488ab7da9Sad callback_head_destroy(struct callback_head *ch)
5588ab7da9Sad {
5688ab7da9Sad 
5788ab7da9Sad 	mutex_destroy(&ch->ch_lock);
5888ab7da9Sad 	cv_destroy(&ch->ch_cv);
5988ab7da9Sad }
6088ab7da9Sad 
6188ab7da9Sad void
callback_register(struct callback_head * ch,struct callback_entry * ce,void * obj,int (* fn)(struct callback_entry *,void *,void *))62c24f70bcSyamt callback_register(struct callback_head *ch, struct callback_entry *ce,
63c24f70bcSyamt     void *obj, int (*fn)(struct callback_entry *, void *, void *))
64c24f70bcSyamt {
65c24f70bcSyamt 
66*606b1d97Srmind 	KASSERT(fn != NULL);
67c24f70bcSyamt 	ce->ce_func = fn;
68c24f70bcSyamt 	ce->ce_obj = obj;
6988ab7da9Sad 	mutex_enter(&ch->ch_lock);
70c24f70bcSyamt 	TAILQ_INSERT_TAIL(&ch->ch_q, ce, ce_q);
71c24f70bcSyamt 	ch->ch_nentries++;
7288ab7da9Sad 	mutex_exit(&ch->ch_lock);
73c24f70bcSyamt }
74c24f70bcSyamt 
75c24f70bcSyamt void
callback_unregister(struct callback_head * ch,struct callback_entry * ce)76c24f70bcSyamt callback_unregister(struct callback_head *ch, struct callback_entry *ce)
77c24f70bcSyamt {
78c24f70bcSyamt 
7988ab7da9Sad 	mutex_enter(&ch->ch_lock);
8088ab7da9Sad 	while (ch->ch_running > 0)
8188ab7da9Sad 		cv_wait(&ch->ch_cv, &ch->ch_lock);
82c24f70bcSyamt 	if (__predict_false(ch->ch_next == ce)) {
83c24f70bcSyamt 		ch->ch_next = TAILQ_NEXT(ce, ce_q);
84c24f70bcSyamt 	}
85c24f70bcSyamt 	TAILQ_REMOVE(&ch->ch_q, ce, ce_q);
86c24f70bcSyamt 	ch->ch_nentries--;
8788ab7da9Sad 	mutex_exit(&ch->ch_lock);
88c24f70bcSyamt }
89c24f70bcSyamt 
90c24f70bcSyamt static int
callback_runone(struct callback_head * ch,void * arg)91c24f70bcSyamt callback_runone(struct callback_head *ch, void *arg)
92c24f70bcSyamt {
93c24f70bcSyamt 	struct callback_entry *ce;
94c24f70bcSyamt 	int result;
95c24f70bcSyamt 
96c24f70bcSyamt 	KASSERT(ch->ch_nentries > 0);
97c24f70bcSyamt 	KASSERT(ch->ch_running > 0);
98c24f70bcSyamt 
99c24f70bcSyamt 	ce = ch->ch_next;
100c24f70bcSyamt 	if (ce == NULL) {
101c24f70bcSyamt 		ce = TAILQ_FIRST(&ch->ch_q);
102c24f70bcSyamt 	}
103c24f70bcSyamt 	KASSERT(ce != NULL);
104*606b1d97Srmind 	KASSERT(ce->ce_func != NULL);
105c24f70bcSyamt 	result = (*ce->ce_func)(ce, ce->ce_obj, arg);
106c24f70bcSyamt 	ch->ch_next = TAILQ_NEXT(ce, ce_q);
107c24f70bcSyamt 	return result;
108c24f70bcSyamt }
109c24f70bcSyamt 
110c24f70bcSyamt static void
callback_run_enter(struct callback_head * ch)111c24f70bcSyamt callback_run_enter(struct callback_head *ch)
112c24f70bcSyamt {
113c24f70bcSyamt 
11488ab7da9Sad 	mutex_enter(&ch->ch_lock);
115c24f70bcSyamt 	ch->ch_running++;
11688ab7da9Sad 	mutex_exit(&ch->ch_lock);
117c24f70bcSyamt }
118c24f70bcSyamt 
119c24f70bcSyamt static void
callback_run_leave(struct callback_head * ch)120c24f70bcSyamt callback_run_leave(struct callback_head *ch)
121c24f70bcSyamt {
122c24f70bcSyamt 
12388ab7da9Sad 	mutex_enter(&ch->ch_lock);
124c24f70bcSyamt 	KASSERT(ch->ch_running > 0);
125c24f70bcSyamt 	ch->ch_running--;
12688ab7da9Sad 	if (ch->ch_running == 0)
12788ab7da9Sad 		cv_broadcast(&ch->ch_cv);
12888ab7da9Sad 	mutex_exit(&ch->ch_lock);
129c24f70bcSyamt }
130c24f70bcSyamt 
131c24f70bcSyamt int
callback_run_roundrobin(struct callback_head * ch,void * arg)132c24f70bcSyamt callback_run_roundrobin(struct callback_head *ch, void *arg)
133c24f70bcSyamt {
134c24f70bcSyamt 	int i;
135c24f70bcSyamt 	int n;
136c24f70bcSyamt 	int result = 0;
137c24f70bcSyamt 
138c24f70bcSyamt 	callback_run_enter(ch);
139c24f70bcSyamt 	n = ch->ch_nentries;
140c24f70bcSyamt 	for (i = 0; i < n; i++) {
141c24f70bcSyamt 		result = callback_runone(ch, arg);
142c24f70bcSyamt 		if (result != CALLBACK_CHAIN_CONTINUE) {
143c24f70bcSyamt 			break;
144c24f70bcSyamt 		}
145c24f70bcSyamt 	}
146c24f70bcSyamt 	callback_run_leave(ch);
147c24f70bcSyamt 
148c24f70bcSyamt 	return result;
149c24f70bcSyamt }
150