package resync import ( "sync" "sync/atomic" ) // Once is an object that will perform exactly one action // until Reset is called. // See http://golang.org/pkg/sync/#Once type Once struct { m sync.Mutex done uint32 } // Do simulates sync.Once.Do by executing the specified function // only once, until Reset is called. // See http://golang.org/pkg/sync/#Once func (o *Once) Do(f func()) { if atomic.LoadUint32(&o.done) == 1 { return } // Slow-path. o.m.Lock() defer o.m.Unlock() if o.done == 0 { defer atomic.StoreUint32(&o.done, 1) f() } } // Reset indicates that the next call to Do should actually be called // once again. func (o *Once) Reset() { o.m.Lock() defer o.m.Unlock() atomic.StoreUint32(&o.done, 0) }