kernel - unwind kthread_create() mplock
[dragonfly.git] / sys / kern / kern_kthread.c
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.13 2006/12/18 20:41:01 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 <sys/mplock2.h>
41
42 #include <machine/stdarg.h>
43
44 static struct lwkt_token kpsus_token = LWKT_TOKEN_MP_INITIALIZER(kpsus_token);
45
46
47 /*
48  * Create a kernel process/thread/whatever.  It shares it's address space
49  * with proc0 - ie: kernel only.  5.x compatible.
50  *
51  * All kthreads are created as MPSAFE threads.
52  */
53 int
54 kthread_create(void (*func)(void *), void *arg,
55                struct thread **tdp, const char *fmt, ...)
56 {
57     thread_t td;
58     __va_list ap;
59
60     td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, -1,
61                            TDF_VERBOSE | TDF_MPSAFE);
62     if (tdp)
63         *tdp = td;
64     cpu_set_thread_handler(td, kthread_exit, func, arg);
65
66     /*
67      * Set up arg0 for 'ps' etc
68      */
69     __va_start(ap, fmt);
70     kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
71     __va_end(ap);
72
73     td->td_ucred = crhold(proc0.p_ucred);
74
75     /*
76      * Schedule the thread to run
77      */
78     lwkt_schedule(td);
79     return 0;
80 }
81
82 int
83 kthread_create_cpu(void (*func)(void *), void *arg,
84                    struct thread **tdp, int cpu, const char *fmt, ...)
85 {
86     thread_t td;
87     __va_list ap;
88
89     td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, cpu,
90                            TDF_VERBOSE | TDF_MPSAFE);
91     if (tdp)
92         *tdp = td;
93     cpu_set_thread_handler(td, kthread_exit, func, arg);
94
95     /*
96      * Set up arg0 for 'ps' etc
97      */
98     __va_start(ap, fmt);
99     kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
100     __va_end(ap);
101
102     td->td_ucred = crhold(proc0.p_ucred);
103
104     /*
105      * Schedule the thread to run
106      */
107     lwkt_schedule(td);
108     return 0;
109 }
110
111 /*
112  * Same as kthread_create() but you can specify a custom stack size.
113  */
114 int
115 kthread_create_stk(void (*func)(void *), void *arg,
116                    struct thread **tdp, int stksize, const char *fmt, ...)
117 {
118     thread_t td;
119     __va_list ap;
120
121     td = lwkt_alloc_thread(NULL, stksize, -1, TDF_VERBOSE | TDF_MPSAFE);
122     if (tdp)
123         *tdp = td;
124     cpu_set_thread_handler(td, kthread_exit, func, arg);
125
126     __va_start(ap, fmt);
127     kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
128     __va_end(ap);
129
130     lwkt_schedule(td);
131     return 0;
132 }
133
134 /*
135  * Destroy an LWKT thread.   Warning!  This function is not called when
136  * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
137  * uses a different reaping mechanism.
138  *
139  * XXX duplicates lwkt_exit()
140  */
141 void
142 kthread_exit(void)
143 {
144     lwkt_exit();
145 }
146
147 /*
148  * Start a kernel process.  This is called after a fork() call in
149  * mi_startup() in the file kern/init_main.c.
150  *
151  * This function is used to start "internal" daemons and intended
152  * to be called from SYSINIT().
153  *
154  * These threads are created MPSAFE.
155  */
156 void
157 kproc_start(const void *udata)
158 {
159         const struct kproc_desc *kp = udata;
160         int error;
161
162         error = kthread_create((void (*)(void *))kp->func, NULL,
163                                 kp->global_threadpp, kp->arg0);
164         lwkt_setpri(*kp->global_threadpp, TDPRI_KERN_DAEMON);
165         if (error)
166                 panic("kproc_start: %s: error %d", kp->arg0, error);
167 }
168
169 /*
170  * Advise a kernel process to suspend (or resume) in its main loop.
171  * Participation is voluntary.
172  */
173 int
174 suspend_kproc(struct thread *td, int timo)
175 {
176         if (td->td_proc == NULL) {
177                 lwkt_gettoken(&kpsus_token);
178                 td->td_flags |= TDF_STOPREQ;    /* request thread pause */
179                 wakeup(td);
180                 while (td->td_flags & TDF_STOPREQ) {
181                         int error = tsleep(td, 0, "suspkp", timo);
182                         if (error == EWOULDBLOCK)
183                                 break;
184                 }
185                 td->td_flags &= ~TDF_STOPREQ;
186                 lwkt_reltoken(&kpsus_token);
187                 return(0);
188         } else {
189                 return(EINVAL); /* not a kernel thread */
190         }
191 }
192
193 void
194 kproc_suspend_loop(void)
195 {
196         struct thread *td = curthread;
197
198         if (td->td_flags & TDF_STOPREQ) {
199                 lwkt_gettoken(&kpsus_token);
200                 td->td_flags &= ~TDF_STOPREQ;
201                 while ((td->td_flags & TDF_WAKEREQ) == 0) {
202                         wakeup(td);
203                         tsleep(td, 0, "kpsusp", 0);
204                 }
205                 td->td_flags &= ~TDF_WAKEREQ;
206                 wakeup(td);
207                 lwkt_reltoken(&kpsus_token);
208         }
209 }
210