github.com/coocood/badger@v1.5.1-0.20200528065104-c02ac3616d04/epoch/epoch.go (about) 1 package epoch 2 3 import "sync/atomic" 4 5 // The least significant bit of epoch is active flag. 6 type epoch uint64 7 8 func (e epoch) isActive() bool { 9 return uint64(e)&1 == 1 10 } 11 12 func (e epoch) activate() epoch { 13 return epoch(uint64(e) | 1) 14 } 15 16 func (e epoch) deactivate() epoch { 17 return epoch(uint64(e) & ^uint64(1)) 18 } 19 20 func (e epoch) sub(a epoch) int { 21 return int((uint64(e) >> 1) - (uint64(a) >> 1)) 22 } 23 24 func (e epoch) successor() epoch { 25 return epoch(uint64(e) + 2) 26 } 27 28 type atomicEpoch struct { 29 epoch uint64 30 } 31 32 func (e *atomicEpoch) load() epoch { 33 return (epoch)(atomic.LoadUint64(&e.epoch)) 34 } 35 36 func (e *atomicEpoch) store(new epoch) { 37 atomic.StoreUint64(&e.epoch, uint64(new)) 38 }