Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.23.2.1
      1 /*	$NetBSD: kern_kthread.c,v 1.23.2.1 2008/05/16 02:25:25 yamt 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.23.2.1 2008/05/16 02:25:25 yamt Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/kthread.h>
     40 #include <sys/proc.h>
     41 #include <sys/sched.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 /*
     47  * note that stdarg.h and the ansi style va_start macro is used for both
     48  * ansi and traditional c complers.
     49  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     50  */
     51 #include <machine/stdarg.h>
     52 
     53 /*
     54  * Fork a kernel thread.  Any process can request this to be done.
     55  */
     56 int
     57 kthread_create(pri_t pri, int flag, struct cpu_info *ci,
     58 	       void (*func)(void *), void *arg,
     59 	       lwp_t **lp, const char *fmt, ...)
     60 {
     61 	lwp_t *l;
     62 	vaddr_t uaddr;
     63 	bool inmem;
     64 	int error;
     65 	va_list ap;
     66 
     67 	inmem = uvm_uarea_alloc(&uaddr);
     68 	if (uaddr == 0)
     69 		return ENOMEM;
     70 	error = lwp_create(&lwp0, &proc0, uaddr, inmem, LWP_DETACHED, NULL,
     71 	    0, func, arg, &l, SCHED_FIFO);
     72 	if (error) {
     73 		uvm_uarea_free(uaddr, curcpu());
     74 		return error;
     75 	}
     76 	uvm_lwp_hold(l);
     77 	if (fmt != NULL) {
     78 		l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
     79 		if (l->l_name == NULL) {
     80 			lwp_exit(l);
     81 			return ENOMEM;
     82 		}
     83 		va_start(ap, fmt);
     84 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
     85 		va_end(ap);
     86 	}
     87 
     88 	/*
     89 	 * Set parameters.
     90 	 */
     91 	if ((flag & KTHREAD_INTR) != 0) {
     92 		KASSERT((flag & KTHREAD_MPSAFE) != 0);
     93 	}
     94 
     95 	if (pri == PRI_NONE) {
     96 		/* Minimum kernel priority level. */
     97 		pri = PRI_KTHREAD;
     98 	}
     99 	mutex_enter(proc0.p_lock);
    100 	lwp_lock(l);
    101 	l->l_priority = pri;
    102 	if (ci != NULL) {
    103 		if (ci != l->l_cpu) {
    104 			lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
    105 			lwp_lock(l);
    106 		}
    107 		l->l_pflag |= LP_BOUND;
    108 		l->l_cpu = ci;
    109 	}
    110 	if ((flag & KTHREAD_INTR) != 0)
    111 		l->l_pflag |= LP_INTR;
    112 	if ((flag & KTHREAD_MPSAFE) == 0)
    113 		l->l_pflag &= ~LP_MPSAFE;
    114 
    115 	/*
    116 	 * Set the new LWP running, unless the caller has requested
    117 	 * otherwise.
    118 	 */
    119 	if ((flag & KTHREAD_IDLE) == 0) {
    120 		l->l_stat = LSRUN;
    121 		sched_enqueue(l, false);
    122 		lwp_unlock(l);
    123 	} else
    124 		lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
    125 
    126 	/*
    127 	 * The LWP is not created suspended or stopped and cannot be set
    128 	 * into those states later, so must be considered runnable.
    129 	 */
    130 	proc0.p_nrlwps++;
    131 	mutex_exit(proc0.p_lock);
    132 
    133 	/* All done! */
    134 	if (lp != NULL)
    135 		*lp = l;
    136 
    137 	return (0);
    138 }
    139 
    140 /*
    141  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    142  * current context.
    143  */
    144 void
    145 kthread_exit(int ecode)
    146 {
    147 	const char *name;
    148 	lwp_t *l = curlwp;
    149 
    150 	/* We can't do much with the exit code, so just report it. */
    151 	if (ecode != 0) {
    152 		if ((name = l->l_name) == NULL)
    153 			name = "unnamed";
    154 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
    155 		    name, l->l_lid, ecode);
    156 	}
    157 
    158 	/* And exit.. */
    159 	lwp_exit(l);
    160 
    161 	/*
    162 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
    163 	 * XXX of worms right now.
    164 	 */
    165 	for (;;)
    166 		;
    167 }
    168 
    169 /*
    170  * Destroy an inactive kthread.  The kthread must be in the LSIDL state.
    171  */
    172 void
    173 kthread_destroy(lwp_t *l)
    174 {
    175 
    176 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
    177 	KASSERT(l->l_stat == LSIDL);
    178 
    179 	lwp_exit(l);
    180 }
    181