定制型网站设计报价表,广州网站建设出名 乐云践新,世界500强企业招聘网站,代做网站的公司数据结构与底层实现
Goroutine结构体
stack#xff08;栈内存范围#xff09; 结构体类型#xff0c;包含 lo#xff08;低地址#xff09;和 hi#xff08;高地址#xff09;两个 uintptr 字段#xff0c;描述 Goroutine 的栈内存区间 [lo, hi)。初始栈大小为 2KB栈内存范围 结构体类型包含 lo低地址和 hi高地址两个 uintptr 字段描述 Goroutine 的栈内存区间 [lo, hi)。初始栈大小为 2KB可动态扩容至 1GB。
mMachine 绑定 指向当前运行此 Goroutine 的内核线程M。调度器通过 M 将 Goroutine 映射到操作系统线程。
_panic 和 _defer异常与延迟调用链
_panic指向当前最内层的 panic 结构体链表处理异常传播。_defer指向延迟调用defer链表按后进先出LIFO顺序执行清理操作。
type g struct {// Stack parameters.// stack describes the actual stack memory: [stack.lo, stack.hi).// stackguard0 is the stack pointer compared in the Go stack growth prologue.// It is stack.loStackGuard normally, but can be StackPreempt to trigger a preemption.// stackguard1 is the stack pointer compared in the //go:systemstack stack growth prologue.// It is stack.loStackGuard on g0 and gsignal stacks.// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).stack stack // offset known to runtime/cgostackguard0 uintptr // offset known to liblinkstackguard1 uintptr // offset known to liblink_panic *_panic // innermost panic - offset known to liblink_defer *_defer // innermost deferm *m // current m; offset known to arm liblinksched gobuf......
}
panic结构体
从上述Goroutine结构体的定义我们可以发现每一个Goroutine维护一个panic的链表panic存储在栈上。
// _panic 保存了一个活跃的 panic 信息。// _panic 的值必须仅存在于栈上。// argp 和 link 字段是栈指针但在栈增长时无需特殊处理
// 由于它们是指针类型且 _panic 值仅存在于栈上
// 常规的栈指针调整机制会自动处理这些字段。
type _panic struct {argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblinkarg any // argument to paniclink *_panic // link to earlier panic// startPC and startSP track where _panic.start was called.startPC uintptrstartSP unsafe.Pointer// The current stack frame that were running deferred calls for.sp unsafe.Pointerlr uintptrfp unsafe.Pointer// retpc stores the PC where the panic should jump back to, if the// function last returned by _panic.next() recovers the panic.retpc uintptr// Extra state for handling open-coded defers.deferBitsPtr *uint8slotsPtr unsafe.Pointerrecovered bool // whether this panic has been recoveredgoexit booldeferreturn bool
}
注意事项
golang中每个goroutine维护自己的panic信息并不是全局的所以如果需要捕获panic信息需要在每个goroutine中处理。
所以在下面的这个案例中recover不能捕获到panic信息。如果需要捕获到需要在每个协程中都执行recover的逻辑。
func main() {defer func() {if r : recover(); r ! nil {log.Printf(Recovered from panic: %v, r)os.Exit(1)}}()// 业务代码...go func() {testPanic()}()time.Sleep(1 * time.Second)
}