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