// Once is an object that will perform exactly one action. 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/x86), // and fewer instructions (to calculate offset) on other architectures. done uint32 m Mutex }
func(o *Once) Do(f func()) { if atomic.LoadUint32(&o.done) == 0 { // check // Outlined slow-path to allow inlining of the fast-path. o.doSlow(f) } }