sysmon_taskq.c revision 1.4 1 1.4 christos /* $NetBSD: sysmon_taskq.c,v 1.4 2005/05/29 22:18:25 christos Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2001, 2003 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * General purpose task queue for sysmon back-ends. This can be
40 1.1 thorpej * used to run callbacks that require thread context.
41 1.1 thorpej */
42 1.2 lukem
43 1.2 lukem #include <sys/cdefs.h>
44 1.4 christos __KERNEL_RCSID(0, "$NetBSD: sysmon_taskq.c,v 1.4 2005/05/29 22:18:25 christos Exp $");
45 1.1 thorpej
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <sys/malloc.h>
48 1.1 thorpej #include <sys/lock.h>
49 1.1 thorpej #include <sys/queue.h>
50 1.1 thorpej #include <sys/proc.h>
51 1.1 thorpej #include <sys/kthread.h>
52 1.1 thorpej #include <sys/systm.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/sysmon/sysmon_taskq.h>
55 1.1 thorpej
56 1.1 thorpej struct sysmon_task {
57 1.1 thorpej TAILQ_ENTRY(sysmon_task) st_list;
58 1.1 thorpej void (*st_func)(void *);
59 1.1 thorpej void *st_arg;
60 1.1 thorpej u_int st_pri;
61 1.1 thorpej };
62 1.1 thorpej
63 1.1 thorpej static TAILQ_HEAD(, sysmon_task) sysmon_task_queue =
64 1.1 thorpej TAILQ_HEAD_INITIALIZER(sysmon_task_queue);
65 1.1 thorpej struct simplelock sysmon_task_queue_slock = SIMPLELOCK_INITIALIZER;
66 1.1 thorpej
67 1.1 thorpej #define SYSMON_TASK_QUEUE_LOCK(s) \
68 1.1 thorpej do { \
69 1.1 thorpej s = splsched(); \
70 1.1 thorpej simple_lock(&sysmon_task_queue_slock); \
71 1.1 thorpej } while (/*CONSTCOND*/0)
72 1.1 thorpej
73 1.1 thorpej #define SYSMON_TASK_QUEUE_UNLOCK(s) \
74 1.1 thorpej do { \
75 1.1 thorpej simple_unlock(&sysmon_task_queue_slock); \
76 1.1 thorpej splx((s)); \
77 1.1 thorpej } while (/*CONSTCOND*/0)
78 1.1 thorpej
79 1.4 christos static int sysmon_task_queue_cleanup_sem;
80 1.1 thorpej
81 1.1 thorpej static struct proc *sysmon_task_queue_proc;
82 1.1 thorpej
83 1.1 thorpej static void sysmon_task_queue_create_thread(void *);
84 1.1 thorpej static void sysmon_task_queue_thread(void *);
85 1.1 thorpej
86 1.1 thorpej static struct simplelock sysmon_task_queue_initialized_slock =
87 1.1 thorpej SIMPLELOCK_INITIALIZER;
88 1.1 thorpej static int sysmon_task_queue_initialized;
89 1.1 thorpej
90 1.1 thorpej /*
91 1.1 thorpej * sysmon_task_queue_init:
92 1.1 thorpej *
93 1.1 thorpej * Initialize the sysmon task queue.
94 1.1 thorpej */
95 1.1 thorpej void
96 1.1 thorpej sysmon_task_queue_init(void)
97 1.1 thorpej {
98 1.1 thorpej
99 1.1 thorpej simple_lock(&sysmon_task_queue_initialized_slock);
100 1.1 thorpej if (sysmon_task_queue_initialized) {
101 1.1 thorpej simple_unlock(&sysmon_task_queue_initialized_slock);
102 1.1 thorpej return;
103 1.1 thorpej }
104 1.1 thorpej
105 1.1 thorpej sysmon_task_queue_initialized = 1;
106 1.1 thorpej simple_unlock(&sysmon_task_queue_initialized_slock);
107 1.1 thorpej
108 1.1 thorpej kthread_create(sysmon_task_queue_create_thread, NULL);
109 1.1 thorpej }
110 1.1 thorpej
111 1.1 thorpej /*
112 1.1 thorpej * sysmon_task_queue_fini:
113 1.1 thorpej *
114 1.1 thorpej * Tear town the sysmon task queue.
115 1.1 thorpej */
116 1.1 thorpej void
117 1.1 thorpej sysmon_task_queue_fini(void)
118 1.1 thorpej {
119 1.1 thorpej int s;
120 1.1 thorpej
121 1.1 thorpej SYSMON_TASK_QUEUE_LOCK(s);
122 1.1 thorpej
123 1.1 thorpej sysmon_task_queue_cleanup_sem = 1;
124 1.1 thorpej wakeup(&sysmon_task_queue);
125 1.1 thorpej
126 1.1 thorpej while (sysmon_task_queue_cleanup_sem != 0) {
127 1.4 christos (void)ltsleep(&sysmon_task_queue_cleanup_sem,
128 1.1 thorpej PVM, "stfini", 0, &sysmon_task_queue_slock);
129 1.1 thorpej }
130 1.1 thorpej
131 1.1 thorpej SYSMON_TASK_QUEUE_UNLOCK(s);
132 1.1 thorpej }
133 1.1 thorpej
134 1.1 thorpej /*
135 1.1 thorpej * sysmon_task_queue_create_thread:
136 1.1 thorpej *
137 1.1 thorpej * Create the sysmon task queue execution thread.
138 1.1 thorpej */
139 1.1 thorpej static void
140 1.1 thorpej sysmon_task_queue_create_thread(void *arg)
141 1.1 thorpej {
142 1.1 thorpej int error;
143 1.1 thorpej
144 1.1 thorpej error = kthread_create1(sysmon_task_queue_thread, NULL,
145 1.3 christos &sysmon_task_queue_proc, "sysmon");
146 1.1 thorpej if (error) {
147 1.1 thorpej printf("Unable to create sysmon task queue thread, "
148 1.1 thorpej "error = %d\n", error);
149 1.1 thorpej panic("sysmon_task_queue_create_thread");
150 1.1 thorpej }
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej /*
154 1.1 thorpej * sysmon_task_queue_thread:
155 1.1 thorpej *
156 1.1 thorpej * The sysmon task queue execution thread. We execute callbacks that
157 1.1 thorpej * have been queued for us.
158 1.1 thorpej */
159 1.1 thorpej static void
160 1.1 thorpej sysmon_task_queue_thread(void *arg)
161 1.1 thorpej {
162 1.1 thorpej struct sysmon_task *st;
163 1.1 thorpej int s;
164 1.1 thorpej
165 1.1 thorpej /*
166 1.1 thorpej * Run through all the tasks before we check for the exit
167 1.1 thorpej * condition; it's probably more important to actually run
168 1.1 thorpej * all the tasks before we exit.
169 1.1 thorpej */
170 1.1 thorpej for (;;) {
171 1.1 thorpej SYSMON_TASK_QUEUE_LOCK(s);
172 1.1 thorpej st = TAILQ_FIRST(&sysmon_task_queue);
173 1.1 thorpej if (st == NULL) {
174 1.1 thorpej /* Check for the exit condition. */
175 1.1 thorpej if (sysmon_task_queue_cleanup_sem != 0) {
176 1.1 thorpej /* Time to die. */
177 1.1 thorpej sysmon_task_queue_cleanup_sem = 0;
178 1.4 christos wakeup(&sysmon_task_queue_cleanup_sem);
179 1.1 thorpej SYSMON_TASK_QUEUE_UNLOCK(s);
180 1.1 thorpej kthread_exit(0);
181 1.1 thorpej }
182 1.1 thorpej (void) ltsleep(&sysmon_task_queue, PVM,
183 1.1 thorpej "smtaskq", 0, &sysmon_task_queue_slock);
184 1.1 thorpej SYSMON_TASK_QUEUE_UNLOCK(s);
185 1.1 thorpej continue;
186 1.1 thorpej }
187 1.1 thorpej TAILQ_REMOVE(&sysmon_task_queue, st, st_list);
188 1.1 thorpej SYSMON_TASK_QUEUE_UNLOCK(s);
189 1.1 thorpej
190 1.1 thorpej (*st->st_func)(st->st_arg);
191 1.1 thorpej free(st, M_TEMP);
192 1.1 thorpej }
193 1.1 thorpej panic("sysmon_task_queue_thread: impossible");
194 1.1 thorpej }
195 1.1 thorpej
196 1.1 thorpej /*
197 1.1 thorpej * sysmon_task_queue_sched:
198 1.1 thorpej *
199 1.1 thorpej * Schedule a task for deferred execution.
200 1.1 thorpej */
201 1.1 thorpej int
202 1.1 thorpej sysmon_task_queue_sched(u_int pri, void (*func)(void *), void *arg)
203 1.1 thorpej {
204 1.1 thorpej struct sysmon_task *st, *lst;
205 1.1 thorpej int s;
206 1.1 thorpej
207 1.1 thorpej if (sysmon_task_queue_proc == NULL)
208 1.1 thorpej printf("WARNING: Callback scheduled before sysmon task queue "
209 1.1 thorpej "thread present.\n");
210 1.1 thorpej
211 1.1 thorpej if (func == NULL)
212 1.1 thorpej return (EINVAL);
213 1.1 thorpej
214 1.1 thorpej st = malloc(sizeof(*st), M_TEMP, M_NOWAIT);
215 1.1 thorpej if (st == NULL)
216 1.1 thorpej return (ENOMEM);
217 1.1 thorpej
218 1.1 thorpej st->st_func = func;
219 1.1 thorpej st->st_arg = arg;
220 1.1 thorpej st->st_pri = pri;
221 1.1 thorpej
222 1.1 thorpej SYSMON_TASK_QUEUE_LOCK(s);
223 1.1 thorpej TAILQ_FOREACH(lst, &sysmon_task_queue, st_list) {
224 1.1 thorpej if (st->st_pri > lst->st_pri) {
225 1.1 thorpej TAILQ_INSERT_BEFORE(lst, st, st_list);
226 1.1 thorpej break;
227 1.1 thorpej }
228 1.1 thorpej }
229 1.1 thorpej if (lst == NULL)
230 1.1 thorpej TAILQ_INSERT_TAIL(&sysmon_task_queue, st, st_list);
231 1.1 thorpej wakeup(&sysmon_task_queue);
232 1.1 thorpej SYSMON_TASK_QUEUE_UNLOCK(s);
233 1.1 thorpej
234 1.1 thorpej return (0);
235 1.1 thorpej }
236