2 * Copyright (C) 2001 Julian Elischer <julian@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(s), this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified other than the possible
11 * addition of one or more copyright notices.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice(s), this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/mutex.h>
38 #include <sys/resourcevar.h>
40 #include <sys/sysctl.h>
41 #include <sys/sched.h>
42 #include <sys/sleepqueue.h>
43 #include <sys/turnstile.h>
47 #include <security/audit/audit.h>
50 #include <vm/vm_extern.h>
54 * thread related storage.
56 static uma_zone_t thread_zone;
59 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
60 static int thread_debug = 0;
61 SYSCTL_INT(_kern_threads, OID_AUTO, debug, CTLFLAG_RW,
62 &thread_debug, 0, "thread debug");
64 int max_threads_per_proc = 1500;
65 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
66 &max_threads_per_proc, 0, "Limit on threads per proc");
68 int max_groups_per_proc = 1500;
69 SYSCTL_INT(_kern_threads, OID_AUTO, max_groups_per_proc, CTLFLAG_RW,
70 &max_groups_per_proc, 0, "Limit on thread groups per proc");
73 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
74 &max_threads_hits, 0, "");
80 TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
81 struct mtx kse_zombie_lock;
82 MTX_SYSINIT(kse_zombie_lock, &kse_zombie_lock, "kse zombie lock", MTX_SPIN);
86 sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS)
95 new_val = virtual_cpu;
96 error = sysctl_handle_int(oidp, &new_val, 0, req);
97 if (error != 0 || req->newptr == NULL)
101 virtual_cpu = new_val;
106 SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW,
107 0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I",
108 "debug virtual cpus");
112 static struct unrhdr *tid_unrhdr;
115 * Prepare a thread for use.
118 thread_ctor(void *mem, int size, void *arg, int flags)
122 td = (struct thread *)mem;
123 td->td_state = TDS_INACTIVE;
124 td->td_oncpu = NOCPU;
126 td->td_tid = alloc_unr(tid_unrhdr);
129 * Note that td_critnest begins life as 1 because the thread is not
130 * running and is thereby implicitly waiting to be on the receiving
131 * end of a context switch. A context switch must occur inside a
132 * critical section, and in fact, includes hand-off of the sched_lock.
133 * After a context switch to a newly created thread, it will release
134 * sched_lock for the first time, and its td_critnest will hit 0 for
135 * the first time. This happens on the far end of a context switch,
136 * and when it context switches away from itself, it will in fact go
137 * back into a critical section, and hand off the sched lock to the
143 audit_thread_alloc(td);
145 umtx_thread_alloc(td);
150 * Reclaim a thread after use.
153 thread_dtor(void *mem, int size, void *arg)
157 td = (struct thread *)mem;
160 /* Verify that this thread is in a safe state to free. */
161 switch (td->td_state) {
167 * We must never unlink a thread that is in one of
168 * these states, because it is currently active.
170 panic("bad state for thread unlinking");
175 panic("bad thread state");
180 audit_thread_free(td);
182 free_unr(tid_unrhdr, td->td_tid);
187 * Initialize type-stable parts of a thread (when newly created).
190 thread_init(void *mem, int size, int flags)
194 td = (struct thread *)mem;
196 vm_thread_new(td, 0);
197 cpu_thread_setup(td);
198 td->td_sleepqueue = sleepq_alloc();
199 td->td_turnstile = turnstile_alloc();
200 td->td_sched = (struct td_sched *)&td[1];
202 umtx_thread_init(td);
207 * Tear down type-stable parts of a thread (just before being discarded).
210 thread_fini(void *mem, int size)
214 td = (struct thread *)mem;
215 turnstile_free(td->td_turnstile);
216 sleepq_free(td->td_sleepqueue);
217 umtx_thread_fini(td);
218 vm_thread_dispose(td);
222 * For a newly created process,
223 * link up all the structures and its initial threads etc.
225 * {arch}/{arch}/machdep.c ia64_init(), init386() etc.
226 * proc_dtor() (should go away)
230 proc_linkup(struct proc *p, struct thread *td)
232 TAILQ_INIT(&p->p_threads); /* all threads in proc */
233 TAILQ_INIT(&p->p_upcalls); /* upcall list */
234 sigqueue_init(&p->p_sigqueue, p);
235 p->p_ksi = ksiginfo_alloc(1);
236 if (p->p_ksi != NULL) {
237 /* XXX p_ksi may be null if ksiginfo zone is not ready */
238 p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
240 LIST_INIT(&p->p_mqnotifier);
246 * Initialize global thread allocation resources.
252 mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
253 tid_unrhdr = new_unrhdr(PID_MAX + 1, INT_MAX, &tid_lock);
255 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
256 thread_ctor, thread_dtor, thread_init, thread_fini,
259 kseinit(); /* set up kse specific stuff e.g. upcall zone*/
264 * Stash an embarasingly extra thread into the zombie thread queue.
265 * Use the slpq as that must be unused by now.
268 thread_stash(struct thread *td)
270 mtx_lock_spin(&kse_zombie_lock);
271 TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
272 mtx_unlock_spin(&kse_zombie_lock);
276 * Reap zombie kse resource.
281 struct thread *td_first, *td_next;
284 * Don't even bother to lock if none at this instant,
285 * we really don't care about the next instant..
287 if (!TAILQ_EMPTY(&zombie_threads)) {
288 mtx_lock_spin(&kse_zombie_lock);
289 td_first = TAILQ_FIRST(&zombie_threads);
291 TAILQ_INIT(&zombie_threads);
292 mtx_unlock_spin(&kse_zombie_lock);
294 td_next = TAILQ_NEXT(td_first, td_slpq);
295 if (td_first->td_ucred)
296 crfree(td_first->td_ucred);
297 thread_free(td_first);
310 thread_reap(); /* check if any zombies to get */
311 return (uma_zalloc(thread_zone, M_WAITOK));
316 * Deallocate a thread.
319 thread_free(struct thread *td)
322 cpu_thread_clean(td);
323 uma_zfree(thread_zone, td);
327 * Discard the current thread and exit from its context.
328 * Always called with scheduler locked.
330 * Because we can't free a thread while we're operating under its context,
331 * push the current thread into our CPU's deadthread holder. This means
332 * we needn't worry about someone else grabbing our context before we
333 * do a cpu_throw(). This may not be needed now as we are under schedlock.
334 * Maybe we can just do a thread_stash() as thr_exit1 does.
337 * libthr expects its thread exit to return for the last
338 * thread, meaning that the program is back to non-threaded
339 * mode I guess. Because we do this (cpu_throw) unconditionally
340 * here, they have their own version of it. (thr_exit1())
341 * that doesn't do it all if this was the last thread.
342 * It is also called from thread_suspend_check().
343 * Of course in the end, they end up coming here through exit1
344 * anyhow.. After fixing 'thr' to play by the rules we should be able
345 * to merge these two functions together.
352 * thread_user_enter()
355 * thread_suspend_check()
360 uint64_t new_switchtime;
367 mtx_assert(&sched_lock, MA_OWNED);
368 mtx_assert(&Giant, MA_NOTOWNED);
369 PROC_LOCK_ASSERT(p, MA_OWNED);
370 KASSERT(p != NULL, ("thread exiting without a process"));
371 CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
372 (long)p->p_pid, p->p_comm);
373 KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
376 AUDIT_SYSCALL_EXIT(0, td);
380 if (td->td_standin != NULL) {
382 * Note that we don't need to free the cred here as it
383 * is done in thread_reap().
385 thread_stash(td->td_standin);
386 td->td_standin = NULL;
390 umtx_thread_exit(td);
393 * drop FPU & debug register state storage, or any other
394 * architecture specific resources that
395 * would not be on a new untouched process.
397 cpu_thread_exit(td); /* XXXSMP */
401 * The thread is exiting. scheduler can release its stuff
402 * and collect stats etc.
403 * XXX this is not very right, since PROC_UNLOCK may still
404 * need scheduler stuff.
406 sched_thread_exit(td);
409 /* Do the same timestamp bookkeeping that mi_switch() would do. */
410 new_switchtime = cpu_ticks();
411 p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime));
412 p->p_rux.rux_uticks += td->td_uticks;
413 p->p_rux.rux_sticks += td->td_sticks;
414 p->p_rux.rux_iticks += td->td_iticks;
415 PCPU_SET(switchtime, new_switchtime);
416 PCPU_SET(switchticks, ticks);
419 /* Add our usage into the usage of all our children. */
420 if (p->p_numthreads == 1)
421 ruadd(p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
424 * The last thread is left attached to the process
425 * So that the whole bundle gets recycled. Skip
426 * all this stuff if we never had threads.
427 * EXIT clears all sign of other threads when
428 * it goes to single threading, so the last thread always
429 * takes the short path.
431 if (p->p_flag & P_HADTHREADS) {
432 if (p->p_numthreads > 1) {
435 sched_exit_thread(FIRST_THREAD_IN_PROC(p), td);
438 * The test below is NOT true if we are the
439 * sole exiting thread. P_STOPPED_SNGL is unset
440 * in exit1() after it is the only survivor.
442 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
443 if (p->p_numthreads == p->p_suspcount) {
444 thread_unsuspend_one(p->p_singlethread);
450 * Because each upcall structure has an owner thread,
451 * owner thread exits only when process is in exiting
452 * state, so upcall to userland is no longer needed,
453 * deleting upcall structure is safe here.
454 * So when all threads in a group is exited, all upcalls
455 * in the group should be automatically freed.
456 * XXXKSE This is a KSE thing and should be exported
463 PCPU_SET(deadthread, td);
466 * The last thread is exiting.. but not through exit()
468 * Theoretically this can't happen
469 * exit1() - clears threading flags before coming here
470 * kse_exit() - treats last thread specially
471 * thr_exit() - treats last thread specially
473 * thread_user_enter() - only if more exist
474 * thread_userret() - only if more exist
476 * thread_suspend_check() - only if more exist
478 panic ("thread_exit: Last thread exiting on its own");
482 * non threaded process comes here.
483 * This includes an EX threaded process that is coming
484 * here via exit1(). (exit1 dethreads the proc first).
488 td->td_state = TDS_INACTIVE;
489 CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
490 cpu_throw(td, choosethread());
491 panic("I'm a teapot!");
496 * Do any thread specific cleanups that may be needed in wait()
497 * called with Giant, proc and schedlock not held.
500 thread_wait(struct proc *p)
504 mtx_assert(&Giant, MA_NOTOWNED);
505 KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()"));
506 FOREACH_THREAD_IN_PROC(p, td) {
508 if (td->td_standin != NULL) {
509 if (td->td_standin->td_ucred != NULL) {
510 crfree(td->td_standin->td_ucred);
511 td->td_standin->td_ucred = NULL;
513 thread_free(td->td_standin);
514 td->td_standin = NULL;
517 cpu_thread_clean(td);
518 crfree(td->td_ucred);
520 thread_reap(); /* check for zombie threads etc. */
524 * Link a thread to a process.
525 * set up anything that needs to be initialized for it to
526 * be used by the process.
528 * Note that we do not link to the proc's ucred here.
529 * The thread is linked as if running but no KSE assigned.
532 * thread_schedule_upcall()
536 thread_link(struct thread *td, struct proc *p)
539 td->td_state = TDS_INACTIVE;
543 LIST_INIT(&td->td_contested);
544 sigqueue_init(&td->td_sigqueue, p);
545 callout_init(&td->td_slpcallout, CALLOUT_MPSAFE);
546 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
551 * Convert a process with one thread to an unthreaded process.
553 * thread_single(exit) (called from execve and exit)
554 * kse_exit() XXX may need cleaning up wrt KSE stuff
557 thread_unthread(struct thread *td)
559 struct proc *p = td->td_proc;
561 KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads"));
564 p->p_flag &= ~(P_SA|P_HADTHREADS);
565 td->td_mailbox = NULL;
566 td->td_pflags &= ~(TDP_SA | TDP_CAN_UNBIND);
567 if (td->td_standin != NULL) {
568 thread_stash(td->td_standin);
569 td->td_standin = NULL;
571 sched_set_concurrency(p, 1);
573 p->p_flag &= ~P_HADTHREADS;
582 thread_unlink(struct thread *td)
584 struct proc *p = td->td_proc;
586 mtx_assert(&sched_lock, MA_OWNED);
587 TAILQ_REMOVE(&p->p_threads, td, td_plist);
589 /* could clear a few other things here */
590 /* Must NOT clear links to proc! */
594 * Enforce single-threading.
596 * Returns 1 if the caller must abort (another thread is waiting to
597 * exit the process or similar). Process is locked!
598 * Returns 0 when you are successfully the only thread running.
599 * A process has successfully single threaded in the suspend mode when
600 * There are no threads in user mode. Threads in the kernel must be
601 * allowed to continue until they get to the user boundary. They may even
602 * copy out their return values and data before suspending. They may however be
603 * accelerated in reaching the user boundary as we will wake up
604 * any sleeping threads that are interruptable. (PCATCH).
607 thread_single(int mode)
616 mtx_assert(&Giant, MA_NOTOWNED);
617 PROC_LOCK_ASSERT(p, MA_OWNED);
618 KASSERT((td != NULL), ("curthread is NULL"));
620 if ((p->p_flag & P_HADTHREADS) == 0)
623 /* Is someone already single threading? */
624 if (p->p_singlethread != NULL && p->p_singlethread != td)
627 if (mode == SINGLE_EXIT) {
628 p->p_flag |= P_SINGLE_EXIT;
629 p->p_flag &= ~P_SINGLE_BOUNDARY;
631 p->p_flag &= ~P_SINGLE_EXIT;
632 if (mode == SINGLE_BOUNDARY)
633 p->p_flag |= P_SINGLE_BOUNDARY;
635 p->p_flag &= ~P_SINGLE_BOUNDARY;
637 p->p_flag |= P_STOPPED_SINGLE;
638 mtx_lock_spin(&sched_lock);
639 p->p_singlethread = td;
640 if (mode == SINGLE_EXIT)
641 remaining = p->p_numthreads;
642 else if (mode == SINGLE_BOUNDARY)
643 remaining = p->p_numthreads - p->p_boundary_count;
645 remaining = p->p_numthreads - p->p_suspcount;
646 while (remaining != 1) {
647 if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
649 FOREACH_THREAD_IN_PROC(p, td2) {
652 td2->td_flags |= TDF_ASTPENDING;
653 if (TD_IS_INHIBITED(td2)) {
656 if (td->td_flags & TDF_DBSUSPEND)
657 td->td_flags &= ~TDF_DBSUSPEND;
658 if (TD_IS_SUSPENDED(td2))
659 thread_unsuspend_one(td2);
660 if (TD_ON_SLEEPQ(td2) &&
661 (td2->td_flags & TDF_SINTR))
662 sleepq_abort(td2, EINTR);
664 case SINGLE_BOUNDARY:
665 if (TD_IS_SUSPENDED(td2) &&
666 !(td2->td_flags & TDF_BOUNDARY))
667 thread_unsuspend_one(td2);
668 if (TD_ON_SLEEPQ(td2) &&
669 (td2->td_flags & TDF_SINTR))
670 sleepq_abort(td2, ERESTART);
673 if (TD_IS_SUSPENDED(td2))
676 * maybe other inhibitted states too?
678 if ((td2->td_flags & TDF_SINTR) &&
679 (td2->td_inhibitors &
680 (TDI_SLEEPING | TDI_SWAPPED)))
681 thread_suspend_one(td2);
686 else if (TD_IS_RUNNING(td2) && td != td2) {
691 if (mode == SINGLE_EXIT)
692 remaining = p->p_numthreads;
693 else if (mode == SINGLE_BOUNDARY)
694 remaining = p->p_numthreads - p->p_boundary_count;
696 remaining = p->p_numthreads - p->p_suspcount;
699 * Maybe we suspended some threads.. was it enough?
706 * Wake us up when everyone else has suspended.
707 * In the mean time we suspend as well.
710 thread_suspend_one(td);
712 mi_switch(SW_VOL, NULL);
713 mtx_unlock_spin(&sched_lock);
715 mtx_lock_spin(&sched_lock);
716 if (mode == SINGLE_EXIT)
717 remaining = p->p_numthreads;
718 else if (mode == SINGLE_BOUNDARY)
719 remaining = p->p_numthreads - p->p_boundary_count;
721 remaining = p->p_numthreads - p->p_suspcount;
723 if (mode == SINGLE_EXIT) {
725 * We have gotten rid of all the other threads and we
726 * are about to either exit or exec. In either case,
727 * we try our utmost to revert to being a non-threaded
730 p->p_singlethread = NULL;
731 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT);
734 mtx_unlock_spin(&sched_lock);
739 * Called in from locations that can safely check to see
740 * whether we have to suspend or at least throttle for a
741 * single-thread event (e.g. fork).
743 * Such locations include userret().
744 * If the "return_instead" argument is non zero, the thread must be able to
745 * accept 0 (caller may continue), or 1 (caller must abort) as a result.
747 * The 'return_instead' argument tells the function if it may do a
748 * thread_exit() or suspend, or whether the caller must abort and back
751 * If the thread that set the single_threading request has set the
752 * P_SINGLE_EXIT bit in the process flags then this call will never return
753 * if 'return_instead' is false, but will exit.
755 * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
756 *---------------+--------------------+---------------------
757 * 0 | returns 0 | returns 0 or 1
758 * | when ST ends | immediatly
759 *---------------+--------------------+---------------------
760 * 1 | thread exits | returns 1
762 * 0 = thread_exit() or suspension ok,
763 * other = return error instead of stopping the thread.
765 * While a full suspension is under effect, even a single threading
766 * thread would be suspended if it made this call (but it shouldn't).
767 * This call should only be made from places where
768 * thread_exit() would be safe as that may be the outcome unless
769 * return_instead is set.
772 thread_suspend_check(int return_instead)
779 mtx_assert(&Giant, MA_NOTOWNED);
780 PROC_LOCK_ASSERT(p, MA_OWNED);
781 while (P_SHOULDSTOP(p) ||
782 ((p->p_flag & P_TRACED) && (td->td_flags & TDF_DBSUSPEND))) {
783 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
784 KASSERT(p->p_singlethread != NULL,
785 ("singlethread not set"));
787 * The only suspension in action is a
788 * single-threading. Single threader need not stop.
789 * XXX Should be safe to access unlocked
790 * as it can only be set to be true by us.
792 if (p->p_singlethread == td)
793 return (0); /* Exempt from stopping. */
795 if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
798 /* Should we goto user boundary if we didn't come from there? */
799 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
800 (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
803 /* If thread will exit, flush its pending signals */
804 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
805 sigqueue_flush(&td->td_sigqueue);
807 mtx_lock_spin(&sched_lock);
810 * If the process is waiting for us to exit,
811 * this thread should just suicide.
812 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
814 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
818 * When a thread suspends, it just
819 * gets taken off all queues.
821 thread_suspend_one(td);
822 if (return_instead == 0) {
823 p->p_boundary_count++;
824 td->td_flags |= TDF_BOUNDARY;
826 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
827 if (p->p_numthreads == p->p_suspcount)
828 thread_unsuspend_one(p->p_singlethread);
831 mi_switch(SW_INVOL, NULL);
832 if (return_instead == 0) {
833 p->p_boundary_count--;
834 td->td_flags &= ~TDF_BOUNDARY;
836 mtx_unlock_spin(&sched_lock);
843 thread_suspend_one(struct thread *td)
845 struct proc *p = td->td_proc;
847 mtx_assert(&sched_lock, MA_OWNED);
848 PROC_LOCK_ASSERT(p, MA_OWNED);
849 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
851 TD_SET_SUSPENDED(td);
855 thread_unsuspend_one(struct thread *td)
857 struct proc *p = td->td_proc;
859 mtx_assert(&sched_lock, MA_OWNED);
860 PROC_LOCK_ASSERT(p, MA_OWNED);
861 KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
862 TD_CLR_SUSPENDED(td);
868 * Allow all threads blocked by single threading to continue running.
871 thread_unsuspend(struct proc *p)
875 mtx_assert(&sched_lock, MA_OWNED);
876 PROC_LOCK_ASSERT(p, MA_OWNED);
877 if (!P_SHOULDSTOP(p)) {
878 FOREACH_THREAD_IN_PROC(p, td) {
879 if (TD_IS_SUSPENDED(td)) {
880 thread_unsuspend_one(td);
883 } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
884 (p->p_numthreads == p->p_suspcount)) {
886 * Stopping everything also did the job for the single
887 * threading request. Now we've downgraded to single-threaded,
890 thread_unsuspend_one(p->p_singlethread);
895 * End the single threading mode..
898 thread_single_end(void)
905 PROC_LOCK_ASSERT(p, MA_OWNED);
906 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY);
907 mtx_lock_spin(&sched_lock);
908 p->p_singlethread = NULL;
910 * If there are other threads they mey now run,
911 * unless of course there is a blanket 'stop order'
912 * on the process. The single threader must be allowed
913 * to continue however as this is a bad place to stop.
915 if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
916 FOREACH_THREAD_IN_PROC(p, td) {
917 if (TD_IS_SUSPENDED(td)) {
918 thread_unsuspend_one(td);
922 mtx_unlock_spin(&sched_lock);
926 thread_find(struct proc *p, lwpid_t tid)
930 PROC_LOCK_ASSERT(p, MA_OWNED);
931 mtx_lock_spin(&sched_lock);
932 FOREACH_THREAD_IN_PROC(p, td) {
933 if (td->td_tid == tid)
936 mtx_unlock_spin(&sched_lock);