Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.16.6.1
      1 /*	$NetBSD: kern_kthread.c,v 1.16.6.1 2007/04/09 22:10:02 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.16.6.1 2007/04/09 22:10:02 ad Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kthread.h>
     47 #include <sys/proc.h>
     48 #include <sys/wait.h>
     49 #include <sys/malloc.h>
     50 #include <sys/queue.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 /*
     55  * note that stdarg.h and the ansi style va_start macro is used for both
     56  * ansi and traditional c complers.
     57  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     58  */
     59 #include <machine/stdarg.h>
     60 
     61 bool	kthread_create_now;
     62 
     63 /*
     64  * Fork a kernel thread.  Any process can request this to be done.
     65  * The VM space and limits, etc. will be shared with proc0.
     66  */
     67 int
     68 kthread_create1(pri_t pri, bool mpsafe, void (*func)(void *), void *arg,
     69 		struct lwp **lp, const char *fmt, ...)
     70 {
     71 	struct lwp *l;
     72 	vaddr_t uaddr;
     73 	bool inmem;
     74 	int error;
     75 	va_list ap;
     76 
     77 	inmem = uvm_uarea_alloc(&uaddr);
     78 	if (uaddr == 0)
     79 		return ENOMEM;
     80 	error = newlwp(&lwp0, &proc0, uaddr, inmem, 0, NULL, 0, func, arg, &l);
     81 	if (error) {
     82 		/* XXX uvm_uarea_free(uaddr); */
     83 		return error;
     84 	}
     85 
     86 	/* Set parameters. */
     87 	if (pri == PRI_NONE) {
     88 		/* Minimum kernel priority level. */
     89 		pri = PUSER - 1;
     90 	}
     91 	if (mpsafe)
     92 		l->l_pflag |= LP_MPSAFE;
     93 	if (fmt != NULL) {
     94 		va_start(ap, fmt);
     95 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
     96 		va_end(ap);
     97 	}
     98 
     99 	/* Set the new LWP running. */
    100 	mutex_enter(&proc0.p_smutex);
    101 	lwp_lock(l);
    102 	l->l_usrpri = pri;
    103 	l->l_priority = pri;
    104 	l->l_stat = LSRUN;
    105 	proc0.p_nrlwps++;
    106 	setrunqueue(l);
    107 	lwp_unlock(l);
    108 	mutex_exit(&proc0.p_smutex);
    109 
    110 	/* All done! */
    111 	if (lp != NULL)
    112 		*lp = l;
    113 
    114 	return (0);
    115 }
    116 
    117 /*
    118  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    119  * current context.
    120  */
    121 void
    122 kthread_exit(int ecode)
    123 {
    124 	struct lwp *l = curlwp;
    125 
    126 	/* We can't do much with the exit code, so just report it. */
    127 	if (ecode != 0)
    128 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
    129 		    l->l_name, l->l_lid, ecode);
    130 
    131 	/* Acquire the sched state mutex.  exit1() will release it. */
    132 	lwp_exit(l);
    133 
    134 	/*
    135 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
    136 	 * XXX of worms right now.
    137 	 */
    138 	for (;;);
    139 }
    140 
    141 struct kthread_q {
    142 	SIMPLEQ_ENTRY(kthread_q) kq_q;
    143 	void (*kq_func)(void *);
    144 	void *kq_arg;
    145 };
    146 
    147 SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q);
    148 
    149 /*
    150  * Defer the creation of a kernel thread.  Once the standard kernel threads
    151  * and processes have been created, this queue will be run to callback to
    152  * the caller to create threads for e.g. file systems and device drivers.
    153  */
    154 void
    155 kthread_create(void (*func)(void *), void *arg)
    156 {
    157 	struct kthread_q *kq;
    158 
    159 	if (kthread_create_now) {
    160 		(*func)(arg);
    161 		return;
    162 	}
    163 
    164 	kq = malloc(sizeof(*kq), M_TEMP, M_NOWAIT);
    165 	if (kq == NULL)
    166 		panic("unable to allocate kthread_q");
    167 	memset(kq, 0, sizeof(*kq));
    168 
    169 	kq->kq_func = func;
    170 	kq->kq_arg = arg;
    171 
    172 	SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q);
    173 }
    174 
    175 void
    176 kthread_run_deferred_queue(void)
    177 {
    178 	struct kthread_q *kq;
    179 
    180 	/* No longer need to defer kthread creation. */
    181 	kthread_create_now = true;
    182 
    183 	while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) {
    184 		SIMPLEQ_REMOVE_HEAD(&kthread_q, kq_q);
    185 		(*kq->kq_func)(kq->kq_arg);
    186 		free(kq, M_TEMP);
    187 	}
    188 }
    189