* Discard the current thread and exit from its context. * Always called with scheduler locked. * * Because we can't free a thread while we're operating under its context, * push the current thread into our CPU's deadthread holder. This means * we needn't worry about someone else grabbing our context before we * do a cpu_throw(). */
| 846 | * do a cpu_throw(). |
| 847 | */ |
| 848 | void |
| 849 | thread_exit(void) |
| 850 | { |
| 851 | uint64_t runtime, new_switchtime; |
| 852 | struct thread *td; |
| 853 | struct thread *td2; |
| 854 | struct proc *p; |
| 855 | int wakeup_swapper; |
| 856 | |
| 857 | td = curthread; |
| 858 | p = td->td_proc; |
| 859 | |
| 860 | PROC_SLOCK_ASSERT(p, MA_OWNED); |
| 861 | mtx_assert(&Giant, MA_NOTOWNED); |
| 862 | |
| 863 | PROC_LOCK_ASSERT(p, MA_OWNED); |
| 864 | KASSERT(p != NULL, ("thread exiting without a process")); |
| 865 | CTR3(KTR_PROC, "thread_exit: thread %p (pid %ld, %s)", td, |
| 866 | (long)p->p_pid, td->td_name); |
| 867 | SDT_PROBE0(proc, , , lwp__exit); |
| 868 | KASSERT(TAILQ_EMPTY(&td->td_sigqueue.sq_list), ("signal pending")); |
| 869 | MPASS(td->td_realucred == td->td_ucred); |
| 870 | |
| 871 | /* |
| 872 | * drop FPU & debug register state storage, or any other |
| 873 | * architecture specific resources that |
| 874 | * would not be on a new untouched process. |
| 875 | */ |
| 876 | cpu_thread_exit(td); |
| 877 | |
| 878 | /* |
| 879 | * The last thread is left attached to the process |
| 880 | * So that the whole bundle gets recycled. Skip |
| 881 | * all this stuff if we never had threads. |
| 882 | * EXIT clears all sign of other threads when |
| 883 | * it goes to single threading, so the last thread always |
| 884 | * takes the short path. |
| 885 | */ |
| 886 | if (p->p_flag & P_HADTHREADS) { |
| 887 | if (p->p_numthreads > 1) { |
| 888 | atomic_add_int(&td->td_proc->p_exitthreads, 1); |
| 889 | thread_unlink(td); |
| 890 | td2 = FIRST_THREAD_IN_PROC(p); |
| 891 | sched_exit_thread(td2, td); |
| 892 | |
| 893 | /* |
| 894 | * The test below is NOT true if we are the |
| 895 | * sole exiting thread. P_STOPPED_SINGLE is unset |
| 896 | * in exit1() after it is the only survivor. |
| 897 | */ |
| 898 | if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { |
| 899 | if (p->p_numthreads == p->p_suspcount) { |
| 900 | thread_lock(p->p_singlethread); |
| 901 | wakeup_swapper = thread_unsuspend_one( |
| 902 | p->p_singlethread, p, false); |
| 903 | if (wakeup_swapper) |
| 904 | kick_proc0(); |
| 905 | } |