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
27 #include <sys/param.h>
28 #include <sys/systm.h>
30 #include <sys/kthread.h>
31 #include <sys/ptrace.h>
32 #include <sys/resourcevar.h>
33 #include <sys/signalvar.h>
34 #include <sys/unistd.h>
37 #include <machine/stdarg.h>
39 static struct lwkt_token kpsus_token
= LWKT_TOKEN_INITIALIZER(kpsus_token
);
43 * Create a new lightweight kernel thread.
45 static int __printflike(6, 0)
46 _kthread_create(void (*func
)(void *), void *arg
,
47 struct thread
**tdp
, int cpu
, bool schedule_now
, const char *fmt
, __va_list ap
)
52 td
= lwkt_alloc_thread(NULL
, LWKT_THREAD_STACK
, cpu
, flags
);
55 cpu_set_thread_handler(td
, kthread_exit
, func
, arg
);
58 * Set up arg0 for 'ps' etc
60 kvsnprintf(td
->td_comm
, sizeof(td
->td_comm
), fmt
, ap
);
62 td
->td_ucred
= crhold(proc0
.p_ucred
);
65 * Schedule the thread to run
73 /* Create a new lightweight kernel thread and do not schedule it */
75 kthread_alloc(void (*func
)(void *), void *arg
,
76 struct thread
**tdp
, const char *fmt
, ...)
82 ret
= _kthread_create(func
, arg
, tdp
, -1, false, fmt
, ap
);
89 * Creates a lwkt. No CPU preference.
92 kthread_create(void (*func
)(void *), void *arg
,
93 struct thread
**tdp
, const char *fmt
, ...)
99 ret
= _kthread_create(func
, arg
, tdp
, -1, true, fmt
, ap
);
106 * Creates a lwkt and schedule it to run in a specific CPU.
110 kthread_create_cpu(void (*func
)(void *), void *arg
,
111 struct thread
**tdp
, int cpu
, const char *fmt
, ...)
117 ret
= _kthread_create(func
, arg
, tdp
, cpu
, true, fmt
, ap
);
125 * Same as kthread_create() but you can specify a custom stack size.
128 kthread_create_stk(void (*func
)(void *), void *arg
,
129 struct thread
**tdp
, int stksize
, const char *fmt
, ...)
134 td
= lwkt_alloc_thread(NULL
, stksize
, -1, 0);
137 cpu_set_thread_handler(td
, kthread_exit
, func
, arg
);
140 kvsnprintf(td
->td_comm
, sizeof(td
->td_comm
), fmt
, ap
);
149 * Destroy an LWKT thread. Warning! This function is not called when
150 * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
151 * uses a different reaping mechanism.
153 * XXX duplicates lwkt_exit()
162 * Start a kernel process. This is called after a fork() call in
163 * mi_startup() in the file kern/init_main.c.
165 * This function is used to start "internal" daemons and intended
166 * to be called from SYSINIT().
168 * These threads are created MPSAFE.
171 kproc_start(const void *udata
)
173 const struct kproc_desc
*kp
= udata
;
176 error
= kthread_create((void (*)(void *))kp
->func
, NULL
,
177 kp
->global_threadpp
, "%s", kp
->arg0
);
178 lwkt_setpri(*kp
->global_threadpp
, TDPRI_KERN_DAEMON
);
180 panic("kproc_start: %s: error %d", kp
->arg0
, error
);
184 * Advise a kernel process to suspend (or resume) in its main loop.
185 * Participation is voluntary.
188 suspend_kproc(struct thread
*td
, int timo
)
190 if (td
->td_proc
== NULL
) {
191 lwkt_gettoken(&kpsus_token
);
192 /* request thread pause */
193 atomic_set_int(&td
->td_mpflags
, TDF_MP_STOPREQ
);
195 while (td
->td_mpflags
& TDF_MP_STOPREQ
) {
196 int error
= tsleep(td
, 0, "suspkp", timo
);
197 if (error
== EWOULDBLOCK
)
200 atomic_clear_int(&td
->td_mpflags
, TDF_MP_STOPREQ
);
201 lwkt_reltoken(&kpsus_token
);
204 return(EINVAL
); /* not a kernel thread */
209 kproc_suspend_loop(void)
211 struct thread
*td
= curthread
;
213 if (td
->td_mpflags
& TDF_MP_STOPREQ
) {
214 lwkt_gettoken(&kpsus_token
);
215 atomic_clear_int(&td
->td_mpflags
, TDF_MP_STOPREQ
);
216 while ((td
->td_mpflags
& TDF_MP_WAKEREQ
) == 0) {
218 tsleep(td
, 0, "kpsusp", 0);
220 atomic_clear_int(&td
->td_mpflags
, TDF_MP_WAKEREQ
);
222 lwkt_reltoken(&kpsus_token
);