timer包的定时器中的startTimer 函数体是什么

屏幕快照 2019-09-12 下午3.09.50
clipboard.png

这边的startTimer(*runtimeTimer)的实现呢

阅读 6.3k
2 个回答

在 runtimetime.go 里面
内部实现的时候,通过linkename 把别的包的实现代码定义为本地包

// startTimer adds t to the timer heap.
//go:linkname startTimer time.startTimer
func startTimer(t *timer) {
    if raceenabled {
        racerelease(unsafe.Pointer(t))
    }
    addtimer(t)
}

官方说明

//go:linkname localname [importpath.name]
The //go:linkname directive instructs the compiler to use “importpath.name” as the object file symbol name for the variable or function declared as “localname” in the source code. If the “importpath.name” argument is omitted, the directive uses the symbol's default object file symbol name and only has the effect of making the symbol accessible to other packages. Because this directive can subvert the type system and package modularity, it is only enabled in files that have imported "unsafe".

楼上正解,这里其实就是在编译的时候会链接到 go-path/src/runtime/time.go;可以到 golang 的安装目录或者 github 仓库下载源码看看 runtime 目录里的内容。

推荐问题