xref: /netbsd/sys/kern/kern_kthread.c (revision 6550d01e)
1 /*	$NetBSD: kern_kthread.c,v 1.30 2010/06/13 04:13:31 yamt 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.30 2010/06/13 04:13:31 yamt 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/proc.h>
41 #include <sys/sched.h>
42 #include <sys/kmem.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 /*
47  * note that stdarg.h and the ansi style va_start macro is used for both
48  * ansi and traditional c complers.
49  * XXX: this requires that stdarg.h define: va_alist and va_dcl
50  */
51 #include <machine/stdarg.h>
52 
53 /*
54  * Fork a kernel thread.  Any process can request this to be done.
55  *
56  * With joinable kthreads KTHREAD_JOINABLE flag this should be known.
57  * 1. If you specify KTHREAD_JOINABLE, you must call kthread_join() to reap
58  *    the thread. It will not be automatically reaped by the system.
59  * 2. For any given call to kthread_create(KTHREAD_JOINABLE), you may call
60  *    kthread_join() only once on the returned lwp_t *.
61  */
62 int
63 kthread_create(pri_t pri, int flag, struct cpu_info *ci,
64 	       void (*func)(void *), void *arg,
65 	       lwp_t **lp, const char *fmt, ...)
66 {
67 	lwp_t *l;
68 	vaddr_t uaddr;
69 	int error, lc, lwp_flags;
70 	va_list ap;
71 
72 	lwp_flags = LWP_DETACHED;
73 
74 	uaddr = uvm_uarea_alloc();
75 	if (uaddr == 0) {
76 		return ENOMEM;
77 	}
78 	if ((flag & KTHREAD_TS) != 0) {
79 		lc = SCHED_OTHER;
80 	} else {
81 		lc = SCHED_RR;
82 	}
83 
84 	if ((flag & KTHREAD_JOINABLE) != 0) {
85 		lwp_flags &= ~LWP_DETACHED;
86 	}
87 
88 	error = lwp_create(&lwp0, &proc0, uaddr, lwp_flags, NULL,
89 	    0, func, arg, &l, lc);
90 	if (error) {
91 		uvm_uarea_free(uaddr);
92 		return error;
93 	}
94 	if (fmt != NULL) {
95 		l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
96 		if (l->l_name == NULL) {
97 			kthread_destroy(l);
98 			return ENOMEM;
99 		}
100 		va_start(ap, fmt);
101 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
102 		va_end(ap);
103 	}
104 
105 	/*
106 	 * Set parameters.
107 	 */
108 	if ((flag & KTHREAD_INTR) != 0) {
109 		KASSERT((flag & KTHREAD_MPSAFE) != 0);
110 	}
111 
112 	/* Joinable kthread can't be NULL. */
113 	if ((flag & KTHREAD_JOINABLE) != 0) {
114 		KASSERT(l != NULL);
115 	}
116 
117 	if (pri == PRI_NONE) {
118 		if ((flag & KTHREAD_TS) != 0) {
119 			/* Maximum user priority level. */
120 			pri = MAXPRI_USER;
121 		} else {
122 			/* Minimum kernel priority level. */
123 			pri = PRI_KTHREAD;
124 		}
125 	}
126 	mutex_enter(proc0.p_lock);
127 	lwp_lock(l);
128 	l->l_priority = pri;
129 	if (ci != NULL) {
130 		if (ci != l->l_cpu) {
131 			lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
132 			lwp_lock(l);
133 		}
134 		l->l_pflag |= LP_BOUND;
135 		l->l_cpu = ci;
136 	}
137 	if ((flag & KTHREAD_INTR) != 0)
138 		l->l_pflag |= LP_INTR;
139 	if ((flag & KTHREAD_MPSAFE) == 0)
140 		l->l_pflag &= ~LP_MPSAFE;
141 
142 	/*
143 	 * Set the new LWP running, unless the caller has requested
144 	 * otherwise.
145 	 */
146 	if ((flag & KTHREAD_IDLE) == 0) {
147 		l->l_stat = LSRUN;
148 		sched_enqueue(l, false);
149 		lwp_unlock(l);
150 	} else
151 		lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
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 	/* And exit.. */
180 	lwp_exit(l);
181 
182 	/*
183 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
184 	 * XXX of worms right now.
185 	 */
186 	for (;;)
187 		;
188 }
189 
190 /*
191  * Destroy an inactive kthread.  The kthread must be in the LSIDL state.
192  */
193 void
194 kthread_destroy(lwp_t *l)
195 {
196 	proc_t *p;
197 
198 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
199 	KASSERT(l->l_stat == LSIDL);
200 
201 	p = l->l_proc;
202 
203 	/* Add LRP_DETACHED flag because we can have joinable kthread now. */
204 	mutex_enter(p->p_lock);
205 	l->l_prflag |= LPR_DETACHED;
206 	mutex_exit(p->p_lock);
207 
208 	lwp_exit(l);
209 }
210 
211 /*
212  * Wait for a kthread to exit, as pthread_join().
213  */
214 int
215 kthread_join(lwp_t *l)
216 {
217 	lwpid_t departed;
218 	proc_t *p;
219 	int error;
220 
221 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
222 	KASSERT((l->l_prflag & LPR_DETACHED) == 0);
223 
224 	p = l->l_proc;
225 
226 	mutex_enter(p->p_lock);
227 	error = lwp_wait1(curlwp, l->l_lid, &departed, LWPWAIT_EXITCONTROL);
228 	mutex_exit(p->p_lock);
229 
230 	return error;
231 }
232