之前我们已经了解了GMP的基础知识,对G、M、P各自的职责和分工都有了大致的认识,本篇文章主要是介绍一个goroutine是怎么被创建出来的
从一个简单的例子开始
func TestNewGoroutine(t *testing.T) {
go func() {
sayHello()
}()
}
func sayHello() {
println("hello gmp")
}
编译文件 go tool compile -N -l -S new_goroutine_test.go
得到汇编代码
"".TestNewGoroutine STEXT size=71 args=0x8 locals=0x18
0x0000 00000 (new_goroutine_test.go:5) TEXT "".TestNewGoroutine(SB), ABIInternal, $24-8
0x0000 00000 (new_goroutine_test.go:5) MOVQ (TLS), CX
0x0009 00009 (new_goroutine_test.go:5) CMPQ SP, 16(CX)
0x000d 00013 (new_goroutine_test.go:5) PCDATA $0, $-2
0x000d 00013 (new_goroutine_test.go:5) JLS 64
0x000f 00015 (new_goroutine_test.go:5) PCDATA $0, $-1
0x000f 00015 (new_goroutine_test.go:5) SUBQ $24, SP
0x0013 00019 (new_goroutine_test.go:5) MOVQ BP, 16(SP)
0x0018 00024 (new_goroutine_test.go:5) LEAQ 16(SP), BP
0x001d 00029 (new_goroutine_test.go:5) FUNCDATA $0, gclocals·2a5305abe05176240e61b8620e19a815(SB)
0x001d 00029 (new_goroutine_test.go:5) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x001d 00029 (new_goroutine_test.go:6) MOVL $0, (SP)
0x0024 00036 (new_goroutine_test.go:6) LEAQ "".TestNewGoroutine