From: Austin Clements Date: Fri, 19 Apr 2019 01:37:51 +0000 (-0400) Subject: runtime: document P statuses X-Git-Tag: go1.13beta1~550 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b0c616560567b16da3c0d0d2d790d0c21fd6ef92;p=gostls13.git runtime: document P statuses We're about to change some of these rules, so it's about time we wrote them down! For #10958, #24543. Change-Id: I3efce0c44b53bfb6f31ce2d299809b2b4eb329f0 Reviewed-on: https://go-review.googlesource.com/c/go/+/172857 Reviewed-by: Russ Cox --- diff --git a/src/runtime/runtime2.go b/src/runtime/runtime2.go index 6d4633821b..fb607898c8 100644 --- a/src/runtime/runtime2.go +++ b/src/runtime/runtime2.go @@ -98,10 +98,51 @@ const ( const ( // P status - _Pidle = iota - _Prunning // Only this P is allowed to change from _Prunning. + + // _Pidle means a P is not being used to run user code or the + // scheduler. Typically, it's on the idle P list and available + // to the scheduler, but it may just be transitioning between + // other states. + // + // The P is owned by the idle list or by whatever is + // transitioning its state. Its run queue is empty. + _Pidle = iota + + // _Prunning means a P is owned by an M and is being used to + // run user code or the scheduler. Only the M that owns this P + // is allowed to change the P's status from _Prunning. The M + // may transition the P to _Pidle (if it has no more work to + // do), _Psyscall (when entering a syscall), or _Pgcstop (to + // halt for the GC). The M may also hand ownership of the P + // off directly to another M (e.g., to schedule a locked G). + _Prunning + + // _Psyscall means a P is not running user code. It has + // affinity to an M in a syscall but is not owned by it and + // may be stolen by another M. This is similar to _Pidle but + // uses lightweight transitions and maintains M affinity. + // + // Leaving _Psyscall must be done with a CAS, either to steal + // or retake the P. Note that there's an ABA hazard: even if + // an M successfully CASes its original P back to _Prunning + // after a syscall, it must understand the P may have been + // used by another M in the interim. _Psyscall + + // _Pgcstop means a P is halted for STW and owned by the M + // that stopped the world. The M that stopped the world + // continues to use its P, even in _Pgcstop. Transitioning + // from _Prunning to _Pgcstop causes an M to release its P and + // park. + // + // The P retains its run queue and startTheWorld will restart + // the scheduler on Ps with non-empty run queues. _Pgcstop + + // _Pdead means a P is no longer used (GOMAXPROCS shrank). We + // reuse Ps if GOMAXPROCS increases. A dead P is mostly + // stripped of its resources, though a few things remain + // (e.g., trace buffers). _Pdead )