Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.10
      1  1.10  thorpej /*	$NetBSD: kern_kthread.c,v 1.10 2000/05/28 05:49:06 thorpej Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4   1.5  thorpej  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7   1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1  thorpej  * NASA Ames Research Center.
     10   1.1  thorpej  *
     11   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.1  thorpej  * modification, are permitted provided that the following conditions
     13   1.1  thorpej  * are met:
     14   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20   1.1  thorpej  *    must display the following acknowledgement:
     21   1.1  thorpej  *	This product includes software developed by the NetBSD
     22   1.1  thorpej  *	Foundation, Inc. and its contributors.
     23   1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25   1.1  thorpej  *    from this software without specific prior written permission.
     26   1.1  thorpej  *
     27   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  thorpej  */
     39   1.1  thorpej 
     40   1.1  thorpej #include <sys/param.h>
     41   1.1  thorpej #include <sys/systm.h>
     42   1.1  thorpej #include <sys/kernel.h>
     43   1.1  thorpej #include <sys/kthread.h>
     44   1.1  thorpej #include <sys/proc.h>
     45   1.1  thorpej #include <sys/wait.h>
     46   1.2  thorpej #include <sys/malloc.h>
     47   1.2  thorpej #include <sys/queue.h>
     48   1.1  thorpej 
     49   1.1  thorpej /*
     50   1.1  thorpej  * note that stdarg.h and the ansi style va_start macro is used for both
     51   1.1  thorpej  * ansi and traditional c complers.
     52   1.1  thorpej  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     53   1.1  thorpej  */
     54   1.1  thorpej #include <machine/stdarg.h>
     55   1.1  thorpej 
     56   1.8  thorpej int	kthread_create_now;
     57   1.8  thorpej 
     58   1.1  thorpej /*
     59   1.1  thorpej  * Fork a kernel thread.  Any process can request this to be done.
     60   1.1  thorpej  * The VM space and limits, etc. will be shared with proc0.
     61   1.1  thorpej  */
     62   1.1  thorpej int
     63   1.3   kleink #if __STDC__
     64   1.8  thorpej kthread_create1(void (*func)(void *), void *arg,
     65   1.1  thorpej     struct proc **newpp, const char *fmt, ...)
     66   1.1  thorpej #else
     67   1.8  thorpej kthread_create1(func, arg, newpp, fmt, va_alist)
     68   1.1  thorpej 	void (*func) __P((void *));
     69   1.1  thorpej 	void *arg;
     70   1.1  thorpej 	struct proc **newpp;
     71   1.1  thorpej 	const char *fmt;
     72   1.1  thorpej 	va_dcl
     73   1.1  thorpej #endif
     74   1.1  thorpej {
     75   1.1  thorpej 	struct proc *p2;
     76   1.1  thorpej 	int error;
     77   1.1  thorpej 	va_list ap;
     78   1.1  thorpej 
     79   1.1  thorpej 	/* First, create the new process. */
     80   1.5  thorpej 	error = fork1(&proc0, FORK_SHAREVM | FORK_SHARECWD | FORK_SHAREFILES |
     81  1.10  thorpej 	    FORK_SHARESIGS, SIGCHLD, NULL, 0, func, arg, NULL, &p2);
     82   1.9  thorpej 	if (__predict_false(error != 0))
     83   1.1  thorpej 		return (error);
     84   1.1  thorpej 
     85   1.1  thorpej 	/*
     86   1.1  thorpej 	 * Mark it as a system process and not a candidate for
     87   1.1  thorpej 	 * swapping.  Set P_NOCLDWAIT so that children are reparented
     88   1.1  thorpej 	 * to init(8) when they exit.  init(8) can easily wait them
     89   1.1  thorpej 	 * out for us.
     90   1.1  thorpej 	 */
     91   1.1  thorpej 	p2->p_flag |= P_INMEM | P_SYSTEM | P_NOCLDWAIT;	/* XXX */
     92   1.1  thorpej 
     93   1.1  thorpej 	/* Name it as specified. */
     94   1.1  thorpej 	va_start(ap, fmt);
     95   1.4      gwr 	vsnprintf(p2->p_comm, MAXCOMLEN, fmt, ap);
     96   1.1  thorpej 	va_end(ap);
     97   1.1  thorpej 
     98   1.1  thorpej 	/* All done! */
     99   1.1  thorpej 	if (newpp != NULL)
    100   1.1  thorpej 		*newpp = p2;
    101   1.1  thorpej 	return (0);
    102   1.1  thorpej }
    103   1.1  thorpej 
    104   1.1  thorpej /*
    105   1.1  thorpej  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    106   1.1  thorpej  * current context.
    107   1.1  thorpej  */
    108   1.1  thorpej void
    109   1.1  thorpej kthread_exit(ecode)
    110   1.1  thorpej 	int ecode;
    111   1.1  thorpej {
    112   1.1  thorpej 
    113   1.1  thorpej 	/*
    114   1.1  thorpej 	 * XXX What do we do with the exit code?  Should we even bother
    115   1.1  thorpej 	 * XXX with it?  The parent (proc0) isn't going to do much with
    116   1.1  thorpej 	 * XXX it.
    117   1.1  thorpej 	 */
    118   1.1  thorpej 	if (ecode != 0)
    119   1.1  thorpej 		printf("WARNING: thread `%s' (%d) exits with status %d\n",
    120   1.1  thorpej 		    curproc->p_comm, curproc->p_pid, ecode);
    121   1.1  thorpej 
    122   1.1  thorpej 	exit1(curproc, W_EXITCODE(ecode, 0));
    123   1.1  thorpej 
    124   1.1  thorpej 	/*
    125   1.1  thorpej 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
    126   1.1  thorpej 	 * XXX of worms right now.
    127   1.1  thorpej 	 */
    128   1.1  thorpej 	for (;;);
    129   1.2  thorpej }
    130   1.2  thorpej 
    131   1.2  thorpej struct kthread_q {
    132   1.2  thorpej 	SIMPLEQ_ENTRY(kthread_q) kq_q;
    133   1.2  thorpej 	void (*kq_func) __P((void *));
    134   1.2  thorpej 	void *kq_arg;
    135   1.2  thorpej };
    136   1.2  thorpej 
    137   1.2  thorpej SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q);
    138   1.2  thorpej 
    139   1.2  thorpej /*
    140   1.2  thorpej  * Defer the creation of a kernel thread.  Once the standard kernel threads
    141   1.2  thorpej  * and processes have been created, this queue will be run to callback to
    142   1.2  thorpej  * the caller to create threads for e.g. file systems and device drivers.
    143   1.2  thorpej  */
    144   1.2  thorpej void
    145   1.8  thorpej kthread_create(func, arg)
    146   1.2  thorpej 	void (*func) __P((void *));
    147   1.2  thorpej 	void *arg;
    148   1.2  thorpej {
    149   1.2  thorpej 	struct kthread_q *kq;
    150   1.2  thorpej 
    151   1.8  thorpej 	if (kthread_create_now) {
    152   1.8  thorpej 		(*func)(arg);
    153   1.8  thorpej 		return;
    154   1.8  thorpej 	}
    155   1.8  thorpej 
    156   1.2  thorpej 	kq = malloc(sizeof(*kq), M_TEMP, M_NOWAIT);
    157   1.2  thorpej 	if (kq == NULL)
    158   1.2  thorpej 		panic("unable to allocate kthread_q");
    159   1.2  thorpej 	memset(kq, 0, sizeof(*kq));
    160   1.2  thorpej 
    161   1.2  thorpej 	kq->kq_func = func;
    162   1.2  thorpej 	kq->kq_arg = arg;
    163   1.2  thorpej 
    164   1.2  thorpej 	SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q);
    165   1.2  thorpej }
    166   1.2  thorpej 
    167   1.2  thorpej void
    168   1.2  thorpej kthread_run_deferred_queue()
    169   1.2  thorpej {
    170   1.2  thorpej 	struct kthread_q *kq;
    171   1.8  thorpej 
    172   1.8  thorpej 	/* No longer need to defer kthread creation. */
    173   1.8  thorpej 	kthread_create_now = 1;
    174   1.2  thorpej 
    175   1.2  thorpej 	while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) {
    176   1.2  thorpej 		SIMPLEQ_REMOVE_HEAD(&kthread_q, kq, kq_q);
    177   1.2  thorpej 		(*kq->kq_func)(kq->kq_arg);
    178   1.2  thorpej 		free(kq, M_TEMP);
    179   1.2  thorpej 	}
    180   1.1  thorpej }
    181