2 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
33 #include <sys/mutex.h>
35 #include <sys/resourcevar.h>
36 #include <sys/sched.h>
37 #include <sys/sysent.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/signalvar.h>
41 #include <sys/ucontext.h>
44 #include <machine/frame.h>
47 * Back end support functions.
63 mtx_assert(&sched_lock, MA_OWNED);
64 PROC_LOCK_ASSERT(p, MA_OWNED);
65 KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
68 * Shutting down last thread in the proc. This will actually
69 * call exit() in the trampoline when it returns.
71 if (p->p_numthreads == 1) {
77 * XXX Undelivered process wide signals should be reposted to the
81 /* Clean up cpu resources. */
84 /* Unlink the thread from the process and kseg. */
87 ke->ke_state = KES_UNQUEUED;
90 sched_exit_kse(TAILQ_NEXT(ke, ke_kglist), ke);
93 * If we were stopped while waiting for all threads to exit and this
94 * is the last thread wakeup the exiting thread.
96 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE)
97 if (p->p_numthreads == 1)
98 thread_unsuspend_one(p->p_singlethread);
102 td->td_state = TDS_INACTIVE;
106 td->td_ksegrp = NULL;
107 td->td_last_kse = NULL;
108 sched_exit_thread(TAILQ_NEXT(td, td_kglist), td);
111 cpu_throw(td, choosethread());
114 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
117 * System call interface.
120 thr_create(struct thread *td, struct thr_create_args *uap)
121 /* ucontext_t *ctx, long *id, int flags */
129 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
132 /* Initialize our td. */
133 td0 = thread_alloc();
136 * Try the copyout as soon as we allocate the td so we don't have to
137 * tear things down in a failure case below.
140 if ((error = copyout(&id, uap->id, sizeof(long)))) {
145 bzero(&td0->td_startzero,
146 (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
147 bcopy(&td->td_startcopy, &td0->td_startcopy,
148 (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
150 td0->td_proc = td->td_proc;
151 PROC_LOCK(td->td_proc);
152 td0->td_sigmask = td->td_sigmask;
153 PROC_UNLOCK(td->td_proc);
154 td0->td_ucred = crhold(td->td_ucred);
156 /* Initialize our kse structure. */
158 bzero(&ke0->ke_startzero,
159 RANGEOF(struct kse, ke_startzero, ke_endzero));
161 /* Set up our machine context. */
162 cpu_set_upcall(td0, td);
163 error = set_mcontext(td0, &ctx.uc_mcontext);
170 /* Link the thread and kse into the ksegrp and make it runnable. */
171 mtx_lock_spin(&sched_lock);
173 thread_link(td0, td->td_ksegrp);
174 kse_link(ke0, td->td_ksegrp);
176 /* Bind this thread and kse together. */
178 ke0->ke_thread = td0;
180 sched_fork_kse(td->td_kse, ke0);
181 sched_fork_thread(td, td0);
184 if ((uap->flags & THR_SUSPENDED) == 0)
187 mtx_unlock_spin(&sched_lock);
194 thr_self(struct thread *td, struct thr_self_args *uap)
201 if ((error = copyout(&id, uap->id, sizeof(long))))
208 thr_exit(struct thread *td, struct thr_exit_args *uap)
216 mtx_lock_spin(&sched_lock);
219 * This unlocks proc and doesn't return unless this is the last
223 mtx_unlock_spin(&sched_lock);
229 thr_kill(struct thread *td, struct thr_kill_args *uap)
230 /* long id, int sig */
239 FOREACH_THREAD_IN_PROC(p, ttd) {
240 if (ttd->td_tid == uap->id)
249 if (!_SIG_VALID(uap->sig)) {
253 tdsignal(ttd, uap->sig, SIGTARGET_TD);
260 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
261 /* const struct timespec *timeout */
270 if (uap->timeout != NULL) {
271 error = copyin((const void *)uap->timeout, (void *)&ts,
272 sizeof(struct timespec));
275 if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
277 if (ts.tv_sec == 0 && ts.tv_nsec == 0)
279 TIMESPEC_TO_TIMEVAL(&tv, &ts);
282 PROC_LOCK(td->td_proc);
283 mtx_lock_spin(&sched_lock);
284 if ((td->td_flags & TDF_THRWAKEUP) == 0) {
285 mtx_unlock_spin(&sched_lock);
286 error = msleep((void *)td, &td->td_proc->p_mtx,
287 td->td_priority | PCATCH, "lthr", hz);
288 mtx_lock_spin(&sched_lock);
290 td->td_flags &= ~TDF_THRWAKEUP;
291 mtx_unlock_spin(&sched_lock);
292 PROC_UNLOCK(td->td_proc);
293 return (error == EWOULDBLOCK ? ETIMEDOUT : error);
297 thr_wake(struct thread *td, struct thr_wake_args *uap)
302 PROC_LOCK(td->td_proc);
303 FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
304 if (ttd->td_tid == uap->id)
308 PROC_UNLOCK(td->td_proc);
311 mtx_lock_spin(&sched_lock);
312 ttd->td_flags |= TDF_THRWAKEUP;
313 mtx_unlock_spin(&sched_lock);
314 wakeup_one((void *)ttd);
315 PROC_UNLOCK(td->td_proc);