sysmon_taskq.c revision 1.17 1 1.17 pgoyette /* $NetBSD: sysmon_taskq.c,v 1.17 2015/04/24 00:31:04 pgoyette 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.17 pgoyette __KERNEL_RCSID(0, "$NetBSD: sysmon_taskq.c,v 1.17 2015/04/24 00:31:04 pgoyette 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/queue.h>
49 1.1 thorpej #include <sys/proc.h>
50 1.1 thorpej #include <sys/kthread.h>
51 1.1 thorpej #include <sys/systm.h>
52 1.15 pgoyette #include <sys/module.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
66 1.10 xtraeme static kmutex_t sysmon_task_queue_mtx;
67 1.10 xtraeme static kmutex_t sysmon_task_queue_init_mtx;
68 1.10 xtraeme static kcondvar_t sysmon_task_queue_cv;
69 1.1 thorpej
70 1.10 xtraeme static int sysmon_task_queue_initialized;
71 1.4 christos static int sysmon_task_queue_cleanup_sem;
72 1.9 ad static struct lwp *sysmon_task_queue_lwp;
73 1.10 xtraeme static void sysmon_task_queue_thread(void *);
74 1.1 thorpej
75 1.15 pgoyette MODULE(MODULE_CLASS_MISC, sysmon_taskq, NULL);
76 1.15 pgoyette
77 1.15 pgoyette /*
78 1.15 pgoyette * XXX Normally, all initialization would be handled as part of
79 1.15 pgoyette * the module(9) framework. However, there are a number of
80 1.15 pgoyette * users of the sysmon_taskq facility that are not modular,
81 1.15 pgoyette * and these can directly call sysmon_task_queue_init()
82 1.15 pgoyette * directly. To accomodate these non-standard users, we
83 1.15 pgoyette * make sure that sysmon_task_queue_init() handles multiple
84 1.15 pgoyette * invocations. And we also ensure that, if any non-module
85 1.15 pgoyette * user exists, we don't allow the module to be unloaded.
86 1.15 pgoyette * (We can't use module_hold() for this, since the module(9)
87 1.15 pgoyette * framework itself isn't necessarily initialized yet.)
88 1.15 pgoyette */
89 1.15 pgoyette
90 1.15 pgoyette /*
91 1.15 pgoyette * sysmon_task_queue_preinit:
92 1.15 pgoyette *
93 1.15 pgoyette * Early one-time initialization of task-queue
94 1.15 pgoyette */
95 1.10 xtraeme void
96 1.10 xtraeme sysmon_task_queue_preinit(void)
97 1.10 xtraeme {
98 1.15 pgoyette
99 1.11 ad mutex_init(&sysmon_task_queue_mtx, MUTEX_DEFAULT, IPL_VM);
100 1.10 xtraeme mutex_init(&sysmon_task_queue_init_mtx, MUTEX_DEFAULT, IPL_NONE);
101 1.10 xtraeme cv_init(&sysmon_task_queue_cv, "smtaskq");
102 1.15 pgoyette sysmon_task_queue_initialized = 0;
103 1.10 xtraeme }
104 1.1 thorpej
105 1.1 thorpej /*
106 1.1 thorpej * sysmon_task_queue_init:
107 1.1 thorpej *
108 1.1 thorpej * Initialize the sysmon task queue.
109 1.1 thorpej */
110 1.1 thorpej void
111 1.1 thorpej sysmon_task_queue_init(void)
112 1.1 thorpej {
113 1.9 ad int error;
114 1.1 thorpej
115 1.10 xtraeme mutex_enter(&sysmon_task_queue_init_mtx);
116 1.15 pgoyette if (sysmon_task_queue_initialized++) {
117 1.10 xtraeme mutex_exit(&sysmon_task_queue_init_mtx);
118 1.1 thorpej return;
119 1.1 thorpej }
120 1.1 thorpej
121 1.10 xtraeme mutex_exit(&sysmon_task_queue_init_mtx);
122 1.1 thorpej
123 1.13 xtraeme error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
124 1.13 xtraeme sysmon_task_queue_thread, NULL, &sysmon_task_queue_lwp, "sysmon");
125 1.9 ad if (error) {
126 1.9 ad printf("Unable to create sysmon task queue thread, "
127 1.9 ad "error = %d\n", error);
128 1.9 ad panic("sysmon_task_queue_init");
129 1.9 ad }
130 1.1 thorpej }
131 1.1 thorpej
132 1.1 thorpej /*
133 1.1 thorpej * sysmon_task_queue_fini:
134 1.1 thorpej *
135 1.1 thorpej * Tear town the sysmon task queue.
136 1.1 thorpej */
137 1.17 pgoyette int
138 1.1 thorpej sysmon_task_queue_fini(void)
139 1.1 thorpej {
140 1.1 thorpej
141 1.15 pgoyette if (sysmon_task_queue_initialized > 1)
142 1.17 pgoyette return EBUSY;
143 1.15 pgoyette
144 1.10 xtraeme mutex_enter(&sysmon_task_queue_mtx);
145 1.1 thorpej
146 1.1 thorpej sysmon_task_queue_cleanup_sem = 1;
147 1.10 xtraeme cv_signal(&sysmon_task_queue_cv);
148 1.1 thorpej
149 1.10 xtraeme while (sysmon_task_queue_cleanup_sem != 0)
150 1.10 xtraeme cv_wait(&sysmon_task_queue_cv,
151 1.10 xtraeme &sysmon_task_queue_mtx);
152 1.1 thorpej
153 1.10 xtraeme mutex_exit(&sysmon_task_queue_mtx);
154 1.17 pgoyette
155 1.17 pgoyette return 0;
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej /*
159 1.1 thorpej * sysmon_task_queue_thread:
160 1.1 thorpej *
161 1.1 thorpej * The sysmon task queue execution thread. We execute callbacks that
162 1.1 thorpej * have been queued for us.
163 1.1 thorpej */
164 1.1 thorpej static void
165 1.7 christos sysmon_task_queue_thread(void *arg)
166 1.1 thorpej {
167 1.1 thorpej struct sysmon_task *st;
168 1.1 thorpej
169 1.1 thorpej /*
170 1.1 thorpej * Run through all the tasks before we check for the exit
171 1.1 thorpej * condition; it's probably more important to actually run
172 1.1 thorpej * all the tasks before we exit.
173 1.1 thorpej */
174 1.14 gmcgarry mutex_enter(&sysmon_task_queue_mtx);
175 1.1 thorpej for (;;) {
176 1.1 thorpej st = TAILQ_FIRST(&sysmon_task_queue);
177 1.14 gmcgarry if (st != NULL) {
178 1.14 gmcgarry TAILQ_REMOVE(&sysmon_task_queue, st, st_list);
179 1.14 gmcgarry mutex_exit(&sysmon_task_queue_mtx);
180 1.14 gmcgarry (*st->st_func)(st->st_arg);
181 1.14 gmcgarry free(st, M_TEMP);
182 1.14 gmcgarry mutex_enter(&sysmon_task_queue_mtx);
183 1.14 gmcgarry } else {
184 1.1 thorpej /* Check for the exit condition. */
185 1.14 gmcgarry if (sysmon_task_queue_cleanup_sem != 0)
186 1.14 gmcgarry break;
187 1.10 xtraeme cv_wait(&sysmon_task_queue_cv, &sysmon_task_queue_mtx);
188 1.1 thorpej }
189 1.1 thorpej }
190 1.14 gmcgarry /* Time to die. */
191 1.14 gmcgarry sysmon_task_queue_cleanup_sem = 0;
192 1.14 gmcgarry cv_broadcast(&sysmon_task_queue_cv);
193 1.14 gmcgarry mutex_exit(&sysmon_task_queue_mtx);
194 1.14 gmcgarry kthread_exit(0);
195 1.1 thorpej }
196 1.1 thorpej
197 1.1 thorpej /*
198 1.1 thorpej * sysmon_task_queue_sched:
199 1.1 thorpej *
200 1.1 thorpej * Schedule a task for deferred execution.
201 1.1 thorpej */
202 1.1 thorpej int
203 1.1 thorpej sysmon_task_queue_sched(u_int pri, void (*func)(void *), void *arg)
204 1.1 thorpej {
205 1.1 thorpej struct sysmon_task *st, *lst;
206 1.1 thorpej
207 1.9 ad if (sysmon_task_queue_lwp == NULL)
208 1.8 xtraeme aprint_debug("WARNING: Callback scheduled before sysmon "
209 1.8 xtraeme "task queue thread present\n");
210 1.1 thorpej
211 1.1 thorpej if (func == NULL)
212 1.10 xtraeme 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.10 xtraeme 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.10 xtraeme mutex_enter(&sysmon_task_queue_mtx);
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.10 xtraeme
230 1.1 thorpej if (lst == NULL)
231 1.1 thorpej TAILQ_INSERT_TAIL(&sysmon_task_queue, st, st_list);
232 1.1 thorpej
233 1.10 xtraeme cv_broadcast(&sysmon_task_queue_cv);
234 1.10 xtraeme mutex_exit(&sysmon_task_queue_mtx);
235 1.10 xtraeme
236 1.10 xtraeme return 0;
237 1.1 thorpej }
238 1.15 pgoyette
239 1.15 pgoyette static
240 1.15 pgoyette int
241 1.15 pgoyette sysmon_taskq_modcmd(modcmd_t cmd, void *arg)
242 1.15 pgoyette {
243 1.15 pgoyette int ret;
244 1.15 pgoyette
245 1.15 pgoyette switch (cmd) {
246 1.15 pgoyette case MODULE_CMD_INIT:
247 1.15 pgoyette #ifdef _MODULE
248 1.15 pgoyette sysmon_task_queue_preinit();
249 1.15 pgoyette #endif
250 1.15 pgoyette sysmon_task_queue_init();
251 1.15 pgoyette ret = 0;
252 1.15 pgoyette break;
253 1.15 pgoyette
254 1.15 pgoyette case MODULE_CMD_FINI:
255 1.17 pgoyette ret = sysmon_task_queue_fini();
256 1.15 pgoyette break;
257 1.15 pgoyette
258 1.15 pgoyette case MODULE_CMD_STAT:
259 1.15 pgoyette default:
260 1.15 pgoyette ret = ENOTTY;
261 1.15 pgoyette }
262 1.15 pgoyette
263 1.15 pgoyette return ret;
264 1.15 pgoyette }
265