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