diff options
Diffstat (limited to 'src/util/atomicbool.go')
| -rw-r--r-- | src/util/atomicbool.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util/atomicbool.go b/src/util/atomicbool.go new file mode 100644 index 00000000..9e1bdc8f --- /dev/null +++ b/src/util/atomicbool.go @@ -0,0 +1,32 @@ +package util + +import "sync" + +// AtomicBool is a boxed-class that provides synchronized access to the +// underlying boolean value +type AtomicBool struct { + mutex sync.Mutex + state bool +} + +// NewAtomicBool returns a new AtomicBool +func NewAtomicBool(initialState bool) *AtomicBool { + return &AtomicBool{ + mutex: sync.Mutex{}, + state: initialState} +} + +// Get returns the current boolean value synchronously +func (a *AtomicBool) Get() bool { + a.mutex.Lock() + defer a.mutex.Unlock() + return a.state +} + +// Set updates the boolean value synchronously +func (a *AtomicBool) Set(newState bool) bool { + a.mutex.Lock() + defer a.mutex.Unlock() + a.state = newState + return a.state +} |
