xref: /dragonfly/sys/kern/kern_kthread.c (revision 86fe9e07)
1 /*
2  * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
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.10 2004/07/29 09:02:33 dillon Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/proc.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>
38 #include <sys/wait.h>
39 
40 #include <machine/stdarg.h>
41 
42 /*
43  * Create a kernel process/thread/whatever.  It shares it's address space
44  * with proc0 - ie: kernel only.  5.x compatible.
45  *
46  * NOTE!  By default kthreads are created with the MP lock held.  A
47  * thread which does not require the MP lock should release it by calling
48  * rel_mplock() at the start of the new thread.
49  */
50 int
51 kthread_create(void (*func)(void *), void *arg,
52     struct thread **tdp, const char *fmt, ...)
53 {
54     thread_t td;
55     __va_list ap;
56 
57     td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, -1);
58     if (tdp)
59 	*tdp = td;
60     cpu_set_thread_handler(td, kthread_exit, func, arg);
61     td->td_flags |= TDF_VERBOSE;
62 #ifdef SMP
63     td->td_mpcount = 1;
64 #endif
65 
66     /*
67      * Set up arg0 for 'ps' etc
68      */
69     __va_start(ap, fmt);
70     vsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
71     __va_end(ap);
72 
73     /*
74      * Schedule the thread to run
75      */
76     lwkt_schedule(td);
77     return 0;
78 }
79 
80 /*
81  * Same as kthread_create() but you can specify a custom stack size.
82  */
83 int
84 kthread_create_stk(void (*func)(void *), void *arg,
85     struct thread **tdp, int stksize, const char *fmt, ...)
86 {
87     thread_t td;
88     __va_list ap;
89 
90     td = lwkt_alloc_thread(NULL, stksize, -1);
91     if (tdp)
92 	*tdp = td;
93     cpu_set_thread_handler(td, kthread_exit, func, arg);
94     td->td_flags |= TDF_VERBOSE;
95 #ifdef SMP
96     td->td_mpcount = 1;
97 #endif
98     __va_start(ap, fmt);
99     vsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
100     __va_end(ap);
101 
102     lwkt_schedule(td);
103     return 0;
104 }
105 
106 /*
107  * Destroy an LWKT thread.   Warning!  This function is not called when
108  * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
109  * uses a different reaping mechanism.
110  *
111  * XXX duplicates lwkt_exit()
112  */
113 void
114 kthread_exit(void)
115 {
116     lwkt_exit();
117 }
118 
119 
120 /*
121  * Start a kernel process.  This is called after a fork() call in
122  * mi_startup() in the file kern/init_main.c.
123  *
124  * This function is used to start "internal" daemons and intended
125  * to be called from SYSINIT().
126  */
127 void
128 kproc_start(udata)
129 	const void *udata;
130 {
131 	const struct kproc_desc	*kp = udata;
132 	int error;
133 
134 	error = kthread_create((void (*)(void *))kp->func, NULL,
135 		    kp->global_threadpp, kp->arg0);
136 	lwkt_setpri(*kp->global_threadpp, TDPRI_KERN_DAEMON);
137 	if (error)
138 		panic("kproc_start: %s: error %d", kp->arg0, error);
139 }
140 
141 /*
142  * Advise a kernel process to suspend (or resume) in its main loop.
143  * Participation is voluntary.
144  */
145 int
146 suspend_kproc(struct thread *td, int timo)
147 {
148 	if (td->td_proc == NULL) {
149 		td->td_flags |= TDF_STOPREQ;	/* request thread pause */
150 		wakeup(td);
151 		while (td->td_flags & TDF_STOPREQ) {
152 			int error = tsleep(td, 0, "suspkp", timo);
153 			if (error == EWOULDBLOCK)
154 				break;
155 		}
156 		td->td_flags &= ~TDF_STOPREQ;
157 		return(0);
158 	} else {
159 		return(EINVAL);	/* not a kernel thread */
160 	}
161 }
162 
163 void
164 kproc_suspend_loop(void)
165 {
166 	struct thread *td = curthread;
167 
168 	if (td->td_flags & TDF_STOPREQ) {
169 		td->td_flags &= ~TDF_STOPREQ;
170 		while ((td->td_flags & TDF_WAKEREQ) == 0) {
171 			wakeup(td);
172 			tsleep(td, 0, "kpsusp", 0);
173 		}
174 		td->td_flags &= ~TDF_WAKEREQ;
175 		wakeup(td);
176 	}
177 }
178 
179