github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/esync/sync_atomic_bool.go (about)

     1  package esync
     2  
     3  import (
     4  	"fmt"
     5  	"sync/atomic"
     6  )
     7  
     8  // AtomicBool support atomic operation
     9  type AtomicBool struct {
    10  	value int32
    11  }
    12  
    13  func (t *AtomicBool) Set(v bool) {
    14  	if v {
    15  		atomic.StoreInt32(&t.value, 1)
    16  	} else {
    17  		atomic.StoreInt32(&t.value, 0)
    18  	}
    19  }
    20  
    21  func (t *AtomicBool) Get() bool {
    22  	return atomic.LoadInt32(&t.value) != 0
    23  }
    24  
    25  func (t *AtomicBool) Swap(v bool) bool {
    26  	var _v int32
    27  	if v {
    28  		_v = 1
    29  	} else {
    30  		_v = 0
    31  	}
    32  	return atomic.SwapInt32(&t.value, _v) != 0
    33  }
    34  
    35  func (t *AtomicBool) String() string {
    36  	return fmt.Sprint(t.Get())
    37  }