threads.c revision 1.2 1 /* $NetBSD: threads.c,v 1.2 2009/11/09 19:00:52 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007-2009 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by
7 * The Finnish Cultural Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.2 2009/11/09 19:00:52 pooka Exp $");
33
34 #include <sys/param.h>
35 #include <sys/kmem.h>
36 #include <sys/kthread.h>
37 #include <sys/systm.h>
38
39 #include <machine/stdarg.h>
40
41 #include <rump/rumpuser.h>
42
43 #include "rump_private.h"
44
45 struct kthdesc {
46 void (*f)(void *);
47 void *arg;
48 struct lwp *mylwp;
49 };
50
51 static void *
52 threadbouncer(void *arg)
53 {
54 struct kthdesc *k = arg;
55 struct lwp *l = k->mylwp;
56 void (*f)(void *);
57 void *thrarg;
58
59 /* schedule ourselves first */
60 f = k->f;
61 thrarg = k->arg;
62 rumpuser_free(k);
63
64 rumpuser_set_curlwp(l);
65 rump_schedule();
66
67 if ((curlwp->l_pflag & LP_MPSAFE) == 0)
68 KERNEL_LOCK(1, NULL);
69
70 f(thrarg);
71
72 panic("unreachable, should kthread_exit()");
73 }
74
75 int
76 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
77 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
78 {
79 char thrstore[MAXCOMLEN];
80 const char *thrname = NULL;
81 va_list ap;
82 struct kthdesc *k;
83 struct lwp *l;
84 int rv;
85
86 thrstore[0] = '\0';
87 if (fmt) {
88 va_start(ap, fmt);
89 vsnprintf(thrstore, sizeof(thrstore), fmt, ap);
90 va_end(ap);
91 thrname = thrstore;
92 }
93
94 /*
95 * We don't want a module unload thread.
96 * (XXX: yes, this is a kludge too, and the kernel should
97 * have a more flexible method for configuring which threads
98 * we want).
99 */
100 if (strcmp(thrstore, "modunload") == 0) {
101 return 0;
102 }
103
104 if (!rump_threads) {
105 /* fake them */
106 if (strcmp(thrstore, "vrele") == 0) {
107 printf("rump warning: threads not enabled, not starting"
108 " vrele thread\n");
109 return 0;
110 } else if (strcmp(thrstore, "cachegc") == 0) {
111 printf("rump warning: threads not enabled, not starting"
112 " namecache g/c thread\n");
113 return 0;
114 } else if (strcmp(thrstore, "nfssilly") == 0) {
115 printf("rump warning: threads not enabled, not enabling"
116 " nfs silly rename\n");
117 return 0;
118 } else if (strcmp(thrstore, "unpgc") == 0) {
119 printf("rump warning: threads not enabled, not enabling"
120 " UNP garbage collection\n");
121 return 0;
122 } else
123 panic("threads not available, setenv RUMP_THREADS 1");
124 }
125
126 KASSERT(fmt != NULL);
127 if (ci != NULL)
128 panic("%s: bounded threads not supported", __func__);
129
130 k = rumpuser_malloc(sizeof(struct kthdesc), 0);
131 k->f = func;
132 k->arg = arg;
133 k->mylwp = l = rump_lwp_alloc(0, rump_nextlid());
134 if (flags & KTHREAD_MPSAFE)
135 l->l_pflag |= LP_MPSAFE;
136 if (flags & KTHREAD_INTR)
137 l->l_pflag |= LP_INTR;
138 rv = rumpuser_thread_create(threadbouncer, k, thrname);
139 if (rv)
140 return rv;
141
142 if (newlp)
143 *newlp = l;
144 return 0;
145 }
146
147 void
148 kthread_exit(int ecode)
149 {
150
151 if ((curlwp->l_pflag & LP_MPSAFE) == 0)
152 KERNEL_UNLOCK_ONE(NULL);
153 rump_lwp_release(curlwp);
154 rump_unschedule();
155 rumpuser_thread_exit();
156 }
157