github.com/influx6/npkg@v0.8.8/natomic/natomic.go (about)

     1  package natomic
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  )
     7  
     8  //******************************************************
     9  // Switches
    10  //******************************************************
    11  
    12  // BoolSwitch defines a concurrent safe bool switch.
    13  //
    14  // Once created, you must not copy a BoolSwitch.
    15  type BoolSwitch struct {
    16  	m  sync.Mutex
    17  	fl bool
    18  }
    19  
    20  // Flip flips the current value of the giving switch to
    21  // giving boolean.
    22  func (f *BoolSwitch) Flip(b bool) {
    23  	f.m.Lock()
    24  	f.fl = b
    25  	f.m.Unlock()
    26  }
    27  
    28  // State returns the current state of the switch.
    29  func (f *BoolSwitch) Read() bool {
    30  	var state bool
    31  	f.m.Lock()
    32  	state = f.fl
    33  	f.m.Unlock()
    34  	return state
    35  }
    36  
    37  // Int64Switch defines a concurrent safe bool switch.
    38  //
    39  // Once created, you must not copy a Int64Switch.
    40  type Int64Switch struct {
    41  	fl int64
    42  }
    43  
    44  // Flip flips the current value of the giving switch to
    45  // giving boolean.
    46  func (f *Int64Switch) Flip(b int64) {
    47  	atomic.StoreInt64(&f.fl, b)
    48  }
    49  
    50  // State returns the current state of the switch.
    51  func (f *Int64Switch) Read() int64 {
    52  	return atomic.LoadInt64(&f.fl)
    53  }
    54  
    55  // Uint64Switch defines a concurrent safe bool switch.
    56  //
    57  // Once created, you must not copy a Uint64Switch.
    58  type Uint64Switch struct {
    59  	fl uint64
    60  }
    61  
    62  // Flip flips the current value of the giving switch to
    63  // giving boolean.
    64  func (f *Uint64Switch) Flip(b uint64) {
    65  	atomic.StoreUint64(&f.fl, b)
    66  }
    67  
    68  // State returns the current state of the switch.
    69  func (f *Uint64Switch) Read() uint64 {
    70  	return atomic.LoadUint64(&f.fl)
    71  }
    72  
    73  // Int64Counter defines a concurrent safe bool switch.
    74  //
    75  // Once created, you must not copy a Int64Counter.
    76  type Int64Counter struct {
    77  	fl int64
    78  }
    79  
    80  // Add increments the current value of the giving counter to
    81  // giving boolean.
    82  func (f *Int64Counter) Add(b int64) {
    83  	atomic.AddInt64(&f.fl, b)
    84  }
    85  
    86  // State returns the current state of the switch.
    87  func (f *Int64Counter) Read() int64 {
    88  	return atomic.LoadInt64(&f.fl)
    89  }
    90  
    91  // Uint64Counter defines a concurrent safe bool switch.
    92  //
    93  // Once created, you must not copy a Uint64Counter.
    94  type Uint64Counter struct {
    95  	fl uint64
    96  }
    97  
    98  // Add increments the current value of the giving counter to
    99  // giving boolean.
   100  func (f *Uint64Counter) Add(b uint64) {
   101  	atomic.AddUint64(&f.fl, b)
   102  }
   103  
   104  // State returns the current state of the switch.
   105  func (f *Uint64Counter) Read() uint64 {
   106  	return atomic.LoadUint64(&f.fl)
   107  }
   108  
   109  // IntSwitch defines a concurrent safe bool switch.
   110  //
   111  // Once created, you must not copy a IntSwitch.
   112  type IntSwitch struct {
   113  	fl int64
   114  }
   115  
   116  // Flip flips the current value of the giving switch to
   117  // giving boolean.
   118  func (f *IntSwitch) Flip(b int) {
   119  	atomic.StoreInt64(&f.fl, int64(b))
   120  }
   121  
   122  // State returns the current state of the switch.
   123  func (f *IntSwitch) Read() int {
   124  	return int(atomic.LoadInt64(&f.fl))
   125  }
   126  
   127  // UintSwitch defines a concurrent safe bool switch.
   128  //
   129  // Once created, you must not copy a UintSwitch.
   130  type UintSwitch struct {
   131  	fl uint64
   132  }
   133  
   134  // Flip flips the current value of the giving switch to
   135  // giving boolean.
   136  func (f *UintSwitch) Flip(b uint) {
   137  	atomic.StoreUint64(&f.fl, uint64(b))
   138  }
   139  
   140  // State returns the current state of the switch.
   141  func (f *UintSwitch) Read() uint {
   142  	return uint(atomic.LoadUint64(&f.fl))
   143  }
   144  
   145  // IntCounter defines a concurrent safe bool switch.
   146  //
   147  // Once created, you must not copy a IntCounter.
   148  type IntCounter struct {
   149  	fl int64
   150  }
   151  
   152  // Add increments the current value of the giving counter to
   153  // giving boolean.
   154  func (f *IntCounter) Add(b int) {
   155  	atomic.AddInt64(&f.fl, int64(b))
   156  }
   157  
   158  // State returns the current state of the switch.
   159  func (f *IntCounter) Read() int {
   160  	return int(atomic.LoadInt64(&f.fl))
   161  }
   162  
   163  // UintCounter defines a concurrent safe bool switch.
   164  //
   165  // Once created, you must not copy a UintCounter.
   166  type UintCounter struct {
   167  	fl uint64
   168  }
   169  
   170  // Add increments the current value of the giving counter to
   171  // giving boolean.
   172  func (f *UintCounter) Add(b uint) {
   173  	atomic.AddUint64(&f.fl, uint64(b))
   174  }
   175  
   176  // State returns the current state of the switch.
   177  func (f *UintCounter) Read() uint {
   178  	return uint(atomic.LoadUint64(&f.fl))
   179  }