github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/esync/sync_atomic_float32.go (about)

     1  package esync
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"sync/atomic"
     7  )
     8  
     9  // AtomicFloat32 support atomic operation
    10  type AtomicFloat32 struct {
    11  	value uint32
    12  }
    13  
    14  func (t *AtomicFloat32) Set(v float32) {
    15  	atomic.StoreUint32(&t.value, math.Float32bits(v))
    16  }
    17  
    18  func (t *AtomicFloat32) Get() float32 {
    19  	return math.Float32frombits(atomic.LoadUint32(&t.value))
    20  }
    21  
    22  func (t *AtomicFloat32) Swap(v float32) float32 {
    23  	return math.Float32frombits(atomic.SwapUint32(&t.value, math.Float32bits(v)))
    24  }
    25  
    26  func (t *AtomicFloat32) String() string {
    27  	return fmt.Sprint(t.Get())
    28  }