github.com/sandwich-go/boost@v1.3.29/xsync/atomic.go (about)

     1  package xsync
     2  
     3  import (
     4  	"sync/atomic"
     5  	"time"
     6  )
     7  
     8  // AtomicBool is an atomic type-safe wrapper for bool values.
     9  type AtomicBool struct{ v int32 }
    10  
    11  // Set Store atomically stores the passed bool.
    12  func (b *AtomicBool) Set(v bool) {
    13  	if v {
    14  		atomic.StoreInt32(&b.v, 1)
    15  	} else {
    16  		atomic.StoreInt32(&b.v, 0)
    17  	}
    18  }
    19  
    20  // Get Load atomically loads the wrapped bool.
    21  func (b *AtomicBool) Get() bool {
    22  	return atomic.LoadInt32(&b.v) == 1
    23  }
    24  
    25  // AtomicInt32 is an atomic type-safe wrapper for int32 values.
    26  type AtomicInt32 int32
    27  
    28  // Add atomically adds n to *AtomicInt32 and returns the new value.
    29  func (i *AtomicInt32) Add(n int32) int32 {
    30  	return atomic.AddInt32((*int32)(i), n)
    31  }
    32  
    33  // Set Store atomically stores the passed int32.
    34  func (i *AtomicInt32) Set(n int32) {
    35  	atomic.StoreInt32((*int32)(i), n)
    36  }
    37  
    38  // Get Load atomically loads the wrapped int32.
    39  func (i *AtomicInt32) Get() int32 {
    40  	return atomic.LoadInt32((*int32)(i))
    41  }
    42  
    43  // CompareAndSwap executes the compare-and-swap operation for a int32 value.
    44  func (i *AtomicInt32) CompareAndSwap(oldval, newval int32) (swapped bool) {
    45  	return atomic.CompareAndSwapInt32((*int32)(i), oldval, newval)
    46  }
    47  
    48  // AtomicUint32 is an atomic type-safe wrapper for uint32 values.
    49  type AtomicUint32 uint32
    50  
    51  // Add atomically adds n to *AtomicUint32 and returns the new value.
    52  func (i *AtomicUint32) Add(n uint32) uint32 {
    53  	return atomic.AddUint32((*uint32)(i), n)
    54  }
    55  
    56  // Set Store atomically stores the passed uint32.
    57  func (i *AtomicUint32) Set(n uint32) {
    58  	atomic.StoreUint32((*uint32)(i), n)
    59  }
    60  
    61  // Get Load atomically loads the wrapped uint32.
    62  func (i *AtomicUint32) Get() uint32 {
    63  	return atomic.LoadUint32((*uint32)(i))
    64  }
    65  
    66  // CompareAndSwap executes the compare-and-swap operation for a uint32 value.
    67  func (i *AtomicUint32) CompareAndSwap(oldval, newval uint32) (swapped bool) {
    68  	return atomic.CompareAndSwapUint32((*uint32)(i), oldval, newval)
    69  }
    70  
    71  // AtomicInt64 is an atomic type-safe wrapper for int64 values.
    72  type AtomicInt64 int64
    73  
    74  // Add atomically adds n to *AtomicInt64 and returns the new value.
    75  func (i *AtomicInt64) Add(n int64) int64 {
    76  	return atomic.AddInt64((*int64)(i), n)
    77  }
    78  
    79  // Set Store atomically stores the passed int64.
    80  func (i *AtomicInt64) Set(n int64) {
    81  	atomic.StoreInt64((*int64)(i), n)
    82  }
    83  
    84  // Get Load atomically loads the wrapped int64.
    85  func (i *AtomicInt64) Get() int64 {
    86  	return atomic.LoadInt64((*int64)(i))
    87  }
    88  
    89  // CompareAndSwap executes the compare-and-swap operation for a int64 value.
    90  func (i *AtomicInt64) CompareAndSwap(oldval, newval int64) (swapped bool) {
    91  	return atomic.CompareAndSwapInt64((*int64)(i), oldval, newval)
    92  }
    93  
    94  // AtomicUint64 is an atomic type-safe wrapper for uint64 values.
    95  type AtomicUint64 uint64
    96  
    97  // Add atomically adds n to *AtomicUint64 and returns the new value.
    98  func (i *AtomicUint64) Add(n uint64) uint64 {
    99  	return atomic.AddUint64((*uint64)(i), n)
   100  }
   101  
   102  // Set Store atomically stores the passed uint64.
   103  func (i *AtomicUint64) Set(n uint64) {
   104  	atomic.StoreUint64((*uint64)(i), n)
   105  }
   106  
   107  // Get Load atomically loads the wrapped uint64.
   108  func (i *AtomicUint64) Get() uint64 {
   109  	return atomic.LoadUint64((*uint64)(i))
   110  }
   111  
   112  // CompareAndSwap executes the compare-and-swap operation for a uint64 value.
   113  func (i *AtomicUint64) CompareAndSwap(oldval, newval uint64) (swapped bool) {
   114  	return atomic.CompareAndSwapUint64((*uint64)(i), oldval, newval)
   115  }
   116  
   117  // AtomicDuration is an atomic type-safe wrapper for duration values.
   118  type AtomicDuration int64
   119  
   120  // Add atomically adds duration to *AtomicDuration and returns the new value.
   121  func (d *AtomicDuration) Add(duration time.Duration) time.Duration {
   122  	return time.Duration(atomic.AddInt64((*int64)(d), int64(duration)))
   123  }
   124  
   125  // Set Store atomically stores the passed time.Duration.
   126  func (d *AtomicDuration) Set(duration time.Duration) {
   127  	atomic.StoreInt64((*int64)(d), int64(duration))
   128  }
   129  
   130  // Get Load atomically loads the wrapped time.Duration.
   131  func (d *AtomicDuration) Get() time.Duration {
   132  	return time.Duration(atomic.LoadInt64((*int64)(d)))
   133  }
   134  
   135  // CompareAndSwap executes the compare-and-swap operation for an time.Duration value
   136  func (d *AtomicDuration) CompareAndSwap(oldval, newval time.Duration) (swapped bool) {
   137  	return atomic.CompareAndSwapInt64((*int64)(d), int64(oldval), int64(newval))
   138  }
   139  
   140  // AtomicString is an atomic type-safe wrapper for string values.
   141  type AtomicString struct {
   142  	v atomic.Value
   143  }
   144  
   145  var _zeroString string
   146  
   147  // NewAtomicString creates a new String.
   148  func NewAtomicString(v string) *AtomicString {
   149  	x := &AtomicString{}
   150  	if v != _zeroString {
   151  		x.Set(v)
   152  	}
   153  	return x
   154  }
   155  
   156  // Get Load atomically loads the wrapped string.
   157  func (x *AtomicString) Get() string {
   158  	if v := x.v.Load(); v != nil {
   159  		return v.(string)
   160  	}
   161  	return _zeroString
   162  }
   163  
   164  // Set Store atomically stores the passed string.
   165  func (x *AtomicString) Set(v string) {
   166  	x.v.Store(v)
   167  }