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/selinfo.h>
44 #include <sys/turnstile.h>
48 #include <security/audit/audit.h>
51 #include <vm/vm_extern.h>
53 #include <sys/eventhandler.h>
56 * thread related storage.
58 static uma_zone_t thread_zone;
60 SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation");
62 int max_threads_per_proc = 1500;
63 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
64 &max_threads_per_proc, 0, "Limit on threads per proc");
67 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
68 &max_threads_hits, 0, "");
74 TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads);
75 static struct mtx zombie_lock;
76 MTX_SYSINIT(zombie_lock, &zombie_lock, "zombie lock", MTX_SPIN);
78 static void thread_zombie(struct thread *);
82 sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS)
91 new_val = virtual_cpu;
92 error = sysctl_handle_int(oidp, &new_val, 0, req);
93 if (error != 0 || req->newptr == NULL)
97 virtual_cpu = new_val;
102 SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW,
103 0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I",
104 "debug virtual cpus");
108 static struct unrhdr *tid_unrhdr;
111 * Prepare a thread for use.
114 thread_ctor(void *mem, int size, void *arg, int flags)
118 td = (struct thread *)mem;
119 td->td_state = TDS_INACTIVE;
120 td->td_oncpu = NOCPU;
122 td->td_tid = alloc_unr(tid_unrhdr);
126 * Note that td_critnest begins life as 1 because the thread is not
127 * running and is thereby implicitly waiting to be on the receiving
128 * end of a context switch.
131 EVENTHANDLER_INVOKE(thread_ctor, td);
133 audit_thread_alloc(td);
135 umtx_thread_alloc(td);
140 * Reclaim a thread after use.
143 thread_dtor(void *mem, int size, void *arg)
147 td = (struct thread *)mem;
150 /* Verify that this thread is in a safe state to free. */
151 switch (td->td_state) {
157 * We must never unlink a thread that is in one of
158 * these states, because it is currently active.
160 panic("bad state for thread unlinking");
165 panic("bad thread state");
170 audit_thread_free(td);
172 EVENTHANDLER_INVOKE(thread_dtor, td);
173 free_unr(tid_unrhdr, td->td_tid);
178 * Initialize type-stable parts of a thread (when newly created).
181 thread_init(void *mem, int size, int flags)
185 td = (struct thread *)mem;
187 td->td_sleepqueue = sleepq_alloc();
188 td->td_turnstile = turnstile_alloc();
189 EVENTHANDLER_INVOKE(thread_init, td);
190 td->td_sched = (struct td_sched *)&td[1];
192 umtx_thread_init(td);
198 * Tear down type-stable parts of a thread (just before being discarded).
201 thread_fini(void *mem, int size)
205 td = (struct thread *)mem;
206 EVENTHANDLER_INVOKE(thread_fini, td);
207 turnstile_free(td->td_turnstile);
208 sleepq_free(td->td_sleepqueue);
209 umtx_thread_fini(td);
214 * For a newly created process,
215 * link up all the structures and its initial threads etc.
217 * {arch}/{arch}/machdep.c ia64_init(), init386() etc.
218 * proc_dtor() (should go away)
222 proc_linkup0(struct proc *p, struct thread *td)
224 TAILQ_INIT(&p->p_threads); /* all threads in proc */
229 proc_linkup(struct proc *p, struct thread *td)
233 TAILQ_INIT(&p->p_upcalls); /* upcall list */
235 sigqueue_init(&p->p_sigqueue, p);
236 p->p_ksi = ksiginfo_alloc(1);
237 if (p->p_ksi != NULL) {
238 /* XXX p_ksi may be null if ksiginfo zone is not ready */
239 p->p_ksi->ksi_flags = KSI_EXT | KSI_INS;
241 LIST_INIT(&p->p_mqnotifier);
247 * Initialize global thread allocation resources.
253 mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF);
254 /* leave one number for thread0 */
255 tid_unrhdr = new_unrhdr(PID_MAX + 2, INT_MAX, &tid_lock);
257 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
258 thread_ctor, thread_dtor, thread_init, thread_fini,
261 kseinit(); /* set up kse specific stuff e.g. upcall zone*/
266 * Place an unused thread on the zombie list.
267 * Use the slpq as that must be unused by now.
270 thread_zombie(struct thread *td)
272 mtx_lock_spin(&zombie_lock);
273 TAILQ_INSERT_HEAD(&zombie_threads, td, td_slpq);
274 mtx_unlock_spin(&zombie_lock);
278 * Release a thread that has exited after cpu_throw().
281 thread_stash(struct thread *td)
283 atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1);
288 * Reap zombie kse resource.
293 struct thread *td_first, *td_next;
296 * Don't even bother to lock if none at this instant,
297 * we really don't care about the next instant..
299 if (!TAILQ_EMPTY(&zombie_threads)) {
300 mtx_lock_spin(&zombie_lock);
301 td_first = TAILQ_FIRST(&zombie_threads);
303 TAILQ_INIT(&zombie_threads);
304 mtx_unlock_spin(&zombie_lock);
306 td_next = TAILQ_NEXT(td_first, td_slpq);
307 if (td_first->td_ucred)
308 crfree(td_first->td_ucred);
309 thread_free(td_first);
326 thread_reap(); /* check if any zombies to get */
328 td = (struct thread *)uma_zalloc(thread_zone, M_WAITOK);
329 KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack"));
330 if (!vm_thread_new(td, 0)) {
331 uma_zfree(thread_zone, td);
334 cpu_thread_alloc(td);
340 * Deallocate a thread.
343 thread_free(struct thread *td)
347 if (td->td_altkstack != 0)
348 vm_thread_dispose_altkstack(td);
349 if (td->td_kstack != 0)
350 vm_thread_dispose(td);
351 uma_zfree(thread_zone, td);
355 * Discard the current thread and exit from its context.
356 * Always called with scheduler locked.
358 * Because we can't free a thread while we're operating under its context,
359 * push the current thread into our CPU's deadthread holder. This means
360 * we needn't worry about someone else grabbing our context before we
361 * do a cpu_throw(). This may not be needed now as we are under schedlock.
362 * Maybe we can just do a thread_stash() as thr_exit1 does.
365 * libthr expects its thread exit to return for the last
366 * thread, meaning that the program is back to non-threaded
367 * mode I guess. Because we do this (cpu_throw) unconditionally
368 * here, they have their own version of it. (thr_exit1())
369 * that doesn't do it all if this was the last thread.
370 * It is also called from thread_suspend_check().
371 * Of course in the end, they end up coming here through exit1
372 * anyhow.. After fixing 'thr' to play by the rules we should be able
373 * to merge these two functions together.
380 * thread_user_enter()
383 * thread_suspend_check()
388 uint64_t new_switchtime;
396 PROC_SLOCK_ASSERT(p, MA_OWNED);
397 mtx_assert(&Giant, MA_NOTOWNED);
399 PROC_LOCK_ASSERT(p, MA_OWNED);
400 KASSERT(p != NULL, ("thread exiting without a process"));
401 CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td,
402 (long)p->p_pid, td->td_name);
403 KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending"));
406 AUDIT_SYSCALL_EXIT(0, td);
410 if (td->td_standin != NULL) {
412 * Note that we don't need to free the cred here as it
413 * is done in thread_reap().
415 thread_zombie(td->td_standin);
416 td->td_standin = NULL;
420 umtx_thread_exit(td);
423 * drop FPU & debug register state storage, or any other
424 * architecture specific resources that
425 * would not be on a new untouched process.
427 cpu_thread_exit(td); /* XXXSMP */
429 /* Do the same timestamp bookkeeping that mi_switch() would do. */
430 new_switchtime = cpu_ticks();
431 p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime));
432 PCPU_SET(switchtime, new_switchtime);
433 PCPU_SET(switchticks, ticks);
434 PCPU_INC(cnt.v_swtch);
435 /* Save our resource usage in our process. */
436 td->td_ru.ru_nvcsw++;
437 rucollect(&p->p_ru, &td->td_ru);
439 * The last thread is left attached to the process
440 * So that the whole bundle gets recycled. Skip
441 * all this stuff if we never had threads.
442 * EXIT clears all sign of other threads when
443 * it goes to single threading, so the last thread always
444 * takes the short path.
446 if (p->p_flag & P_HADTHREADS) {
447 if (p->p_numthreads > 1) {
455 td2 = FIRST_THREAD_IN_PROC(p);
456 sched_exit_thread(td2, td);
459 * The test below is NOT true if we are the
460 * sole exiting thread. P_STOPPED_SNGL is unset
461 * in exit1() after it is the only survivor.
463 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
464 if (p->p_numthreads == p->p_suspcount) {
465 thread_lock(p->p_singlethread);
466 thread_unsuspend_one(p->p_singlethread);
467 thread_unlock(p->p_singlethread);
471 atomic_add_int(&td->td_proc->p_exitthreads, 1);
472 PCPU_SET(deadthread, td);
475 * The last thread is exiting.. but not through exit()
477 * Theoretically this can't happen
478 * exit1() - clears threading flags before coming here
479 * kse_exit() - treats last thread specially
480 * thr_exit() - treats last thread specially
482 * thread_user_enter() - only if more exist
483 * thread_userret() - only if more exist
485 * thread_suspend_check() - only if more exist
487 panic ("thread_exit: Last thread exiting on its own");
492 /* Save our tick information with both the thread and proc locked */
493 ruxagg(&p->p_rux, td);
495 td->td_state = TDS_INACTIVE;
496 CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td);
498 panic("I'm a teapot!");
503 * Do any thread specific cleanups that may be needed in wait()
504 * called with Giant, proc and schedlock not held.
507 thread_wait(struct proc *p)
511 mtx_assert(&Giant, MA_NOTOWNED);
512 KASSERT((p->p_numthreads == 1), ("Multiple threads in wait1()"));
513 td = FIRST_THREAD_IN_PROC(p);
515 if (td->td_standin != NULL) {
516 if (td->td_standin->td_ucred != NULL) {
517 crfree(td->td_standin->td_ucred);
518 td->td_standin->td_ucred = NULL;
520 thread_free(td->td_standin);
521 td->td_standin = NULL;
524 /* Lock the last thread so we spin until it exits cpu_throw(). */
527 /* Wait for any remaining threads to exit cpu_throw(). */
528 while (p->p_exitthreads)
529 sched_relinquish(curthread);
530 cpu_thread_clean(td);
531 crfree(td->td_ucred);
532 thread_reap(); /* check for zombie threads etc. */
536 * Link a thread to a process.
537 * set up anything that needs to be initialized for it to
538 * be used by the process.
540 * Note that we do not link to the proc's ucred here.
541 * The thread is linked as if running but no KSE assigned.
544 * thread_schedule_upcall()
548 thread_link(struct thread *td, struct proc *p)
552 * XXX This can't be enabled because it's called for proc0 before
553 * it's spinlock has been created.
554 * PROC_SLOCK_ASSERT(p, MA_OWNED);
556 td->td_state = TDS_INACTIVE;
558 td->td_flags = TDF_INMEM;
560 LIST_INIT(&td->td_contested);
561 LIST_INIT(&td->td_lprof[0]);
562 LIST_INIT(&td->td_lprof[1]);
563 sigqueue_init(&td->td_sigqueue, p);
564 callout_init(&td->td_slpcallout, CALLOUT_MPSAFE);
565 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
570 * Convert a process with one thread to an unthreaded process.
572 * thread_single(exit) (called from execve and exit)
573 * kse_exit() XXX may need cleaning up wrt KSE stuff
576 thread_unthread(struct thread *td)
578 struct proc *p = td->td_proc;
580 KASSERT((p->p_numthreads == 1), ("Unthreading with >1 threads"));
585 p->p_flag &= ~(P_SA|P_HADTHREADS);
586 td->td_mailbox = NULL;
587 td->td_pflags &= ~(TDP_SA | TDP_CAN_UNBIND);
588 if (td->td_standin != NULL) {
589 thread_zombie(td->td_standin);
590 td->td_standin = NULL;
593 p->p_flag &= ~P_HADTHREADS;
602 thread_unlink(struct thread *td)
604 struct proc *p = td->td_proc;
606 PROC_SLOCK_ASSERT(p, MA_OWNED);
607 TAILQ_REMOVE(&p->p_threads, td, td_plist);
609 /* could clear a few other things here */
610 /* Must NOT clear links to proc! */
614 * Enforce single-threading.
616 * Returns 1 if the caller must abort (another thread is waiting to
617 * exit the process or similar). Process is locked!
618 * Returns 0 when you are successfully the only thread running.
619 * A process has successfully single threaded in the suspend mode when
620 * There are no threads in user mode. Threads in the kernel must be
621 * allowed to continue until they get to the user boundary. They may even
622 * copy out their return values and data before suspending. They may however be
623 * accelerated in reaching the user boundary as we will wake up
624 * any sleeping threads that are interruptable. (PCATCH).
627 thread_single(int mode)
636 mtx_assert(&Giant, MA_NOTOWNED);
637 PROC_LOCK_ASSERT(p, MA_OWNED);
638 KASSERT((td != NULL), ("curthread is NULL"));
640 if ((p->p_flag & P_HADTHREADS) == 0)
643 /* Is someone already single threading? */
644 if (p->p_singlethread != NULL && p->p_singlethread != td)
647 if (mode == SINGLE_EXIT) {
648 p->p_flag |= P_SINGLE_EXIT;
649 p->p_flag &= ~P_SINGLE_BOUNDARY;
651 p->p_flag &= ~P_SINGLE_EXIT;
652 if (mode == SINGLE_BOUNDARY)
653 p->p_flag |= P_SINGLE_BOUNDARY;
655 p->p_flag &= ~P_SINGLE_BOUNDARY;
657 p->p_flag |= P_STOPPED_SINGLE;
659 p->p_singlethread = td;
660 if (mode == SINGLE_EXIT)
661 remaining = p->p_numthreads;
662 else if (mode == SINGLE_BOUNDARY)
663 remaining = p->p_numthreads - p->p_boundary_count;
665 remaining = p->p_numthreads - p->p_suspcount;
666 while (remaining != 1) {
667 if (P_SHOULDSTOP(p) != P_STOPPED_SINGLE)
669 FOREACH_THREAD_IN_PROC(p, td2) {
673 td2->td_flags |= TDF_ASTPENDING;
674 if (TD_IS_INHIBITED(td2)) {
677 if (td->td_flags & TDF_DBSUSPEND)
678 td->td_flags &= ~TDF_DBSUSPEND;
679 if (TD_IS_SUSPENDED(td2))
680 thread_unsuspend_one(td2);
681 if (TD_ON_SLEEPQ(td2) &&
682 (td2->td_flags & TDF_SINTR))
683 sleepq_abort(td2, EINTR);
685 case SINGLE_BOUNDARY:
688 if (TD_IS_SUSPENDED(td2)) {
693 * maybe other inhibited states too?
695 if ((td2->td_flags & TDF_SINTR) &&
696 (td2->td_inhibitors &
697 (TDI_SLEEPING | TDI_SWAPPED)))
698 thread_suspend_one(td2);
703 else if (TD_IS_RUNNING(td2) && td != td2) {
709 if (mode == SINGLE_EXIT)
710 remaining = p->p_numthreads;
711 else if (mode == SINGLE_BOUNDARY)
712 remaining = p->p_numthreads - p->p_boundary_count;
714 remaining = p->p_numthreads - p->p_suspcount;
717 * Maybe we suspended some threads.. was it enough?
724 * Wake us up when everyone else has suspended.
725 * In the mean time we suspend as well.
727 thread_suspend_switch(td);
728 if (mode == SINGLE_EXIT)
729 remaining = p->p_numthreads;
730 else if (mode == SINGLE_BOUNDARY)
731 remaining = p->p_numthreads - p->p_boundary_count;
733 remaining = p->p_numthreads - p->p_suspcount;
735 if (mode == SINGLE_EXIT) {
737 * We have gotten rid of all the other threads and we
738 * are about to either exit or exec. In either case,
739 * we try our utmost to revert to being a non-threaded
742 p->p_singlethread = NULL;
743 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT);
751 * Called in from locations that can safely check to see
752 * whether we have to suspend or at least throttle for a
753 * single-thread event (e.g. fork).
755 * Such locations include userret().
756 * If the "return_instead" argument is non zero, the thread must be able to
757 * accept 0 (caller may continue), or 1 (caller must abort) as a result.
759 * The 'return_instead' argument tells the function if it may do a
760 * thread_exit() or suspend, or whether the caller must abort and back
763 * If the thread that set the single_threading request has set the
764 * P_SINGLE_EXIT bit in the process flags then this call will never return
765 * if 'return_instead' is false, but will exit.
767 * P_SINGLE_EXIT | return_instead == 0| return_instead != 0
768 *---------------+--------------------+---------------------
769 * 0 | returns 0 | returns 0 or 1
770 * | when ST ends | immediatly
771 *---------------+--------------------+---------------------
772 * 1 | thread exits | returns 1
774 * 0 = thread_exit() or suspension ok,
775 * other = return error instead of stopping the thread.
777 * While a full suspension is under effect, even a single threading
778 * thread would be suspended if it made this call (but it shouldn't).
779 * This call should only be made from places where
780 * thread_exit() would be safe as that may be the outcome unless
781 * return_instead is set.
784 thread_suspend_check(int return_instead)
791 mtx_assert(&Giant, MA_NOTOWNED);
792 PROC_LOCK_ASSERT(p, MA_OWNED);
793 while (P_SHOULDSTOP(p) ||
794 ((p->p_flag & P_TRACED) && (td->td_flags & TDF_DBSUSPEND))) {
795 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
796 KASSERT(p->p_singlethread != NULL,
797 ("singlethread not set"));
799 * The only suspension in action is a
800 * single-threading. Single threader need not stop.
801 * XXX Should be safe to access unlocked
802 * as it can only be set to be true by us.
804 if (p->p_singlethread == td)
805 return (0); /* Exempt from stopping. */
807 if ((p->p_flag & P_SINGLE_EXIT) && return_instead)
810 /* Should we goto user boundary if we didn't come from there? */
811 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE &&
812 (p->p_flag & P_SINGLE_BOUNDARY) && return_instead)
815 /* If thread will exit, flush its pending signals */
816 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
817 sigqueue_flush(&td->td_sigqueue);
822 * If the process is waiting for us to exit,
823 * this thread should just suicide.
824 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE.
826 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td))
828 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
829 if (p->p_numthreads == p->p_suspcount + 1) {
830 thread_lock(p->p_singlethread);
831 thread_unsuspend_one(p->p_singlethread);
832 thread_unlock(p->p_singlethread);
838 * When a thread suspends, it just
839 * gets taken off all queues.
841 thread_suspend_one(td);
842 if (return_instead == 0) {
843 p->p_boundary_count++;
844 td->td_flags |= TDF_BOUNDARY;
847 mi_switch(SW_INVOL, NULL);
848 if (return_instead == 0)
849 td->td_flags &= ~TDF_BOUNDARY;
852 if (return_instead == 0)
853 p->p_boundary_count--;
859 thread_suspend_switch(struct thread *td)
864 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
865 PROC_LOCK_ASSERT(p, MA_OWNED);
866 PROC_SLOCK_ASSERT(p, MA_OWNED);
868 * We implement thread_suspend_one in stages here to avoid
869 * dropping the proc lock while the thread lock is owned.
876 TD_SET_SUSPENDED(td);
879 mi_switch(SW_VOL, NULL);
887 thread_suspend_one(struct thread *td)
889 struct proc *p = td->td_proc;
891 PROC_SLOCK_ASSERT(p, MA_OWNED);
892 THREAD_LOCK_ASSERT(td, MA_OWNED);
893 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended"));
896 TD_SET_SUSPENDED(td);
900 thread_unsuspend_one(struct thread *td)
902 struct proc *p = td->td_proc;
904 PROC_SLOCK_ASSERT(p, MA_OWNED);
905 THREAD_LOCK_ASSERT(td, MA_OWNED);
906 KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended"));
907 TD_CLR_SUSPENDED(td);
913 * Allow all threads blocked by single threading to continue running.
916 thread_unsuspend(struct proc *p)
920 PROC_LOCK_ASSERT(p, MA_OWNED);
921 PROC_SLOCK_ASSERT(p, MA_OWNED);
922 if (!P_SHOULDSTOP(p)) {
923 FOREACH_THREAD_IN_PROC(p, td) {
925 if (TD_IS_SUSPENDED(td)) {
926 thread_unsuspend_one(td);
930 } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) &&
931 (p->p_numthreads == p->p_suspcount)) {
933 * Stopping everything also did the job for the single
934 * threading request. Now we've downgraded to single-threaded,
937 thread_lock(p->p_singlethread);
938 thread_unsuspend_one(p->p_singlethread);
939 thread_unlock(p->p_singlethread);
944 * End the single threading mode..
947 thread_single_end(void)
954 PROC_LOCK_ASSERT(p, MA_OWNED);
955 p->p_flag &= ~(P_STOPPED_SINGLE | P_SINGLE_EXIT | P_SINGLE_BOUNDARY);
957 p->p_singlethread = NULL;
959 * If there are other threads they mey now run,
960 * unless of course there is a blanket 'stop order'
961 * on the process. The single threader must be allowed
962 * to continue however as this is a bad place to stop.
964 if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) {
965 FOREACH_THREAD_IN_PROC(p, td) {
967 if (TD_IS_SUSPENDED(td)) {
968 thread_unsuspend_one(td);
977 thread_find(struct proc *p, lwpid_t tid)
981 PROC_LOCK_ASSERT(p, MA_OWNED);
983 FOREACH_THREAD_IN_PROC(p, td) {
984 if (td->td_tid == tid)