github.com/puellanivis/breton@v0.2.16/lib/metrics/internal/atomic/float.go (about)

     1  package atomic
     2  
     3  import (
     4  	"math"
     5  	"sync/atomic"
     6  )
     7  
     8  // Float is a replica of the prometheus spinlock counter based on float64.
     9  type Float uint64
    10  
    11  // Inc increments the Float by one (1).
    12  func (f *Float) Inc() {
    13  	f.Add(1)
    14  }
    15  
    16  // Dec decrements the Float by one (1).
    17  func (f *Float) Dec() {
    18  	f.Add(-1)
    19  }
    20  
    21  // Add increments the Float by a given floating-point value (may be negative).
    22  func (f *Float) Add(delta float64) {
    23  	var o, n uint64
    24  	for {
    25  		o = atomic.LoadUint64((*uint64)(f))
    26  		n = math.Float64bits(math.Float64frombits(o) + delta)
    27  		if atomic.CompareAndSwapUint64((*uint64)(f), o, n) {
    28  			return
    29  		}
    30  	}
    31  }
    32  
    33  // Sub decrements the Float by a given floating-point value (may be negative).
    34  func (f *Float) Sub(delta float64) {
    35  	f.Add(-delta)
    36  }
    37  
    38  // Set assigns a value into the Float, overriding any previous value.
    39  func (f *Float) Set(value float64) {
    40  	atomic.StoreUint64((*uint64)(f), math.Float64bits(value))
    41  }
    42  
    43  // Get returns a momentary value of the Float.
    44  func (f *Float) Get() float64 {
    45  	return math.Float64frombits(atomic.LoadUint64((*uint64)(f)))
    46  }