github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/runtime/preempt.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Goroutine preemption 6 // 7 // A goroutine can be preempted at any safe-point. Currently, there 8 // are a few categories of safe-points: 9 // 10 // 1. A blocked safe-point occurs for the duration that a goroutine is 11 // descheduled, blocked on synchronization, or in a system call. 12 // 13 // 2. Synchronous safe-points occur when a running goroutine checks 14 // for a preemption request. 15 // 16 // 3. Asynchronous safe-points occur at any instruction in user code 17 // where the goroutine can be safely paused and a conservative 18 // stack and register scan can find stack roots. The runtime can 19 // stop a goroutine at an async safe-point using a signal. 20 // 21 // At both blocked and synchronous safe-points, a goroutine's CPU 22 // state is minimal and the garbage collector has complete information 23 // about its entire stack. This makes it possible to deschedule a 24 // goroutine with minimal space, and to precisely scan a goroutine's 25 // stack. 26 // 27 // Synchronous safe-points are implemented by overloading the stack 28 // bound check in function prologues. To preempt a goroutine at the 29 // next synchronous safe-point, the runtime poisons the goroutine's 30 // stack bound to a value that will cause the next stack bound check 31 // to fail and enter the stack growth implementation, which will 32 // detect that it was actually a preemption and redirect to preemption 33 // handling. 34 // 35 // Preemption at asynchronous safe-points is implemented by suspending 36 // the thread using an OS mechanism (e.g., signals) and inspecting its 37 // state to determine if the goroutine was at an asynchronous 38 // safe-point. Since the thread suspension itself is generally 39 // asynchronous, it also checks if the running goroutine wants to be 40 // preempted, since this could have changed. If all conditions are 41 // satisfied, it adjusts the signal context to make it look like the 42 // signaled thread just called asyncPreempt and resumes the thread. 43 // asyncPreempt spills all registers and enters the scheduler. 44 // 45 // (An alternative would be to preempt in the signal handler itself. 46 // This would let the OS save and restore the register state and the 47 // runtime would only need to know how to extract potentially 48 // pointer-containing registers from the signal context. However, this 49 // would consume an M for every preempted G, and the scheduler itself 50 // is not designed to run from a signal handler, as it tends to 51 // allocate memory and start threads in the preemption path.) 52 53 package runtime