gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

int.go (2210B)


      1 package atomics
      2 
      3 import "sync/atomic"
      4 
      5 // Int32 provides user-friendly means of performing atomic operations on int32 types.
      6 type Int32 int32
      7 
      8 // NewInt32 will return a new Int32 instance initialized with zero value.
      9 func NewInt32() *Int32 {
     10 	return new(Int32)
     11 }
     12 
     13 // Add will atomically add int32 delta to value in address contained within i, returning new value.
     14 func (i *Int32) Add(delta int32) int32 {
     15 	return atomic.AddInt32((*int32)(i), delta)
     16 }
     17 
     18 // Store will atomically store int32 value in address contained within i.
     19 func (i *Int32) Store(val int32) {
     20 	atomic.StoreInt32((*int32)(i), val)
     21 }
     22 
     23 // Load will atomically load int32 value at address contained within i.
     24 func (i *Int32) Load() int32 {
     25 	return atomic.LoadInt32((*int32)(i))
     26 }
     27 
     28 // CAS performs a compare-and-swap for a(n) int32 value at address contained within i.
     29 func (i *Int32) CAS(cmp, swp int32) bool {
     30 	return atomic.CompareAndSwapInt32((*int32)(i), cmp, swp)
     31 }
     32 
     33 // Swap atomically stores new int32 value into address contained within i, and returns previous value.
     34 func (i *Int32) Swap(swp int32) int32 {
     35 	return atomic.SwapInt32((*int32)(i), swp)
     36 }
     37 
     38 // Int64 provides user-friendly means of performing atomic operations on int64 types.
     39 type Int64 int64
     40 
     41 // NewInt64 will return a new Int64 instance initialized with zero value.
     42 func NewInt64() *Int64 {
     43 	return new(Int64)
     44 }
     45 
     46 // Add will atomically add int64 delta to value in address contained within i, returning new value.
     47 func (i *Int64) Add(delta int64) int64 {
     48 	return atomic.AddInt64((*int64)(i), delta)
     49 }
     50 
     51 // Store will atomically store int64 value in address contained within i.
     52 func (i *Int64) Store(val int64) {
     53 	atomic.StoreInt64((*int64)(i), val)
     54 }
     55 
     56 // Load will atomically load int64 value at address contained within i.
     57 func (i *Int64) Load() int64 {
     58 	return atomic.LoadInt64((*int64)(i))
     59 }
     60 
     61 // CAS performs a compare-and-swap for a(n) int64 value at address contained within i.
     62 func (i *Int64) CAS(cmp, swp int64) bool {
     63 	return atomic.CompareAndSwapInt64((*int64)(i), cmp, swp)
     64 }
     65 
     66 // Swap atomically stores new int64 value into address contained within i, and returns previous value.
     67 func (i *Int64) Swap(swp int64) int64 {
     68 	return atomic.SwapInt64((*int64)(i), swp)
     69 }