github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/time/monotonic/atomic.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package monotonic
     4  
     5  import (
     6  	"sync/atomic"
     7  
     8  	"github.com/GeniusesGroup/libgo/protocol"
     9  )
    10  
    11  // Atomic same as Time is monotonic clock is for measuring time.
    12  // Just due to 32bit hardwares alignment problem,
    13  // we suggest don't use Time for atomic operation and Use this type for any atomic purposes.
    14  type Atomic struct {
    15  	atomic.Int64
    16  }
    17  
    18  func (a *Atomic) Load() Time               { return Time(a.Int64.Load()) }
    19  func (a *Atomic) Store(t Time)             { a.Int64.Store(int64(t)) }
    20  func (a *Atomic) Swap(new Time) (old Time) { return Time(a.Int64.Swap(int64(new))) }
    21  func (a *Atomic) CompareAndSwap(old, new Time) (swapped bool) {
    22  	return a.Int64.CompareAndSwap(int64(old), int64(new))
    23  }
    24  func (a *Atomic) Add(d protocol.Duration) { a.Int64.Add(int64(d)) }