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