FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_thr.c
1 /*-
2 * Copyright (c) 2003, Jeffrey Roberson <jeff@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 unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/5.4/sys/kern/kern_thr.c 145335 2005-04-20 19:11:07Z cvs2svn $");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/proc.h>
35 #include <sys/resourcevar.h>
36 #include <sys/sched.h>
37 #include <sys/sysctl.h>
38 #include <sys/smp.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/signalvar.h>
43 #include <sys/ucontext.h>
44 #include <sys/thr.h>
45
46 #include <machine/frame.h>
47
48 extern int max_threads_per_proc;
49 extern int max_groups_per_proc;
50
51 SYSCTL_DECL(_kern_threads);
52 static int thr_scope_sys = 0;
53 SYSCTL_INT(_kern_threads, OID_AUTO, thr_scope_sys, CTLFLAG_RW,
54 &thr_scope_sys, 0, "sys or proc scope scheduling");
55
56 static int thr_concurrency = 0;
57 SYSCTL_INT(_kern_threads, OID_AUTO, thr_concurrency, CTLFLAG_RW,
58 &thr_concurrency, 0, "a concurrency value if not default");
59
60 /*
61 * System call interface.
62 */
63 int
64 thr_create(struct thread *td, struct thr_create_args *uap)
65 /* ucontext_t *ctx, long *id, int flags */
66 {
67 struct thread *newtd;
68 ucontext_t ctx;
69 long id;
70 int error;
71 struct ksegrp *kg, *newkg;
72 struct proc *p;
73 int scope_sys;
74
75 p = td->td_proc;
76 kg = td->td_ksegrp;
77 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
78 return (error);
79
80 /* Have race condition but it is cheap */
81 if ((p->p_numksegrps >= max_groups_per_proc) ||
82 (p->p_numthreads >= max_threads_per_proc)) {
83 return (EPROCLIM);
84 }
85
86 /*
87 * Use a local copy to close a race against the user
88 * changing thr_scope_sys.
89 */
90 scope_sys = thr_scope_sys;
91
92 /* Initialize our td and new ksegrp.. */
93 newtd = thread_alloc();
94 if (scope_sys)
95 newkg = ksegrp_alloc();
96 else
97 newkg = kg;
98 /*
99 * Try the copyout as soon as we allocate the td so we don't have to
100 * tear things down in a failure case below.
101 */
102 id = newtd->td_tid;
103 if ((error = copyout(&id, uap->id, sizeof(long)))) {
104 if (scope_sys)
105 ksegrp_free(newkg);
106 thread_free(newtd);
107 return (error);
108 }
109
110 bzero(&newtd->td_startzero,
111 __rangeof(struct thread, td_startzero, td_endzero));
112 bcopy(&td->td_startcopy, &newtd->td_startcopy,
113 __rangeof(struct thread, td_startcopy, td_endcopy));
114
115 if (scope_sys) {
116 bzero(&newkg->kg_startzero,
117 __rangeof(struct ksegrp, kg_startzero, kg_endzero));
118 bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
119 __rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
120 }
121
122 newtd->td_proc = td->td_proc;
123 newtd->td_ucred = crhold(td->td_ucred);
124
125 /* Set up our machine context. */
126 cpu_set_upcall(newtd, td);
127 error = set_mcontext(newtd, &ctx.uc_mcontext);
128 if (error != 0) {
129 if (scope_sys)
130 ksegrp_free(newkg);
131 thread_free(newtd);
132 crfree(td->td_ucred);
133 goto out;
134 }
135
136 /* Link the thread and kse into the ksegrp and make it runnable. */
137 PROC_LOCK(td->td_proc);
138 if (scope_sys) {
139 sched_init_concurrency(newkg);
140 } else {
141 if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
142 sched_set_concurrency(kg,
143 thr_concurrency ? thr_concurrency : (2*mp_ncpus));
144 }
145 }
146
147 td->td_proc->p_flag |= P_HADTHREADS;
148 newtd->td_sigmask = td->td_sigmask;
149 mtx_lock_spin(&sched_lock);
150 if (scope_sys)
151 ksegrp_link(newkg, p);
152 thread_link(newtd, newkg);
153 mtx_unlock_spin(&sched_lock);
154 PROC_UNLOCK(p);
155
156 /* let the scheduler know about these things. */
157 mtx_lock_spin(&sched_lock);
158 if (scope_sys)
159 sched_fork_ksegrp(td, newkg);
160 sched_fork_thread(td, newtd);
161
162 TD_SET_CAN_RUN(newtd);
163 if ((uap->flags & THR_SUSPENDED) == 0)
164 setrunqueue(newtd, SRQ_BORING);
165
166 mtx_unlock_spin(&sched_lock);
167
168 out:
169 return (error);
170 }
171
172 int
173 thr_self(struct thread *td, struct thr_self_args *uap)
174 /* long *id */
175 {
176 long id;
177 int error;
178
179 id = td->td_tid;
180 if ((error = copyout(&id, uap->id, sizeof(long))))
181 return (error);
182
183 return (0);
184 }
185
186 int
187 thr_exit(struct thread *td, struct thr_exit_args *uap)
188 /* long *state */
189 {
190 struct proc *p;
191
192 p = td->td_proc;
193
194 /* Signal userland that it can free the stack. */
195 if ((void *)uap->state != NULL)
196 suword((void *)uap->state, 1);
197
198 PROC_LOCK(p);
199 mtx_lock_spin(&sched_lock);
200
201 /*
202 * Shutting down last thread in the proc. This will actually
203 * call exit() in the trampoline when it returns.
204 */
205 if (p->p_numthreads != 1) {
206 thread_exit();
207 /* NOTREACHED */
208 }
209 mtx_unlock_spin(&sched_lock);
210 PROC_UNLOCK(p);
211 return (0);
212 }
213
214 int
215 thr_kill(struct thread *td, struct thr_kill_args *uap)
216 /* long id, int sig */
217 {
218 struct thread *ttd;
219 struct proc *p;
220 int error;
221
222 p = td->td_proc;
223 error = 0;
224 PROC_LOCK(p);
225 FOREACH_THREAD_IN_PROC(p, ttd) {
226 if (ttd->td_tid == uap->id)
227 break;
228 }
229 if (ttd == NULL) {
230 error = ESRCH;
231 goto out;
232 }
233 if (uap->sig == 0)
234 goto out;
235 if (!_SIG_VALID(uap->sig)) {
236 error = EINVAL;
237 goto out;
238 }
239 tdsignal(ttd, uap->sig, SIGTARGET_TD);
240 out:
241 PROC_UNLOCK(p);
242 return (error);
243 }
244
245 int
246 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
247 /* const struct timespec *timeout */
248 {
249 struct timespec ts;
250 struct timeval tv;
251 int error;
252 int hz;
253
254 hz = 0;
255 error = 0;
256 if (uap->timeout != NULL) {
257 error = copyin((const void *)uap->timeout, (void *)&ts,
258 sizeof(struct timespec));
259 if (error != 0)
260 return (error);
261 if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
262 return (EINVAL);
263 if (ts.tv_sec == 0 && ts.tv_nsec == 0)
264 return (ETIMEDOUT);
265 TIMESPEC_TO_TIMEVAL(&tv, &ts);
266 hz = tvtohz(&tv);
267 }
268 PROC_LOCK(td->td_proc);
269 if ((td->td_flags & TDF_THRWAKEUP) == 0)
270 error = msleep((void *)td, &td->td_proc->p_mtx,
271 td->td_priority | PCATCH, "lthr", hz);
272 mtx_lock_spin(&sched_lock);
273 td->td_flags &= ~TDF_THRWAKEUP;
274 mtx_unlock_spin(&sched_lock);
275 PROC_UNLOCK(td->td_proc);
276 return (error == EWOULDBLOCK ? ETIMEDOUT : error);
277 }
278
279 int
280 thr_wake(struct thread *td, struct thr_wake_args *uap)
281 /* long id */
282 {
283 struct thread *ttd;
284
285 PROC_LOCK(td->td_proc);
286 FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
287 if (ttd->td_tid == uap->id)
288 break;
289 }
290 if (ttd == NULL) {
291 PROC_UNLOCK(td->td_proc);
292 return (ESRCH);
293 }
294 mtx_lock_spin(&sched_lock);
295 ttd->td_flags |= TDF_THRWAKEUP;
296 mtx_unlock_spin(&sched_lock);
297 wakeup_one((void *)ttd);
298 PROC_UNLOCK(td->td_proc);
299 return (0);
300 }
Cache object: 1ece88d6e6a9a34f7c7fd15d6bee994b
|