Home | History | Annotate | Line # | Download | only in sysmon
sysmon_taskq.c revision 1.9
      1 /*	$NetBSD: sysmon_taskq.c,v 1.9 2007/07/09 21:01:24 ad 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/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: sysmon_taskq.c,v 1.9 2007/07/09 21:01:24 ad Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/malloc.h>
     48 #include <sys/lock.h>
     49 #include <sys/queue.h>
     50 #include <sys/proc.h>
     51 #include <sys/kthread.h>
     52 #include <sys/systm.h>
     53 
     54 #include <dev/sysmon/sysmon_taskq.h>
     55 
     56 struct sysmon_task {
     57 	TAILQ_ENTRY(sysmon_task) st_list;
     58 	void (*st_func)(void *);
     59 	void *st_arg;
     60 	u_int st_pri;
     61 };
     62 
     63 static TAILQ_HEAD(, sysmon_task) sysmon_task_queue =
     64     TAILQ_HEAD_INITIALIZER(sysmon_task_queue);
     65 struct simplelock sysmon_task_queue_slock = SIMPLELOCK_INITIALIZER;
     66 
     67 #define	SYSMON_TASK_QUEUE_LOCK(s)					\
     68 do {									\
     69 	s = splsched();							\
     70 	simple_lock(&sysmon_task_queue_slock);				\
     71 } while (/*CONSTCOND*/0)
     72 
     73 #define	SYSMON_TASK_QUEUE_UNLOCK(s)					\
     74 do {									\
     75 	simple_unlock(&sysmon_task_queue_slock);			\
     76 	splx((s));							\
     77 } while (/*CONSTCOND*/0)
     78 
     79 static int sysmon_task_queue_cleanup_sem;
     80 
     81 static struct lwp *sysmon_task_queue_lwp;
     82 
     83 static void sysmon_task_queue_thread(void *);
     84 
     85 static struct simplelock sysmon_task_queue_initialized_slock =
     86     SIMPLELOCK_INITIALIZER;
     87 static int sysmon_task_queue_initialized;
     88 
     89 /*
     90  * sysmon_task_queue_init:
     91  *
     92  *	Initialize the sysmon task queue.
     93  */
     94 void
     95 sysmon_task_queue_init(void)
     96 {
     97 	int error;
     98 
     99 	simple_lock(&sysmon_task_queue_initialized_slock);
    100 	if (sysmon_task_queue_initialized) {
    101 		simple_unlock(&sysmon_task_queue_initialized_slock);
    102 		return;
    103 	}
    104 
    105 	sysmon_task_queue_initialized = 1;
    106 	simple_unlock(&sysmon_task_queue_initialized_slock);
    107 
    108 	error = kthread_create(PRI_NONE, 0, NULL, sysmon_task_queue_thread,
    109 	    NULL, &sysmon_task_queue_lwp, "sysmon");
    110 	if (error) {
    111 		printf("Unable to create sysmon task queue thread, "
    112 		    "error = %d\n", error);
    113 		panic("sysmon_task_queue_init");
    114 	}
    115 }
    116 
    117 /*
    118  * sysmon_task_queue_fini:
    119  *
    120  *	Tear town the sysmon task queue.
    121  */
    122 void
    123 sysmon_task_queue_fini(void)
    124 {
    125 	int s;
    126 
    127 	SYSMON_TASK_QUEUE_LOCK(s);
    128 
    129 	sysmon_task_queue_cleanup_sem = 1;
    130 	wakeup(&sysmon_task_queue);
    131 
    132 	while (sysmon_task_queue_cleanup_sem != 0) {
    133 		(void)ltsleep(&sysmon_task_queue_cleanup_sem,
    134 		    PVM, "stfini", 0, &sysmon_task_queue_slock);
    135 	}
    136 
    137 	SYSMON_TASK_QUEUE_UNLOCK(s);
    138 }
    139 
    140 /*
    141  * sysmon_task_queue_thread:
    142  *
    143  *	The sysmon task queue execution thread.  We execute callbacks that
    144  *	have been queued for us.
    145  */
    146 static void
    147 sysmon_task_queue_thread(void *arg)
    148 {
    149 	struct sysmon_task *st;
    150 	int s;
    151 
    152 	/*
    153 	 * Run through all the tasks before we check for the exit
    154 	 * condition; it's probably more important to actually run
    155 	 * all the tasks before we exit.
    156 	 */
    157 	for (;;) {
    158 		SYSMON_TASK_QUEUE_LOCK(s);
    159 		st = TAILQ_FIRST(&sysmon_task_queue);
    160 		if (st == NULL) {
    161 			/* Check for the exit condition. */
    162 			if (sysmon_task_queue_cleanup_sem != 0) {
    163 				/* Time to die. */
    164 				sysmon_task_queue_cleanup_sem = 0;
    165 				wakeup(&sysmon_task_queue_cleanup_sem);
    166 				SYSMON_TASK_QUEUE_UNLOCK(s);
    167 				kthread_exit(0);
    168 			}
    169 			(void) ltsleep(&sysmon_task_queue, PVM,
    170 			    "smtaskq", 0, &sysmon_task_queue_slock);
    171 			SYSMON_TASK_QUEUE_UNLOCK(s);
    172 			continue;
    173 		}
    174 		TAILQ_REMOVE(&sysmon_task_queue, st, st_list);
    175 		SYSMON_TASK_QUEUE_UNLOCK(s);
    176 
    177 		(*st->st_func)(st->st_arg);
    178 		free(st, M_TEMP);
    179 	}
    180 	panic("sysmon_task_queue_thread: impossible");
    181 }
    182 
    183 /*
    184  * sysmon_task_queue_sched:
    185  *
    186  *	Schedule a task for deferred execution.
    187  */
    188 int
    189 sysmon_task_queue_sched(u_int pri, void (*func)(void *), void *arg)
    190 {
    191 	struct sysmon_task *st, *lst;
    192 	int s;
    193 
    194 	if (sysmon_task_queue_lwp == NULL)
    195 		aprint_debug("WARNING: Callback scheduled before sysmon "
    196 		    "task queue thread present\n");
    197 
    198 	if (func == NULL)
    199 		return (EINVAL);
    200 
    201 	st = malloc(sizeof(*st), M_TEMP, M_NOWAIT);
    202 	if (st == NULL)
    203 		return (ENOMEM);
    204 
    205 	st->st_func = func;
    206 	st->st_arg = arg;
    207 	st->st_pri = pri;
    208 
    209 	SYSMON_TASK_QUEUE_LOCK(s);
    210 	TAILQ_FOREACH(lst, &sysmon_task_queue, st_list) {
    211 		if (st->st_pri > lst->st_pri) {
    212 			TAILQ_INSERT_BEFORE(lst, st, st_list);
    213 			break;
    214 		}
    215 	}
    216 	if (lst == NULL)
    217 		TAILQ_INSERT_TAIL(&sysmon_task_queue, st, st_list);
    218 	wakeup(&sysmon_task_queue);
    219 	SYSMON_TASK_QUEUE_UNLOCK(s);
    220 
    221 	return (0);
    222 }
    223