18ee90a3c276a31cacbbedfd66e67605ff99a188
[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.6 2003/06/27 01:53:25 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  * Start a kernel process.  This is called after a fork() call in
44  * mi_startup() in the file kern/init_main.c.
45  *
46  * This function is used to start "internal" daemons and intended
47  * to be called from SYSINIT().
48  */
49 void
50 kproc_start(udata)
51         const void *udata;
52 {
53         const struct kproc_desc *kp = udata;
54         int error;
55
56         error = kthread_create((void (*)(void *))kp->func, NULL,
57                     kp->global_threadpp, kp->arg0);
58         if (error)
59                 panic("kproc_start: %s: error %d", kp->arg0, error);
60 }
61
62 /*
63  * Create a kernel process/thread/whatever.  It shares it's address space
64  * with proc0 - ie: kernel only.
65  */
66 int
67 kthread_create(void (*func)(void *), void *arg,
68     struct thread **tdp, const char *fmt, ...)
69 {
70         struct thread *td;
71         va_list ap;
72
73         td = *tdp = lwkt_alloc_thread();
74         cpu_set_thread_handler(td, kthread_exit, func, arg);
75
76         /*
77          * Set up arg0 for 'ps' etc
78          */
79         va_start(ap, fmt);
80         vsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
81         va_end(ap);
82
83         /*
84          * Schedule the thread to run
85          */
86         lwkt_schedule(td);
87         return 0;
88 }
89
90 /*
91  * YYY kthread_exit() should get rid of the kthread.  We have to put it on
92  * some sort of wait list and set our switcher to interlock against
93  * the reaper.
94  */
95 void
96 kthread_exit(void)
97 {
98         thread_t td = curthread;
99
100         printf("kthread %p %s has exited\n", td, td->td_comm); /* YYY */
101         for (;;) {
102                 td->td_flags |= TDF_STOPREQ;
103                 kproc_suspend_loop();
104         }
105 }
106
107 /*
108  * Advise a kernel process to suspend (or resume) in its main loop.
109  * Participation is voluntary.
110  */
111 int
112 suspend_kproc(struct thread *td, int timo)
113 {
114         if (td->td_proc == NULL) {
115                 td->td_flags |= TDF_STOPREQ;    /* request thread exit */
116                 wakeup(td);
117                 while (td->td_flags & TDF_STOPREQ) {
118                         int error = tsleep(td, PPAUSE, "suspkp", timo);
119                         if (error == EWOULDBLOCK)
120                                 break;
121                 }
122                 td->td_flags &= ~TDF_STOPREQ;
123                 return(0);
124         } else {
125                 return(EINVAL); /* not a kernel thread */
126         }
127 }
128
129 void
130 kproc_suspend_loop(void)
131 {
132         struct thread *td = curthread;
133
134         if (td->td_flags & TDF_STOPREQ) {
135                 td->td_flags &= ~TDF_STOPREQ;
136                 while ((td->td_flags & TDF_WAKEREQ) == 0) {
137                         wakeup(td);
138                         tsleep(td, PPAUSE, "kpsusp", 0);
139                 }
140                 td->td_flags &= ~TDF_WAKEREQ;
141                 wakeup(td);
142         }
143 }
144