2 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/kern_kthread.c,v 1.5.2.3 2001/12/25 01:51:14 dillon Exp $
27 * $DragonFly: src/sys/kern/kern_kthread.c,v 1.13 2006/12/18 20:41:01 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kthread.h>
34 #include <sys/ptrace.h>
35 #include <sys/resourcevar.h>
36 #include <sys/signalvar.h>
37 #include <sys/unistd.h>
40 #include <machine/stdarg.h>
42 static struct lwkt_token kpsus_token = LWKT_TOKEN_INITIALIZER(kpsus_token);
46 * Create a new lightweight kernel thread.
49 _kthread_create(void (*func)(void *), void *arg,
50 struct thread **tdp, int cpu, const char *fmt, __va_list ap)
56 atomic_set_int(&flags, TDF_VERBOSE);
58 td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, cpu, flags);
61 cpu_set_thread_handler(td, kthread_exit, func, arg);
64 * Set up arg0 for 'ps' etc
66 kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
68 td->td_ucred = crhold(proc0.p_ucred);
71 * Schedule the thread to run
79 * Creates a lwkt. No CPU preference.
82 kthread_create(void (*func)(void *), void *arg,
83 struct thread **tdp, const char *fmt, ...)
89 ret = _kthread_create(func, arg, tdp, -1, fmt, ap);
96 * Creates a lwkt and schedule it to run in a specific CPU.
100 kthread_create_cpu(void (*func)(void *), void *arg,
101 struct thread **tdp, int cpu, const char *fmt, ...)
107 ret = _kthread_create(func, arg, tdp, cpu, fmt, ap);
115 * Same as kthread_create() but you can specify a custom stack size.
118 kthread_create_stk(void (*func)(void *), void *arg,
119 struct thread **tdp, int stksize, const char *fmt, ...)
124 td = lwkt_alloc_thread(NULL, stksize, -1, TDF_VERBOSE);
127 cpu_set_thread_handler(td, kthread_exit, func, arg);
130 kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
139 * Destroy an LWKT thread. Warning! This function is not called when
140 * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
141 * uses a different reaping mechanism.
143 * XXX duplicates lwkt_exit()
152 * Start a kernel process. This is called after a fork() call in
153 * mi_startup() in the file kern/init_main.c.
155 * This function is used to start "internal" daemons and intended
156 * to be called from SYSINIT().
158 * These threads are created MPSAFE.
161 kproc_start(const void *udata)
163 const struct kproc_desc *kp = udata;
166 error = kthread_create((void (*)(void *))kp->func, NULL,
167 kp->global_threadpp, kp->arg0);
168 lwkt_setpri(*kp->global_threadpp, TDPRI_KERN_DAEMON);
170 panic("kproc_start: %s: error %d", kp->arg0, error);
174 * Advise a kernel process to suspend (or resume) in its main loop.
175 * Participation is voluntary.
178 suspend_kproc(struct thread *td, int timo)
180 if (td->td_proc == NULL) {
181 lwkt_gettoken(&kpsus_token);
182 /* request thread pause */
183 atomic_set_int(&td->td_mpflags, TDF_MP_STOPREQ);
185 while (td->td_mpflags & TDF_MP_STOPREQ) {
186 int error = tsleep(td, 0, "suspkp", timo);
187 if (error == EWOULDBLOCK)
190 atomic_clear_int(&td->td_mpflags, TDF_MP_STOPREQ);
191 lwkt_reltoken(&kpsus_token);
194 return(EINVAL); /* not a kernel thread */
199 kproc_suspend_loop(void)
201 struct thread *td = curthread;
203 if (td->td_mpflags & TDF_MP_STOPREQ) {
204 lwkt_gettoken(&kpsus_token);
205 atomic_clear_int(&td->td_mpflags, TDF_MP_STOPREQ);
206 while ((td->td_mpflags & TDF_MP_WAKEREQ) == 0) {
208 tsleep(td, 0, "kpsusp", 0);
210 atomic_clear_int(&td->td_mpflags, TDF_MP_WAKEREQ);
212 lwkt_reltoken(&kpsus_token);