1 /* $OpenBSD: kern_kthread.c,v 1.23 2004/11/23 19:08:55 miod 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 compilers.
55 * XXX: this requires that stdarg.h define: va_alist and va_dcl
56 */
57 #include <sys/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
kthread_create(void (* func)(void *),void * arg,struct proc ** newpp,const char * fmt,...)64 kthread_create(void (*func)(void *), void *arg,
65 struct proc **newpp, const char *fmt, ...)
66 {
67 struct proc *p2;
68 int error;
69 va_list ap;
70
71 /*
72 * First, create the new process. Share the memory, file
73 * descriptors and don't leave the exit status around for the
74 * parent to wait for.
75 */
76 error = fork1(&proc0, 0, FORK_SHAREVM |FORK_NOZOMBIE |FORK_SIGHAND,
77 NULL, 0, func, arg, NULL, &p2);
78 if (error)
79 return (error);
80
81 /*
82 * Mark it as a system process and not a candidate for
83 * swapping.
84 */
85 p2->p_flag |= P_INMEM | P_SYSTEM; /* XXX */
86
87 /* Name it as specified. */
88 va_start(ap, fmt);
89 vsnprintf(p2->p_comm, sizeof p2->p_comm, fmt, ap);
90 va_end(ap);
91
92 /* All done! */
93 if (newpp != NULL)
94 *newpp = p2;
95 return (0);
96 }
97
98 /*
99 * Cause a kernel thread to exit. Assumes the exiting thread is the
100 * current context.
101 */
102 void
kthread_exit(int ecode)103 kthread_exit(int ecode)
104 {
105
106 /*
107 * XXX What do we do with the exit code? Should we even bother
108 * XXX with it? The parent (proc0) isn't going to do much with
109 * XXX it.
110 */
111 if (ecode != 0)
112 printf("WARNING: thread `%s' (%d) exits with status %d\n",
113 curproc->p_comm, curproc->p_pid, ecode);
114
115 exit1(curproc, W_EXITCODE(ecode, 0));
116
117 /*
118 * XXX Fool the compiler. Making exit1() __dead is a can
119 * XXX of worms right now.
120 */
121 for (;;);
122 }
123
124 struct kthread_q {
125 SIMPLEQ_ENTRY(kthread_q) kq_q;
126 void (*kq_func)(void *);
127 void *kq_arg;
128 };
129
130 SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q);
131
132 /*
133 * Defer the creation of a kernel thread. Once the standard kernel threads
134 * and processes have been created, this queue will be run to callback to
135 * the caller to create threads for e.g. file systems and device drivers.
136 */
137 void
kthread_create_deferred(void (* func)(void *),void * arg)138 kthread_create_deferred(void (*func)(void *), void *arg)
139 {
140 struct kthread_q *kq;
141
142 kq = malloc(sizeof *kq, M_TEMP, M_NOWAIT);
143 if (kq == NULL)
144 panic("unable to allocate kthread_q");
145 bzero(kq, sizeof *kq);
146
147 kq->kq_func = func;
148 kq->kq_arg = arg;
149
150 SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q);
151 }
152
153 void
kthread_run_deferred_queue(void)154 kthread_run_deferred_queue(void)
155 {
156 struct kthread_q *kq;
157
158 while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) {
159 SIMPLEQ_REMOVE_HEAD(&kthread_q, kq_q);
160 (*kq->kq_func)(kq->kq_arg);
161 free(kq, M_TEMP);
162 }
163 }
164