在Golang源代码中,g struct的定义在哪里?

huangapple go评论81阅读模式
英文:

Where is g struct define in golang source code?

问题

在golang的源代码中,有一些代码如下:

MOVL	(g_sched+gobuf_sp)(SI), SP	// sp = m->g0->sched.sp

runtime/stubs.go中,我找到了以下代码:

func getg() *g

但是我找不到getg函数或g结构体,它们在哪里?

英文:

In golang source code, there is some code like:

MOVL	(g_sched+gobuf_sp)(SI), SP	// sp = m->g0->sched.sp

And in runtime/stubs.go I found

func getg() *g

But i can not find getg function or g struct, where is it?

答案1

得分: 3

这个结构体在runtime2.go中。

> type g struct {
> // Stack parameters.
> // stack描述了实际的堆栈内存:[stack.lo, stack.hi)。
> // stackguard0是在Go堆栈增长序言中与堆栈指针进行比较的。

你可以使用godef快速回答这类问题。

函数的注释描述了它的工作原理:

> 编译器将对此函数的调用重写为直接获取g的指令(从TLS或专用寄存器获取)。

要了解编译器是如何实现这一点的,请查看cmd/compile/internal/gc/typecheck.go

> if ..... && n.Left.Sym.Name == "getg" { ...

英文:

The struct is in runtime2.go.

> 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.

You can use godef to quickly answer questions like that on your own.

The function's comment describes how that works:

> The compiler rewrites calls to this function into instructions
> that fetch the g directly (from TLS or from the dedicated register).

To see how the compiler does this, check cmd/compile/internal/gc/typecheck.go:

> if ..... && n.Left.Sym.Name == "getg" { ...

huangapple
  • 本文由 发表于 2016年9月7日 13:02:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/39361537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定