聊聊Goroutine-结构体、全局变量(三)

先来看看goroutine调度器相关重要结构体,位于Go源代码下runtime/runtime2.go。

因其成员变量极多,细节极其复杂,所以只看与调度器相关的成员变量。

stack:记录goroutine使用的栈信息,包括栈顶和栈底的位置信息。

// Stack describes a Go execution stack.// The bounds of the stack are exactly [lo, hi),// with no implicit data structures on either side.//用于记录goroutine使用的栈的起始和结束位置type stack struct {      lo uintptr    // 栈顶,指向内存低地址    hi uintptr    // 栈底,指向内存高地址}

gobuf:保存goroutine的调度信息,主要是CPU几个寄存器的值。

type gobuf struct {
  
      // The offsets of sp, pc, and g are known to (hard-coded in) libmach.    //    // ctxt is unusual with respect to GC: it may be a    // heap-allocated funcval, so GC needs to track it, but it    // needs to be set and cleared from assembly, where it's    // difficult to have write barriers. However, ctxt is really a    // saved, live register, and we only ever exchange it between    // the real register and the gobuf. Hence, we treat it as a    // root during stack scanning, which means assembly that saves    // and restores it doesn't need write barriers. It's still    // typed as a pointer so that any other writes from Go get    // write barriers.    sp   uintptr  // 保存CPU的rsp寄存器的值    pc   uintptr  // 保存CPU的rip寄存器的值    g    guintptr // 记录当前这个gobuf对象属于哪个goroutine    ctxt unsafe.Pointer     // 保存系统调用的返回值,因为从系统调用返回之后如果p被其它工作线程抢占,    // 则这个goroutine会被放入全局运行队列被其它工作线程调度,其它线程需要知道系统调用的返回值。    ret  sys.Uintreg      lr   uintptr     // 保存CPU的rip寄存器的值    bp   uintptr // for GOEXPERIMENT=framepointer}

g:用于代表一个goroutine,保存了goroutine的所有信息,包括栈。

// 前文所说的g结构体,它代表了一个goroutinetype g struct {
  
      // Stack parameters.    // stack describes the actual stack memory: [stack.lo, stack.hi).<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luyaran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值