上QQ阅读APP看书,第一时间看更新
Context or processor
We have a global scheduler which takes care of bringing up new M, registering G, and handling system calls. However, it does not handle the actual execution of goroutines. This is done by an entity called Processor, which has its own internal scheduler and a queue called runqueue (runq in code) consisting of goroutines that will be executed in the current context. It also handles switching between various goroutines and so on:
// Denoted as P in runtime code type p struct { id int32 m muintptr // back-link to associated m (nil if idle) runq [256]guintptr //... }
From Go 1.5 onwards, a Go runtime can have a maximum number of GOMAXPROCS Ps running at any given point in the program's lifetime. Of course, we can change this number by either setting the GOMAXPROCS environment variable or by calling the GOMAXPROCS() function.