github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/autoprofile/internal/flag.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package internal
     5  
     6  import "sync/atomic"
     7  
     8  // Flag is a boolean value that can be set and unset atomically
     9  type Flag struct {
    10  	value int32
    11  }
    12  
    13  // SetIfUnset sets the Flag to true if it's false and returns whether
    14  // the value has been changed
    15  func (f *Flag) SetIfUnset() bool {
    16  	return atomic.CompareAndSwapInt32(&f.value, 0, 1)
    17  }
    18  
    19  // UnsetIfSet sets the Flag to false if it's true and returns whether
    20  // the value has been changed
    21  func (f *Flag) UnsetIfSet() bool {
    22  	return atomic.CompareAndSwapInt32(&f.value, 1, 0)
    23  }
    24  
    25  // Set sets the Flag to true
    26  func (f *Flag) Set() {
    27  	atomic.StoreInt32(&f.value, 1)
    28  }
    29  
    30  // Unset sets the Flag to false
    31  func (f *Flag) Unset() {
    32  	atomic.StoreInt32(&f.value, 0)
    33  }
    34  
    35  // IsSet returns whether the Flag is set to true
    36  func (f *Flag) IsSet() bool {
    37  	return atomic.LoadInt32(&f.value) == 1
    38  }