上卷 程序设计
中卷 标准库
- bufio 1.18
- bytes 1.18
- io 1.18
- container 1.18
- encoding 1.18
- crypto 1.18
- hash 1.18
- index 1.18
- sort 1.18
- context 1.18
- database 1.18
- connection
- query
- queryrow
- exec
- prepare
- transaction
- scan & null
- context
- tcp
- udp
- http
- server
- handler
- client
- h2、tls
- url
- rpc
- exec
- signal
- embed 1.18
- plugin 1.18
- reflect 1.18
- runtime 1.18
- KeepAlived
- ReadMemStats
- SetFinalizer
- Stack
- sync 1.18
- atomic
- mutex
- rwmutex
- waitgroup
- cond
- once
- map
- pool
- copycheck
- nocopy
- unsafe 1.18
- fmt 1.18
- log 1.18
- math 1.18
- time 1.18
- timer
下卷 运行时
源码剖析
附录
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
4.3.2 新建
每个 G 对象都自带一块栈内存(stack),默认 2 KB。
// stack.go // The minimum size of stack used by Go code _StackMin = 2048
// proc.go func newproc1(fn *funcval, callergp *g, callerpc uintptr) *g { // 复用或新建。 newg := gfget(_p_) if newg == nil { newg = malg(_StackMin) casgstatus(newg, _Gidle, _Gdead) allgadd(newg) } }
// proc.go // Allocate a new g, with a stack big enough for stacksize bytes. func malg(stacksize int32) *g { newg := new(g) if stacksize >= 0 { stacksize = round2(_StackSystem + stacksize) newg.stack = stackalloc(uint32(stacksize)) newg.stackguard0 = newg.stack.lo + _StackGuard newg.stackguard1 = ^uintptr(0) *(*uintptr)(unsafe.Pointer(newg.stack.lo)) = 0 } return newg }
所有 G 对象创建后被存入 allgs 内,以供查阅。G 会被复用,但不会销毁。
var ( // allgs contains all Gs ever created (including dead Gs), and thus // never shrinks. // // Access via the slice is protected by allglock or stop-the-world. // Readers that cannot take the lock may (carefully!) use the atomic // variables below. allglock mutex allgs []*g // allglen and allgptr are atomic variables that contain len(allgs) and // &allgs[0] respectively. Proper ordering depends on totally-ordered // loads and stores. Writes are protected by allglock. // // allgptr is updated before allglen. Readers should read allglen // before allgptr to ensure that allglen is always <= len(allgptr). New // Gs appended during the race can be missed. For a consistent view of // all Gs, allglock must be held. // // allgptr copies should always be stored as a concrete type or // unsafe.Pointer, not uintptr, to ensure that GC can still reach it // even if it points to a stale array. allglen uintptr allgptr **g )
func allgadd(gp *g) { lock(&allglock) allgs = append(allgs, gp) if &allgs[0] != allgptr { atomicstorep(unsafe.Pointer(&allgptr), unsafe.Pointer(&allgs[0])) } atomic.Storeuintptr(&allglen, uintptr(len(allgs))) unlock(&allglock) }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论