如何在Golang的MPG模型中获取g?

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

how to get g in golang MPG model

问题

我正在阅读一些关于golang(go1.6.2 linux/amd64)的代码运行时,有人可以帮我理解runtime/stubs.go文件中getg()函数的底层机制吗?

// getg返回指向当前g的指针。
// 编译器将对此函数的调用重写为直接获取g的指令(从TLS或专用寄存器获取)。
func getg() *g

在这里,getg()函数是如何操作的?这个函数的具体内容是什么?

英文:

I'm reading some runtime of code of golang(go1.6.2 linux/amd64), Could someone help me to understand the underlying mechanism of getg() function in runtime/stubs.go file?

// getg returns the pointer to the current g.
// The compiler rewrites calls to this function into instructions
// that fetch the g directly (from TLS or from the dedicated register).
func getg() *g

how do the getg() function operate here? What is the body of this function?

答案1

得分: 5

请参考 https://stackoverflow.com/questions/14189606/function-signature-with-no-function-bodyFunction Declarations

对于 runtime.getg,代码是由编译器直接生成的。

英文:

See https://stackoverflow.com/questions/14189606/function-signature-with-no-function-body, and the specification for Function Declarations.

In the case of runtime.getg the code is directly emitted by the compiler.

答案2

得分: 2

这段代码的意思是,runtime.getg() 函数的调用会被编译器重写成直接获取 g 的指令。具体的代码实现是与架构相关的,可以在 {src}/src/cmd/compile/internal/{arch}/ggen.go 文件中找到,其中 {src} 是你的 Go 代码的源目录,{arch} 是特定架构的目录。例如,在 amd64 架构下,对应的文件路径是 {src}/cmd/compile/internal/amd64/ggen.go

英文:

It's not being executed—exactly because, as stated in the comment

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

As you can obtain yourself via something like

grep -rFw getg /usr/share/go-1.7/src/

the code which is emitted when the compiler sees a call to runtime.getg()
is architecture-dependent and is located in the files

{src}/src/cmd/compile/internal/{arch}/ggen.go

(for Go 1.7)—where {src} is the source directory of your Go code
and {arch} is an architecture-specific directory.

Say, for amd64, it's {src}/cmd/compile/internal/amd64/ggen.go.

huangapple
  • 本文由 发表于 2017年2月14日 21:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/42227934.html
匿名

发表评论

匿名网友

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

确定