Skip to content
Myr

Update AtomicBool to use atomic memory operation (#1939)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alexandr · committed by GitHub · parent a5c2f28 · Browse files · 1 file+15 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−13 removed

  • other:+15 added−13 removed

1 file+15 added−13 removed

modified:src/util/atomicbool.go+15 added-13 removed
1package util23import "sync"3import (4 "sync/atomic"5)67func convertBoolToInt32(b bool) int32 {8 if b {9 return 110 }11 return 012}1314// AtomicBool is a boxed-class that provides synchronized access to the15// underlying boolean value16type AtomicBool struct {8 mutex sync.Mutex9 state bool17 state int32 // "1" is true, "0" is false18}1920// NewAtomicBool returns a new AtomicBool21func NewAtomicBool(initialState bool) *AtomicBool {14 return &AtomicBool{15 mutex: sync.Mutex{},16 state: initialState}22 return &AtomicBool{state: convertBoolToInt32(initialState)}23}2425// Get returns the current boolean value synchronously26func (a *AtomicBool) Get() bool {21 a.mutex.Lock()22 defer a.mutex.Unlock()23 return a.state27 return atomic.LoadInt32(&a.state) == 128}2930// Set updates the boolean value synchronously31func (a *AtomicBool) Set(newState bool) bool {28 a.mutex.Lock()29 defer a.mutex.Unlock()30 a.state = newState31 return a.state32 atomic.StoreInt32(&a.state, convertBoolToInt32(newState))33 return newState34}