github.com/hslam/atomic@v1.0.0/uint32.go (about)

     1  // Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
     2  // This package is licensed under a MIT license that can be found in the LICENSE file.
     3  
     4  package atomic
     5  
     6  import (
     7  	"sync/atomic"
     8  )
     9  
    10  // Uint32 represents an uint32.
    11  type Uint32 struct {
    12  	v uint32
    13  }
    14  
    15  // NewUint32 returns a new Uint32.
    16  func NewUint32(val uint32) *Uint32 {
    17  	addr := &Uint32{}
    18  	addr.Store(val)
    19  	return addr
    20  }
    21  
    22  // Swap atomically stores new into *addr and returns the previous *addr value.
    23  func (addr *Uint32) Swap(new uint32) (old uint32) {
    24  	return atomic.SwapUint32(&addr.v, new)
    25  }
    26  
    27  // CompareAndSwap executes the compare-and-swap operation for an uint32 value.
    28  func (addr *Uint32) CompareAndSwap(old, new uint32) (swapped bool) {
    29  	return atomic.CompareAndSwapUint32(&addr.v, old, new)
    30  }
    31  
    32  // Add atomically adds delta to *addr and returns the new value.
    33  func (addr *Uint32) Add(delta uint32) (new uint32) {
    34  	return atomic.AddUint32(&addr.v, delta)
    35  }
    36  
    37  // Load atomically loads *addr.
    38  func (addr *Uint32) Load() (val uint32) {
    39  	return atomic.LoadUint32(&addr.v)
    40  }
    41  
    42  // Store atomically stores val into *addr.
    43  func (addr *Uint32) Store(val uint32) {
    44  	atomic.StoreUint32(&addr.v, val)
    45  }