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