解析Golang sync.Once源码
2022-12-05 本文已影响0人
robertzhai
数据结构
type Once struct {
// done indicates whether the action has been performed.
// It is first in the struct because it is used in the hot path.
// The hot path is inlined at every call site.
// Placing done first allows more compact instructions on some architectures (amd64/386),
// and fewer instructions (to calculate offset) on other architectures.
done uint32 // 初始值为0表示还未执行过,1表示已经执行过
m Mutex // done 的lock
}
核心代码
func (o *Once) Do(f func()) {
// Note: Here is an incorrect implementation of Do:
//
// if atomic.CompareAndSwapUint32(&o.done, 0, 1) {
// f()
// }
//
// Do guarantees that when it returns, f has finished.
// This implementation would not implement that guarantee:
// given two simultaneous calls, the winner of the cas would
// call f, and the second would return immediately, without
// waiting for the first's call to f to complete.
// This is why the slow path falls back to a mutex, and why
// the atomic.StoreUint32 must be delayed until after f returns.
if atomic.LoadUint32(&o.done) == 0 { // 先检查是否没执行
// Outlined slow-path to allow inlining of the fast-path.
o.doSlow(f) // 没执行就调用 doSlow
}
}
func (o *Once) doSlow(f func()) {
o.m.Lock() // 加锁
defer o.m.Unlock()
// 采用双重检测机制 加锁判断done是否为零
if o.done == 0 { // 防止多个goroutine 并发进入doSlow 等待lock 后进入 再检查一次
defer atomic.StoreUint32(&o.done, 1) // 返回后设置已经执行
f() // 执行业务函数
}
}