kern_kthread.c revision 1.16
1/* $NetBSD: kern_kthread.c,v 1.16 2007/02/09 21:55:30 ad 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/cdefs.h> 41__KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.16 2007/02/09 21:55:30 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/* 53 * note that stdarg.h and the ansi style va_start macro is used for both 54 * ansi and traditional c complers. 55 * XXX: this requires that stdarg.h define: va_alist and va_dcl 56 */ 57#include <machine/stdarg.h> 58 59int kthread_create_now; 60 61/* 62 * Fork a kernel thread. Any process can request this to be done. 63 * The VM space and limits, etc. will be shared with proc0. 64 */ 65int 66kthread_create1(void (*func)(void *), void *arg, 67 struct proc **newpp, const char *fmt, ...) 68{ 69 struct proc *p2; 70 int error; 71 va_list ap; 72 73 /* First, create the new process. */ 74 error = fork1(&lwp0, FORK_SHAREVM | FORK_SHARECWD | FORK_SHAREFILES | 75 FORK_SHARESIGS | FORK_SYSTEM, SIGCHLD, NULL, 0, func, arg, NULL, 76 &p2); 77 if (__predict_false(error != 0)) 78 return (error); 79 80 /* Name it as specified. */ 81 va_start(ap, fmt); 82 vsnprintf(p2->p_comm, MAXCOMLEN, fmt, ap); 83 va_end(ap); 84 85 /* All done! */ 86 if (newpp != NULL) 87 *newpp = p2; 88 return (0); 89} 90 91/* 92 * Cause a kernel thread to exit. Assumes the exiting thread is the 93 * current context. 94 */ 95void 96kthread_exit(int ecode) 97{ 98 struct proc *p = curproc; 99 100 /* 101 * XXX What do we do with the exit code? Should we even bother 102 * XXX with it? The parent (proc0) isn't going to do much with 103 * XXX it. 104 */ 105 if (ecode != 0) 106 printf("WARNING: thread `%s' (%d) exits with status %d\n", 107 p->p_comm, p->p_pid, ecode); 108 109 /* Acquire the sched state mutex. exit1() will release it. */ 110 mutex_enter(&p->p_smutex); 111 exit1(curlwp, W_EXITCODE(ecode, 0)); 112 113 /* 114 * XXX Fool the compiler. Making exit1() __noreturn__ is a can 115 * XXX of worms right now. 116 */ 117 for (;;); 118} 119 120struct kthread_q { 121 SIMPLEQ_ENTRY(kthread_q) kq_q; 122 void (*kq_func)(void *); 123 void *kq_arg; 124}; 125 126SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q); 127 128/* 129 * Defer the creation of a kernel thread. Once the standard kernel threads 130 * and processes have been created, this queue will be run to callback to 131 * the caller to create threads for e.g. file systems and device drivers. 132 */ 133void 134kthread_create(void (*func)(void *), void *arg) 135{ 136 struct kthread_q *kq; 137 138 if (kthread_create_now) { 139 (*func)(arg); 140 return; 141 } 142 143 kq = malloc(sizeof(*kq), M_TEMP, M_NOWAIT); 144 if (kq == NULL) 145 panic("unable to allocate kthread_q"); 146 memset(kq, 0, sizeof(*kq)); 147 148 kq->kq_func = func; 149 kq->kq_arg = arg; 150 151 SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q); 152} 153 154void 155kthread_run_deferred_queue(void) 156{ 157 struct kthread_q *kq; 158 159 /* No longer need to defer kthread creation. */ 160 kthread_create_now = 1; 161 162 while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) { 163 SIMPLEQ_REMOVE_HEAD(&kthread_q, kq_q); 164 (*kq->kq_func)(kq->kq_arg); 165 free(kq, M_TEMP); 166 } 167} 168