kern_kthread.c revision 1.24.10.2.4.1 1 /* $NetBSD: kern_kthread.c,v 1.24.10.2.4.1 2011/12/27 16:35:13 matt Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2007, 2009 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, and by Andrew Doran.
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.24.10.2.4.1 2011/12/27 16:35:13 matt Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/mutex.h>
41 #include <sys/sched.h>
42 #include <sys/kmem.h>
43
44 #include <uvm/uvm_extern.h>
45 #include <machine/stdarg.h>
46
47 static lwp_t * kthread_jtarget;
48 static kmutex_t kthread_lock;
49 static kcondvar_t kthread_cv;
50
51 void
52 kthread_sysinit(void)
53 {
54
55 mutex_init(&kthread_lock, MUTEX_DEFAULT, IPL_NONE);
56 cv_init(&kthread_cv, "kthrwait");
57 kthread_jtarget = NULL;
58 }
59
60 /*
61 * kthread_create: create a kernel thread, that is, system-only LWP.
62 */
63 int
64 kthread_create(pri_t pri, int flag, struct cpu_info *ci,
65 void (*func)(void *), void *arg, lwp_t **lp, const char *fmt, ...)
66 {
67 lwp_t *l;
68 vaddr_t uaddr;
69 bool inmem;
70 int error, lc;
71 va_list ap;
72
73 KASSERT((flag & KTHREAD_INTR) == 0 || (flag & KTHREAD_MPSAFE) != 0);
74
75 inmem = uvm_uarea_alloc(&uaddr);
76 if (uaddr == 0)
77 return ENOMEM;
78 if ((flag & KTHREAD_TS) != 0) {
79 lc = SCHED_OTHER;
80 } else {
81 lc = SCHED_RR;
82 }
83 error = lwp_create(&lwp0, &proc0, uaddr, inmem, LWP_DETACHED, NULL,
84 0, func, arg, &l, lc);
85 if (error) {
86 uvm_uarea_free(uaddr, curcpu());
87 return error;
88 }
89 uvm_lwp_hold(l);
90 if (fmt != NULL) {
91 l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
92 if (l->l_name == NULL) {
93 kthread_destroy(l);
94 return ENOMEM;
95 }
96 va_start(ap, fmt);
97 vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
98 va_end(ap);
99 }
100
101 /*
102 * Set parameters.
103 */
104 if (pri == PRI_NONE) {
105 if ((flag & KTHREAD_TS) != 0) {
106 /* Maximum user priority level. */
107 pri = MAXPRI_USER;
108 } else {
109 /* Minimum kernel priority level. */
110 pri = PRI_KTHREAD;
111 }
112 }
113 mutex_enter(proc0.p_lock);
114 lwp_lock(l);
115 l->l_priority = pri;
116 if (ci != NULL) {
117 if (ci != l->l_cpu) {
118 lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
119 lwp_lock(l);
120 }
121 l->l_pflag |= LP_BOUND;
122 l->l_cpu = ci;
123 }
124
125 if ((flag & KTHREAD_JOINABLE) != 0) {
126 KASSERT(lp != NULL);
127 l->l_pflag |= LP_JOINABLE;
128 }
129 if ((flag & KTHREAD_INTR) != 0) {
130 l->l_pflag |= LP_INTR;
131 }
132 if ((flag & KTHREAD_MPSAFE) == 0) {
133 l->l_pflag &= ~LP_MPSAFE;
134 }
135
136 /*
137 * Set the new LWP running, unless the caller has requested
138 * otherwise.
139 */
140 if ((flag & KTHREAD_IDLE) == 0) {
141 l->l_stat = LSRUN;
142 sched_enqueue(l, false);
143 lwp_unlock(l);
144 } else
145 lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
146
147 /*
148 * The LWP is not created suspended or stopped and cannot be set
149 * into those states later, so must be considered runnable.
150 */
151 proc0.p_nrlwps++;
152 mutex_exit(proc0.p_lock);
153
154 /* All done! */
155 if (lp != NULL) {
156 *lp = l;
157 }
158 return 0;
159 }
160
161 /*
162 * Cause a kernel thread to exit. Assumes the exiting thread is the
163 * current context.
164 */
165 void
166 kthread_exit(int ecode)
167 {
168 const char *name;
169 lwp_t *l = curlwp;
170
171 /* We can't do much with the exit code, so just report it. */
172 if (ecode != 0) {
173 if ((name = l->l_name) == NULL)
174 name = "unnamed";
175 printf("WARNING: kthread `%s' (%d) exits with status %d\n",
176 name, l->l_lid, ecode);
177 }
178
179 /* Barrier for joining. */
180 if (l->l_pflag & LP_JOINABLE) {
181 mutex_enter(&kthread_lock);
182 while (kthread_jtarget != l) {
183 cv_wait(&kthread_cv, &kthread_lock);
184 }
185 kthread_jtarget = NULL;
186 cv_broadcast(&kthread_cv);
187 mutex_exit(&kthread_lock);
188 }
189
190 /* And exit.. */
191 lwp_exit(l);
192 KASSERT(false);
193 }
194
195 /*
196 * Destroy an inactive kthread. The kthread must be in the LSIDL state.
197 */
198 void
199 kthread_destroy(lwp_t *l)
200 {
201
202 KASSERT((l->l_flag & LW_SYSTEM) != 0);
203 KASSERT(l->l_stat == LSIDL);
204
205 lwp_exit(l);
206 }
207
208 /*
209 * Wait for a kthread to exit, as pthread_join().
210 */
211 int
212 kthread_join(lwp_t *l)
213 {
214
215 KASSERT((l->l_flag & LW_SYSTEM) != 0);
216
217 /*
218 * - Wait if some other thread has occupied the target.
219 * - Speicfy our kthread as a target and notify it.
220 * - Wait for the target kthread to notify us.
221 */
222 mutex_enter(&kthread_lock);
223 while (kthread_jtarget) {
224 cv_wait(&kthread_cv, &kthread_lock);
225 }
226 kthread_jtarget = l;
227 cv_broadcast(&kthread_cv);
228 while (kthread_jtarget == l) {
229 cv_wait(&kthread_cv, &kthread_lock);
230 }
231 mutex_exit(&kthread_lock);
232
233 return 0;
234 }
235