gitee.com/quant1x/gox@v1.7.6/util/once_multi.go (about)

     1  package util
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  )
     7  
     8  type MultiOnce struct {
     9  	done uint32
    10  	m    sync.Mutex
    11  }
    12  
    13  func (o *MultiOnce) Do(f func()) {
    14  	if atomic.LoadUint32(&o.done) == 0 {
    15  		o.doSlow(f)
    16  	}
    17  }
    18  
    19  func (o *MultiOnce) doSlow(f func()) {
    20  	o.m.Lock()
    21  	defer o.m.Unlock()
    22  	if o.done == 0 {
    23  		defer atomic.StoreUint32(&o.done, 1)
    24  		f()
    25  	}
    26  }
    27  
    28  func (o *MultiOnce) Reset() {
    29  	atomic.StoreUint32(&o.done, 0)
    30  }