英文:
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-body 和 Function 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论