Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.9.2.1
      1 /*	$NetBSD: kern_kthread.c,v 1.9.2.1 2000/06/22 17:09:07 minoura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 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.
     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/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/kthread.h>
     44 #include <sys/proc.h>
     45 #include <sys/wait.h>
     46 #include <sys/malloc.h>
     47 #include <sys/queue.h>
     48 
     49 /*
     50  * note that stdarg.h and the ansi style va_start macro is used for both
     51  * ansi and traditional c complers.
     52  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     53  */
     54 #include <machine/stdarg.h>
     55 
     56 int	kthread_create_now;
     57 
     58 /*
     59  * Fork a kernel thread.  Any process can request this to be done.
     60  * The VM space and limits, etc. will be shared with proc0.
     61  */
     62 int
     63 #if __STDC__
     64 kthread_create1(void (*func)(void *), void *arg,
     65     struct proc **newpp, const char *fmt, ...)
     66 #else
     67 kthread_create1(func, arg, newpp, fmt, va_alist)
     68 	void (*func) __P((void *));
     69 	void *arg;
     70 	struct proc **newpp;
     71 	const char *fmt;
     72 	va_dcl
     73 #endif
     74 {
     75 	struct proc *p2;
     76 	int error;
     77 	va_list ap;
     78 
     79 	/* First, create the new process. */
     80 	error = fork1(&proc0, FORK_SHAREVM | FORK_SHARECWD | FORK_SHAREFILES |
     81 	    FORK_SHARESIGS, SIGCHLD, NULL, 0, func, arg, NULL, &p2);
     82 	if (__predict_false(error != 0))
     83 		return (error);
     84 
     85 	/*
     86 	 * Mark it as a system process and not a candidate for
     87 	 * swapping.  Set P_NOCLDWAIT so that children are reparented
     88 	 * to init(8) when they exit.  init(8) can easily wait them
     89 	 * out for us.
     90 	 */
     91 	p2->p_flag |= P_INMEM | P_SYSTEM | P_NOCLDWAIT;	/* XXX */
     92 
     93 	/* Name it as specified. */
     94 	va_start(ap, fmt);
     95 	vsnprintf(p2->p_comm, MAXCOMLEN, fmt, ap);
     96 	va_end(ap);
     97 
     98 	/* All done! */
     99 	if (newpp != NULL)
    100 		*newpp = p2;
    101 	return (0);
    102 }
    103 
    104 /*
    105  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    106  * current context.
    107  */
    108 void
    109 kthread_exit(ecode)
    110 	int ecode;
    111 {
    112 
    113 	/*
    114 	 * XXX What do we do with the exit code?  Should we even bother
    115 	 * XXX with it?  The parent (proc0) isn't going to do much with
    116 	 * XXX it.
    117 	 */
    118 	if (ecode != 0)
    119 		printf("WARNING: thread `%s' (%d) exits with status %d\n",
    120 		    curproc->p_comm, curproc->p_pid, ecode);
    121 
    122 	exit1(curproc, W_EXITCODE(ecode, 0));
    123 
    124 	/*
    125 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
    126 	 * XXX of worms right now.
    127 	 */
    128 	for (;;);
    129 }
    130 
    131 struct kthread_q {
    132 	SIMPLEQ_ENTRY(kthread_q) kq_q;
    133 	void (*kq_func) __P((void *));
    134 	void *kq_arg;
    135 };
    136 
    137 SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q);
    138 
    139 /*
    140  * Defer the creation of a kernel thread.  Once the standard kernel threads
    141  * and processes have been created, this queue will be run to callback to
    142  * the caller to create threads for e.g. file systems and device drivers.
    143  */
    144 void
    145 kthread_create(func, arg)
    146 	void (*func) __P((void *));
    147 	void *arg;
    148 {
    149 	struct kthread_q *kq;
    150 
    151 	if (kthread_create_now) {
    152 		(*func)(arg);
    153 		return;
    154 	}
    155 
    156 	kq = malloc(sizeof(*kq), M_TEMP, M_NOWAIT);
    157 	if (kq == NULL)
    158 		panic("unable to allocate kthread_q");
    159 	memset(kq, 0, sizeof(*kq));
    160 
    161 	kq->kq_func = func;
    162 	kq->kq_arg = arg;
    163 
    164 	SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q);
    165 }
    166 
    167 void
    168 kthread_run_deferred_queue()
    169 {
    170 	struct kthread_q *kq;
    171 
    172 	/* No longer need to defer kthread creation. */
    173 	kthread_create_now = 1;
    174 
    175 	while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) {
    176 		SIMPLEQ_REMOVE_HEAD(&kthread_q, kq, kq_q);
    177 		(*kq->kq_func)(kq->kq_arg);
    178 		free(kq, M_TEMP);
    179 	}
    180 }
    181