...

Source file src/github.com/launchdarkly/go-server-sdk/v6/internal/concurrent.go

Documentation: github.com/launchdarkly/go-server-sdk/v6/internal

     1  package internal
     2  
     3  import (
     4  	"sync/atomic"
     5  )
     6  
     7  // AtomicBoolean is a simple atomic boolean type based on sync/atomic. Since sync/atomic supports
     8  // only integer types, the implementation uses an int32. (Note: we should be able to get rid of
     9  // this once our minimum Go version becomes 1.19 or higher.)
    10  type AtomicBoolean struct {
    11  	value int32
    12  }
    13  
    14  // Get returns the current value.
    15  func (a *AtomicBoolean) Get() bool {
    16  	return int32ToBoolean(atomic.LoadInt32(&a.value))
    17  }
    18  
    19  // Set updates the value.
    20  func (a *AtomicBoolean) Set(value bool) {
    21  	atomic.StoreInt32(&a.value, booleanToInt32(value))
    22  }
    23  
    24  // GetAndSet atomically updates the value and returns the previous value.
    25  func (a *AtomicBoolean) GetAndSet(value bool) bool {
    26  	return int32ToBoolean(atomic.SwapInt32(&a.value, booleanToInt32(value)))
    27  }
    28  
    29  func booleanToInt32(value bool) int32 {
    30  	if value {
    31  		return 1
    32  	}
    33  	return 0
    34  }
    35  
    36  func int32ToBoolean(value int32) bool {
    37  	return value != 0
    38  }
    39  

View as plain text