func schedinit() { // raceinit must be the first call to race detector. // In particular, it must be done before mallocinit below calls racemapshadow. // 从TLS中获取g实例 _g_ := getg()
if raceenabled { _g_.racectx, raceprocctx0 = raceinit() }
// 设置全局线程数上限 sched.maxmcount = 10000
// 初始化一系列函数所在的PC计数器,用于traceback tracebackinit() // 貌似是验证链接器符号的正确性 moduledataverify() // 栈的初始化 stackinit() // 内存分配器初始化 mallocinit() mcommoninit(_g_.m) // 初始化AES HASH算法 alginit() // maps must not be used before this call modulesinit() // provides activeModules typelinksinit() // uses maps, activeModules itabsinit() // uses activeModules
// P个数检查 procs := ncpu if n, ok := atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 { procs = n } if procs > _MaxGomaxprocs { procs = _MaxGomaxprocs } // 所有P的初始化 if procresize(procs) != nil { throw("unknown runnable goroutine during bootstrap") }
if buildVersion == "" { // Condition should never trigger. This code just serves // to ensure runtime·buildVersion is kept in the resulting binary. buildVersion = "unknown" } }