Lines Matching refs:td
147 static int thread_unsuspend_one(struct thread *td, struct proc *p,
149 static void thread_free_batched(struct thread *td);
305 tidbatch_add(struct tidbatch *tb, struct thread *td) in tidbatch_add() argument
310 tb->tab[tb->n] = td->td_tid; in tidbatch_add()
352 tdcountbatch_add(struct tdcountbatch *tb, struct thread *td __unused) in tdcountbatch_add()
383 struct thread *td; in thread_ctor() local
385 td = (struct thread *)mem; in thread_ctor()
386 TD_SET_STATE(td, TDS_INACTIVE); in thread_ctor()
387 td->td_lastcpu = td->td_oncpu = NOCPU; in thread_ctor()
394 td->td_critnest = 1; in thread_ctor()
395 td->td_lend_user_pri = PRI_MAX; in thread_ctor()
397 audit_thread_alloc(td); in thread_ctor()
400 kdtrace_thread_ctor(td); in thread_ctor()
402 umtx_thread_alloc(td); in thread_ctor()
403 MPASS(td->td_sel == NULL); in thread_ctor()
413 struct thread *td; in thread_dtor() local
415 td = (struct thread *)mem; in thread_dtor()
419 switch (TD_GET_STATE(td)) { in thread_dtor()
438 audit_thread_free(td); in thread_dtor()
441 kdtrace_thread_dtor(td); in thread_dtor()
444 osd_thread_exit(td); in thread_dtor()
445 ast_kclear(td); in thread_dtor()
446 seltdfini(td); in thread_dtor()
455 struct thread *td; in thread_init() local
457 td = (struct thread *)mem; in thread_init()
459 td->td_allocdomain = vm_phys_domain(vtophys(td)); in thread_init()
460 td->td_sleepqueue = sleepq_alloc(); in thread_init()
461 td->td_turnstile = turnstile_alloc(); in thread_init()
462 td->td_rlqe = NULL; in thread_init()
463 EVENTHANDLER_DIRECT_INVOKE(thread_init, td); in thread_init()
464 umtx_thread_init(td); in thread_init()
465 td->td_kstack = 0; in thread_init()
466 td->td_sel = NULL; in thread_init()
476 struct thread *td; in thread_fini() local
478 td = (struct thread *)mem; in thread_fini()
479 EVENTHANDLER_DIRECT_INVOKE(thread_fini, td); in thread_fini()
480 rlqentry_free(td->td_rlqe); in thread_fini()
481 turnstile_free(td->td_turnstile); in thread_fini()
482 sleepq_free(td->td_sleepqueue); in thread_fini()
483 umtx_thread_fini(td); in thread_fini()
484 MPASS(td->td_sel == NULL); in thread_fini()
496 proc_linkup0(struct proc *p, struct thread *td) in proc_linkup0() argument
499 proc_linkup(p, td); in proc_linkup0()
503 proc_linkup(struct proc *p, struct thread *td) in proc_linkup() argument
514 thread_link(td, p); in proc_linkup()
518 ast_suspend(struct thread *td, int tda __unused) in ast_suspend() argument
522 p = td->td_proc; in ast_suspend()
598 thread_zombie(struct thread *td) in thread_zombie() argument
603 tdd = &thread_domain_data[td->td_allocdomain]; in thread_zombie()
606 td->td_zombie = ztd; in thread_zombie()
608 (uintptr_t *)&ztd, (uintptr_t)td)) in thread_zombie()
618 thread_stash(struct thread *td) in thread_stash() argument
620 atomic_subtract_rel_int(&td->td_proc->p_exitthreads, 1); in thread_stash()
621 thread_zombie(td); in thread_stash()
782 struct thread *td; in thread_alloc() local
790 td = uma_zalloc(thread_zone, M_WAITOK); in thread_alloc()
791 KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); in thread_alloc()
792 if (!vm_thread_new(td, pages)) { in thread_alloc()
793 uma_zfree(thread_zone, td); in thread_alloc()
798 td->td_tid = tid; in thread_alloc()
799 bzero(&td->td_sa.args, sizeof(td->td_sa.args)); in thread_alloc()
800 kmsan_thread_alloc(td); in thread_alloc()
801 cpu_thread_alloc(td); in thread_alloc()
802 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td); in thread_alloc()
803 return (td); in thread_alloc()
807 thread_alloc_stack(struct thread *td, int pages) in thread_alloc_stack() argument
810 KASSERT(td->td_kstack == 0, in thread_alloc_stack()
812 if (!vm_thread_new(td, pages)) in thread_alloc_stack()
814 cpu_thread_alloc(td); in thread_alloc_stack()
822 thread_free_batched(struct thread *td) in thread_free_batched() argument
825 lock_profile_thread_exit(td); in thread_free_batched()
826 if (td->td_cpuset) in thread_free_batched()
827 cpuset_rel(td->td_cpuset); in thread_free_batched()
828 td->td_cpuset = NULL; in thread_free_batched()
829 cpu_thread_free(td); in thread_free_batched()
830 if (td->td_kstack != 0) in thread_free_batched()
831 vm_thread_dispose(td); in thread_free_batched()
832 callout_drain(&td->td_slpcallout); in thread_free_batched()
836 td->td_tid = -1; in thread_free_batched()
837 kmsan_thread_free(td); in thread_free_batched()
838 uma_zfree(thread_zone, td); in thread_free_batched()
842 thread_free(struct thread *td) in thread_free() argument
846 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td); in thread_free()
847 tid = td->td_tid; in thread_free()
848 thread_free_batched(td); in thread_free()
865 thread_cow_get(struct thread *newtd, struct thread *td) in thread_cow_get() argument
868 MPASS(td->td_realucred == td->td_ucred); in thread_cow_get()
869 newtd->td_realucred = crcowget(td->td_realucred); in thread_cow_get()
871 newtd->td_limit = lim_hold(td->td_limit); in thread_cow_get()
872 newtd->td_cowgen = td->td_cowgen; in thread_cow_get()
876 thread_cow_free(struct thread *td) in thread_cow_free() argument
879 if (td->td_realucred != NULL) in thread_cow_free()
880 crcowfree(td); in thread_cow_free()
881 if (td->td_limit != NULL) in thread_cow_free()
882 lim_free(td->td_limit); in thread_cow_free()
886 thread_cow_update(struct thread *td) in thread_cow_update() argument
892 p = td->td_proc; in thread_cow_update()
896 td->td_cowgen = p->p_cowgen; in thread_cow_update()
905 thread_cow_synced(struct thread *td) in thread_cow_synced() argument
909 p = td->td_proc; in thread_cow_synced()
911 MPASS(td->td_cowgen != p->p_cowgen); in thread_cow_synced()
912 MPASS(td->td_ucred == p->p_ucred); in thread_cow_synced()
913 MPASS(td->td_limit == p->p_limit); in thread_cow_synced()
914 td->td_cowgen = p->p_cowgen; in thread_cow_synced()
930 struct thread *td; in thread_exit() local
935 td = curthread; in thread_exit()
936 p = td->td_proc; in thread_exit()
943 CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, in thread_exit()
944 (long)p->p_pid, td->td_name); in thread_exit()
946 KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); in thread_exit()
947 MPASS(td->td_realucred == td->td_ucred); in thread_exit()
954 cpu_thread_exit(td); in thread_exit()
966 atomic_add_int(&td->td_proc->p_exitthreads, 1); in thread_exit()
967 thread_unlink(td); in thread_exit()
969 sched_exit_thread(td2, td); in thread_exit()
986 PCPU_SET(deadthread, td); in thread_exit()
999 if (PMC_PROC_IS_USING_PMCS(td->td_proc)) { in thread_exit()
1000 PMC_SWITCH_CONTEXT(td, PMC_FN_CSW_OUT); in thread_exit()
1001 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT, NULL); in thread_exit()
1003 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL); in thread_exit()
1007 thread_lock(td); in thread_exit()
1013 td->td_runtime += runtime; in thread_exit()
1014 td->td_incruntime += runtime; in thread_exit()
1020 td->td_ru.ru_nvcsw++; in thread_exit()
1021 ruxagg_locked(p, td); in thread_exit()
1022 rucollect(&p->p_ru, &td->td_ru); in thread_exit()
1025 TD_SET_STATE(td, TDS_INACTIVE); in thread_exit()
1027 witness_thread_exit(td); in thread_exit()
1029 CTR1(KTR_PROC, "thread_exit: cpu_throw() thread %p", td); in thread_exit()
1030 sched_throw(td); in thread_exit()
1042 struct thread *td; in thread_wait() local
1047 td = FIRST_THREAD_IN_PROC(p); in thread_wait()
1049 thread_lock(td); in thread_wait()
1050 thread_unlock(td); in thread_wait()
1051 lock_profile_thread_exit(td); in thread_wait()
1052 cpuset_rel(td->td_cpuset); in thread_wait()
1053 td->td_cpuset = NULL; in thread_wait()
1054 cpu_thread_clean(td); in thread_wait()
1055 thread_cow_free(td); in thread_wait()
1056 callout_drain(&td->td_slpcallout); in thread_wait()
1066 thread_link(struct thread *td, struct proc *p) in thread_link() argument
1074 TD_SET_STATE(td, TDS_INACTIVE); in thread_link()
1075 td->td_proc = p; in thread_link()
1076 td->td_flags = TDF_INMEM; in thread_link()
1078 LIST_INIT(&td->td_contested); in thread_link()
1079 LIST_INIT(&td->td_lprof[0]); in thread_link()
1080 LIST_INIT(&td->td_lprof[1]); in thread_link()
1082 SLIST_INIT(&td->td_epochs); in thread_link()
1084 sigqueue_init(&td->td_sigqueue, p); in thread_link()
1085 callout_init(&td->td_slpcallout, 1); in thread_link()
1086 TAILQ_INSERT_TAIL(&p->p_threads, td, td_plist); in thread_link()
1095 thread_unlink(struct thread *td) in thread_unlink() argument
1097 struct proc *p = td->td_proc; in thread_unlink()
1101 MPASS(SLIST_EMPTY(&td->td_epochs)); in thread_unlink()
1104 TAILQ_REMOVE(&p->p_threads, td, td_plist); in thread_unlink()
1220 struct thread *td; in thread_single() local
1224 td = curthread; in thread_single()
1234 KASSERT((mode == SINGLE_ALLPROC && td->td_proc != p) || in thread_single()
1235 (mode != SINGLE_ALLPROC && td->td_proc == p), in thread_single()
1236 ("mode %d proc %p curproc %p", mode, p, td->td_proc)); in thread_single()
1255 if (p->p_singlethread != NULL && p->p_singlethread != td) in thread_single()
1272 p->p_singlethread = td; in thread_single()
1279 if (td2 == td) in thread_single()
1308 thread_suspend_switch(td, p); in thread_single()
1327 sched_relinquish(td); in thread_single()
1345 if (td2 == td) in thread_single()
1363 struct thread *td; in thread_suspend_check_needed() local
1365 td = curthread; in thread_suspend_check_needed()
1366 p = td->td_proc; in thread_suspend_check_needed()
1369 (td->td_dbgflags & TDB_SUSPEND) != 0)); in thread_suspend_check_needed()
1408 struct thread *td; in thread_suspend_check() local
1412 td = curthread; in thread_suspend_check()
1413 p = td->td_proc; in thread_suspend_check()
1426 if (p->p_singlethread == td) in thread_suspend_check()
1440 if ((td->td_flags & TDF_SBDRY) != 0) { in thread_suspend_check()
1443 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) != in thread_suspend_check()
1446 return (TD_SBDRY_INTR(td) ? TD_SBDRY_ERRNO(td) : 0); in thread_suspend_check()
1454 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { in thread_suspend_check()
1462 (p->p_sysent->sv_thread_detach)(td); in thread_suspend_check()
1463 umtx_thread_exit(td); in thread_suspend_check()
1464 kern_thr_exit(td); in thread_suspend_check()
1480 thread_lock(td); in thread_suspend_check()
1485 thread_suspend_one(td); in thread_suspend_check()
1488 td->td_flags |= TDF_BOUNDARY; in thread_suspend_check()
1517 thread_check_susp(struct thread *td, bool sleep) in thread_check_susp() argument
1526 if (!td_ast_pending(td, TDA_SUSPEND)) in thread_check_susp()
1529 p = td->td_proc; in thread_check_susp()
1534 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) in thread_check_susp()
1541 thread_suspend_switch(struct thread *td, struct proc *p) in thread_suspend_switch() argument
1544 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); in thread_suspend_switch()
1551 if (p == td->td_proc) { in thread_suspend_switch()
1556 thread_lock(td); in thread_suspend_switch()
1557 ast_unsched_locked(td, TDA_SUSPEND); in thread_suspend_switch()
1558 TD_SET_SUSPENDED(td); in thread_suspend_switch()
1559 sched_sleep(td, 0); in thread_suspend_switch()
1569 thread_suspend_one(struct thread *td) in thread_suspend_one() argument
1573 p = td->td_proc; in thread_suspend_one()
1575 THREAD_LOCK_ASSERT(td, MA_OWNED); in thread_suspend_one()
1576 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); in thread_suspend_one()
1578 ast_unsched_locked(td, TDA_SUSPEND); in thread_suspend_one()
1579 TD_SET_SUSPENDED(td); in thread_suspend_one()
1580 sched_sleep(td, 0); in thread_suspend_one()
1584 thread_unsuspend_one(struct thread *td, struct proc *p, bool boundary) in thread_unsuspend_one() argument
1587 THREAD_LOCK_ASSERT(td, MA_OWNED); in thread_unsuspend_one()
1588 KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); in thread_unsuspend_one()
1589 TD_CLR_SUSPENDED(td); in thread_unsuspend_one()
1590 td->td_flags &= ~TDF_ALLPROCSUSP; in thread_unsuspend_one()
1591 if (td->td_proc == p) { in thread_unsuspend_one()
1594 if (boundary && (td->td_flags & TDF_BOUNDARY) != 0) { in thread_unsuspend_one()
1595 td->td_flags &= ~TDF_BOUNDARY; in thread_unsuspend_one()
1599 return (setrunnable(td, 0)); in thread_unsuspend_one()
1603 thread_run_flash(struct thread *td) in thread_run_flash() argument
1607 p = td->td_proc; in thread_run_flash()
1610 if (TD_ON_SLEEPQ(td)) in thread_run_flash()
1611 sleepq_remove_nested(td); in thread_run_flash()
1613 thread_lock(td); in thread_run_flash()
1615 THREAD_LOCK_ASSERT(td, MA_OWNED); in thread_run_flash()
1616 KASSERT(TD_IS_SUSPENDED(td), ("Thread not suspended")); in thread_run_flash()
1618 TD_CLR_SUSPENDED(td); in thread_run_flash()
1623 if (setrunnable(td, 0)) in thread_run_flash()
1633 struct thread *td; in thread_unsuspend() local
1640 FOREACH_THREAD_IN_PROC(p, td) { in thread_unsuspend()
1641 thread_lock(td); in thread_unsuspend()
1642 if (TD_IS_SUSPENDED(td)) in thread_unsuspend()
1643 wakeup_swapper |= thread_unsuspend_one(td, p, in thread_unsuspend()
1646 thread_unlock(td); in thread_unsuspend()
1671 struct thread *td; in thread_single_end() local
1699 FOREACH_THREAD_IN_PROC(p, td) { in thread_single_end()
1700 thread_lock(td); in thread_single_end()
1701 if (TD_IS_SUSPENDED(td)) { in thread_single_end()
1702 wakeup_swapper |= thread_unsuspend_one(td, p, in thread_single_end()
1705 thread_unlock(td); in thread_single_end()
1732 struct thread *td; in tdfind_hash() local
1739 LIST_FOREACH(td, TIDHASH(tid), td_hash) { in tdfind_hash()
1740 if (td->td_tid != tid) { in tdfind_hash()
1744 p = td->td_proc; in tdfind_hash()
1746 td = NULL; in tdfind_hash()
1751 LIST_REMOVE(td, td_hash); in tdfind_hash()
1752 LIST_INSERT_HEAD(TIDHASH(td->td_tid), in tdfind_hash()
1753 td, td_hash); in tdfind_hash()
1763 if (td == NULL) in tdfind_hash()
1766 *tdp = td; in tdfind_hash()
1774 struct thread *td; in tdfind() local
1776 td = curthread; in tdfind()
1777 if (td->td_tid == tid) { in tdfind()
1778 if (pid != -1 && td->td_proc->p_pid != pid) in tdfind()
1780 PROC_LOCK(td->td_proc); in tdfind()
1781 return (td); in tdfind()
1785 if (!tdfind_hash(tid, pid, &p, &td)) in tdfind()
1788 if (td->td_tid != tid) { in tdfind()
1792 if (td->td_proc != p) { in tdfind()
1800 return (td); in tdfind()
1805 tidhash_add(struct thread *td) in tidhash_add() argument
1807 rw_wlock(TIDHASHLOCK(td->td_tid)); in tidhash_add()
1808 LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); in tidhash_add()
1809 rw_wunlock(TIDHASHLOCK(td->td_tid)); in tidhash_add()
1813 tidhash_remove(struct thread *td) in tidhash_remove() argument
1816 rw_wlock(TIDHASHLOCK(td->td_tid)); in tidhash_remove()
1817 LIST_REMOVE(td, td_hash); in tidhash_remove()
1818 rw_wunlock(TIDHASHLOCK(td->td_tid)); in tidhash_remove()