github.com/searKing/golang/go@v1.2.117/sync/atomic/atomic_go1.19.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.19
     6  
     7  package atomic
     8  
     9  import (
    10  	"math"
    11  	"sync/atomic"
    12  	"time"
    13  )
    14  
    15  // Int32 is an atomic wrapper around an int32.
    16  // Deprecated: Use atomic.Int32 instead since go1.19.
    17  type Int32 atomic.Int32
    18  
    19  // NewInt32 creates an Int32.
    20  func NewInt32(i int32) *Int32 {
    21  	var v Int32
    22  	v.Store(i)
    23  	return &v
    24  }
    25  
    26  // Load atomically loads the wrapped value.
    27  func (i *Int32) Load() int32 {
    28  	return (*atomic.Int32)(i).Load()
    29  }
    30  
    31  // Add atomically adds to the wrapped int32 and returns the new value.
    32  func (i *Int32) Add(n int32) int32 {
    33  	return (*atomic.Int32)(i).Add(n)
    34  }
    35  
    36  // Sub atomically subtracts from the wrapped int32 and returns the new value.
    37  func (i *Int32) Sub(n int32) int32 {
    38  	return (*atomic.Int32)(i).Add(-n)
    39  }
    40  
    41  // Inc atomically increments the wrapped int32 and returns the new value.
    42  func (i *Int32) Inc() int32 {
    43  	return (*atomic.Int32)(i).Add(1)
    44  }
    45  
    46  // Dec atomically decrements the wrapped int32 and returns the new value.
    47  func (i *Int32) Dec() int32 {
    48  	return i.Sub(1)
    49  }
    50  
    51  // CAS is an atomic compare-and-swap.
    52  func (i *Int32) CAS(old, new int32) bool {
    53  	return (*atomic.Int32)(i).CompareAndSwap(old, new)
    54  }
    55  
    56  // Store atomically stores the passed value.
    57  func (i *Int32) Store(n int32) {
    58  	(*atomic.Int32)(i).Store(n)
    59  }
    60  
    61  // Swap atomically swaps the wrapped int32 and returns the old value.
    62  func (i *Int32) Swap(n int32) int32 {
    63  	return (*atomic.Int32)(i).Swap(n)
    64  }
    65  
    66  // Int64 is an atomic wrapper around an int64.
    67  // Deprecated: Use atomic.Int64 instead since go1.19.
    68  type Int64 atomic.Int64
    69  
    70  // NewInt64 creates an Int64.
    71  func NewInt64(i int64) *Int64 {
    72  	var v Int64
    73  	v.Store(i)
    74  	return &v
    75  }
    76  
    77  // Load atomically loads the wrapped value.
    78  func (i *Int64) Load() int64 {
    79  	return (*atomic.Int64)(i).Load()
    80  }
    81  
    82  // Add atomically adds to the wrapped int64 and returns the new value.
    83  func (i *Int64) Add(n int64) int64 {
    84  	return (*atomic.Int64)(i).Add(n)
    85  }
    86  
    87  // Sub atomically subtracts from the wrapped int64 and returns the new value.
    88  func (i *Int64) Sub(n int64) int64 {
    89  	return (*atomic.Int64)(i).Add(-n)
    90  }
    91  
    92  // Inc atomically increments the wrapped int64 and returns the new value.
    93  func (i *Int64) Inc() int64 {
    94  	return i.Add(1)
    95  }
    96  
    97  // Dec atomically decrements the wrapped int64 and returns the new value.
    98  func (i *Int64) Dec() int64 {
    99  	return i.Sub(1)
   100  }
   101  
   102  // CAS is an atomic compare-and-swap.
   103  func (i *Int64) CAS(old, new int64) bool {
   104  	return (*atomic.Int64)(i).CompareAndSwap(old, new)
   105  }
   106  
   107  // Store atomically stores the passed value.
   108  func (i *Int64) Store(n int64) {
   109  	(*atomic.Int64)(i).Store(n)
   110  }
   111  
   112  // Swap atomically swaps the wrapped int64 and returns the old value.
   113  func (i *Int64) Swap(n int64) int64 {
   114  	return (*atomic.Int64)(i).Swap(n)
   115  }
   116  
   117  // Uint32 is an atomic wrapper around an uint32.
   118  // Deprecated: Use atomic.Uint32 instead since go1.19.
   119  type Uint32 atomic.Uint32
   120  
   121  // NewUint32 creates a Uint32.
   122  func NewUint32(i uint32) *Uint32 {
   123  	var v Uint32
   124  	v.Store(i)
   125  	return &v
   126  }
   127  
   128  // Load atomically loads the wrapped value.
   129  func (i *Uint32) Load() uint32 {
   130  	return (*atomic.Uint32)(i).Load()
   131  }
   132  
   133  // Add atomically adds to the wrapped uint32 and returns the new value.
   134  func (i *Uint32) Add(n uint32) uint32 {
   135  	return (*atomic.Uint32)(i).Add(n)
   136  }
   137  
   138  // Sub atomically subtracts from the wrapped uint32 and returns the new value.
   139  func (i *Uint32) Sub(n uint32) uint32 {
   140  	return (*atomic.Uint32)(i).Add(^(n - 1))
   141  }
   142  
   143  // Inc atomically increments the wrapped uint32 and returns the new value.
   144  func (i *Uint32) Inc() uint32 {
   145  	return i.Add(1)
   146  }
   147  
   148  // Dec atomically decrements the wrapped int32 and returns the new value.
   149  func (i *Uint32) Dec() uint32 {
   150  	return i.Sub(1)
   151  }
   152  
   153  // CAS is an atomic compare-and-swap.
   154  func (i *Uint32) CAS(old, new uint32) bool {
   155  	return (*atomic.Uint32)(i).CompareAndSwap(old, new)
   156  }
   157  
   158  // Store atomically stores the passed value.
   159  func (i *Uint32) Store(n uint32) {
   160  	(*atomic.Uint32)(i).Store(n)
   161  }
   162  
   163  // Swap atomically swaps the wrapped uint32 and returns the old value.
   164  func (i *Uint32) Swap(n uint32) uint32 {
   165  	return (*atomic.Uint32)(i).Swap(n)
   166  }
   167  
   168  // Uint64 is an atomic wrapper around a uint64.
   169  // Deprecated: Use atomic.Uint64 instead since go1.19.
   170  type Uint64 atomic.Uint64
   171  
   172  // NewUint64 creates a Uint64.
   173  func NewUint64(i uint64) *Uint64 {
   174  	var v Uint64
   175  	v.Store(i)
   176  	return &v
   177  }
   178  
   179  // Load atomically loads the wrapped value.
   180  func (i *Uint64) Load() uint64 {
   181  	return (*atomic.Uint64)(i).Load()
   182  }
   183  
   184  // Add atomically adds to the wrapped uint64 and returns the new value.
   185  func (i *Uint64) Add(n uint64) uint64 {
   186  	return (*atomic.Uint64)(i).Add(n)
   187  }
   188  
   189  // Sub atomically subtracts from the wrapped uint64 and returns the new value.
   190  func (i *Uint64) Sub(n uint64) uint64 {
   191  	return (*atomic.Uint64)(i).Add(^(n - 1))
   192  }
   193  
   194  // Inc atomically increments the wrapped uint64 and returns the new value.
   195  func (i *Uint64) Inc() uint64 {
   196  	return i.Add(1)
   197  }
   198  
   199  // Dec atomically decrements the wrapped uint64 and returns the new value.
   200  func (i *Uint64) Dec() uint64 {
   201  	return i.Sub(1)
   202  }
   203  
   204  // CAS is an atomic compare-and-swap.
   205  func (i *Uint64) CAS(old, new uint64) bool {
   206  	return (*atomic.Uint64)(i).CompareAndSwap(old, new)
   207  }
   208  
   209  // Store atomically stores the passed value.
   210  func (i *Uint64) Store(n uint64) {
   211  	(*atomic.Uint64)(i).Store(n)
   212  }
   213  
   214  // Swap atomically swaps the wrapped uint64 and returns the old value.
   215  func (i *Uint64) Swap(n uint64) uint64 {
   216  	return (*atomic.Uint64)(i).Swap(n)
   217  }
   218  
   219  // Bool is an atomic Boolean.
   220  // Deprecated: Use atomic.Bool instead since go1.19.
   221  type Bool atomic.Bool
   222  
   223  // NewBool creates a Bool.
   224  func NewBool(i bool) *Bool {
   225  	var v Bool
   226  	v.Store(i)
   227  	return &v
   228  }
   229  
   230  // Load atomically loads the Boolean.
   231  func (b *Bool) Load() bool {
   232  	return (*atomic.Bool)(b).Load()
   233  }
   234  
   235  // CAS is an atomic compare-and-swap.
   236  func (b *Bool) CAS(old, new bool) bool {
   237  	return (*atomic.Bool)(b).CompareAndSwap(old, new)
   238  }
   239  
   240  // Store atomically stores the passed value.
   241  func (b *Bool) Store(new bool) {
   242  	(*atomic.Bool)(b).Store(new)
   243  }
   244  
   245  // Swap sets the given value and returns the previous value.
   246  func (b *Bool) Swap(new bool) bool {
   247  	return (*atomic.Bool)(b).Swap(new)
   248  }
   249  
   250  // Toggle atomically negates the Boolean and returns the previous value.
   251  func (b *Bool) Toggle() bool {
   252  	for {
   253  		old := b.Load()
   254  		if b.CAS(old, !old) {
   255  			return old
   256  		}
   257  	}
   258  }
   259  
   260  // Float32 is an atomic wrapper around float32.
   261  type Float32 atomic.Uint32
   262  
   263  // NewFloat32 creates a Float32.
   264  func NewFloat32(i float32) *Float32 {
   265  	var v Float32
   266  	v.Store(i)
   267  	return &v
   268  }
   269  
   270  // Load atomically loads the wrapped value.
   271  func (f *Float32) Load() float32 {
   272  	return math.Float32frombits((*atomic.Uint32)(f).Load())
   273  }
   274  
   275  // Store atomically stores the passed value.
   276  func (f *Float32) Store(s float32) {
   277  	(*atomic.Uint32)(f).Store(math.Float32bits(s))
   278  }
   279  
   280  // Add atomically adds to the wrapped float32 and returns the new value.
   281  func (f *Float32) Add(s float32) float32 {
   282  	for {
   283  		old := f.Load()
   284  		new := old + s
   285  		if f.CAS(old, new) {
   286  			return new
   287  		}
   288  	}
   289  }
   290  
   291  // Sub atomically subtracts from the wrapped float32 and returns the new value.
   292  func (f *Float32) Sub(s float32) float32 {
   293  	return f.Add(-s)
   294  }
   295  
   296  // CAS is an atomic compare-and-swap.
   297  func (f *Float32) CAS(old, new float32) bool {
   298  	return (*atomic.Uint32)(f).CompareAndSwap(math.Float32bits(old), math.Float32bits(new))
   299  }
   300  
   301  // Float64 is an atomic wrapper around float64.
   302  type Float64 atomic.Uint64
   303  
   304  // NewFloat64 creates a Float64.
   305  func NewFloat64(i float64) *Float64 {
   306  	var v Float64
   307  	v.Store(i)
   308  	return &v
   309  }
   310  
   311  // Load atomically loads the wrapped value.
   312  func (f *Float64) Load() float64 {
   313  	return math.Float64frombits((*atomic.Uint64)(f).Load())
   314  }
   315  
   316  // Store atomically stores the passed value.
   317  func (f *Float64) Store(s float64) {
   318  	(*atomic.Uint64)(f).Store(math.Float64bits(s))
   319  }
   320  
   321  // Add atomically adds to the wrapped float64 and returns the new value.
   322  func (f *Float64) Add(s float64) float64 {
   323  	for {
   324  		old := f.Load()
   325  		new := old + s
   326  		if f.CAS(old, new) {
   327  			return new
   328  		}
   329  	}
   330  }
   331  
   332  // Sub atomically subtracts from the wrapped float64 and returns the new value.
   333  func (f *Float64) Sub(s float64) float64 {
   334  	return f.Add(-s)
   335  }
   336  
   337  // CAS is an atomic compare-and-swap.
   338  func (f *Float64) CAS(old, new float64) bool {
   339  	return (*atomic.Uint64)(f).CompareAndSwap(math.Float64bits(old), math.Float64bits(new))
   340  }
   341  
   342  // Duration is an atomic wrapper around time.Duration
   343  // https://godoc.org/time#Duration
   344  type Duration atomic.Int64
   345  
   346  // NewDuration creates a Duration.
   347  func NewDuration(d time.Duration) *Duration {
   348  	var v Duration
   349  	v.Store(d)
   350  	return &v
   351  }
   352  
   353  // Load atomically loads the wrapped value.
   354  func (d *Duration) Load() time.Duration {
   355  	return time.Duration((*atomic.Int64)(d).Load())
   356  }
   357  
   358  // Store atomically stores the passed value.
   359  func (d *Duration) Store(n time.Duration) {
   360  	(*atomic.Int64)(d).Store(int64(n))
   361  }
   362  
   363  // Add atomically adds to the wrapped time.Duration and returns the new value.
   364  func (d *Duration) Add(n time.Duration) time.Duration {
   365  	return time.Duration((*atomic.Int64)(d).Add(int64(n)))
   366  }
   367  
   368  // Sub atomically subtracts from the wrapped time.Duration and returns the new value.
   369  func (d *Duration) Sub(n time.Duration) time.Duration {
   370  	return time.Duration((*atomic.Int64)(d).Add(int64(-n)))
   371  }
   372  
   373  // Swap atomically swaps the wrapped time.Duration and returns the old value.
   374  func (d *Duration) Swap(n time.Duration) time.Duration {
   375  	return time.Duration((*atomic.Int64)(d).Swap(int64(n)))
   376  }
   377  
   378  // CAS is an atomic compare-and-swap.
   379  func (d *Duration) CAS(old, new time.Duration) bool {
   380  	return (*atomic.Int64)(d).CompareAndSwap(int64(old), int64(new))
   381  }
   382  
   383  // Value shadows the type of the same name from sync/atomic
   384  // https://godoc.org/sync/atomic#Value
   385  type Value atomic.Value