]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: document P statuses
authorAustin Clements <austin@google.com>
Fri, 19 Apr 2019 01:37:51 +0000 (21:37 -0400)
committerAustin Clements <austin@google.com>
Thu, 25 Apr 2019 22:08:33 +0000 (22:08 +0000)
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 <rsc@golang.org>
src/runtime/runtime2.go

index 6d4633821bec1b330a0c4996137925709ea9589a..fb607898c8adb75f4059f0626cf713e0c9c11520 100644 (file)
@@ -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
 )