• Austin Clements's avatar
    runtime: make it possible to exit Go-created threads · eff2b262
    Austin Clements authored
    Currently, threads created by the runtime exist until the whole
    program exits. For #14592 and #20395, we want to be able to exit and
    clean up threads created by the runtime. This commit implements that
    mechanism.
    
    The main difficulty is how to clean up the g0 stack. In cgo mode and
    on Solaris and Windows where the OS manages thread stacks, we simply
    arrange to return from mstart and let the system clean up the thread.
    If the runtime allocated the g0 stack, then we use a new exitThread
    syscall wrapper that arranges to clear a flag in the M once the stack
    can safely be reaped and call the thread termination syscall.
    
    exitThread is based on the existing exit1 wrapper, which was always
    meant to terminate the calling thread. However, exit1 has never been
    used since it was introduced 9 years ago, so it was broken on several
    platforms. exitThread also has the additional complication of having
    to flag that the stack is unused, which requires some tricks on
    platforms that use the stack for syscalls.
    
    This still leaves the problem of how to reap the unused g0 stacks. For
    this, we move the M from allm to a new freem list as part of the M
    exiting. Later, allocm scans the freem list, finds Ms that are marked
    as done with their stack, removes these from the list and frees their
    g0 stacks. This also allows these Ms to be garbage collected.
    
    This CL does not yet use any of this functionality. Follow-up CLs
    will. Likewise, there are no new tests in this CL because we'll need
    follow-up functionality to test it.
    
    Change-Id: Ic851ee74227b6d39c6fc1219fc71b45d3004bc63
    Reviewed-on: https://go-review.googlesource.com/46037
    Run-TryBot: Austin Clements <austin@google.com>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: 's avatarKeith Randall <khr@golang.org>
    eff2b262
os_netbsd.go 7.24 KB