目录
程序入口
在执行一系列检查和初始化(创建多少个P,与M0关联)后,进入runtime.main,创建main goroutine,执行mian.mian。

一开始GO语言的调度只有M和G。每个M获取G都要加锁。所以加入了P。一个P关联一个M,这样就可以从P的本地队列获取任务。
创建一个协程主要是调用newproc函数,而newproc函数是在G0创建的,为什么是G0?因为他的栈空间大!


创建完新G(协程栈)后,会赋状态Grunnable,.表示可以运行的G..于是看情况调度到别的P
协程主动让出:

万一所有M都在忙,无法执行时间到的协程,怎么办?


被动让出:
1.监控线程会监控运行时间过长的协程.
2.协程执行过程中栈增大到一定量后,1.14
3,信号量,.当检测到信号后,注入一个异步抢占函数调用,处理完信号后返回立刻执行被注入的异步抢占函数,而这个函数就有schedul逻辑 1.14
4,系统调用.监控线程还会抢占处在系统调用的P.因为执行系统调用就要切换到G0栈.在系统调用没执行完之前,这个M和G就抱团了.此时P可能关联到其他M.

schedule
1看是否绑定了当前G,有就执行
2,看看GC是否等待
3,有没有要执行的runtimer
4,全局G调度部分到本地G
5,最后再次 本地G, 全局G,netpoll,steal from other p ..若是别人的G则要看看这个G是否有绑定的M.
5.G改为Grunning 状态,执行
监控线程
1,保障计时器正常执行
2,网络轮询
3,强制长时间运行的G 或处于系统调用的P
4,强制GC