Skip to content
Myr

atomicbool.go

This file

Path

src/util/atomicbool.go

34 lines · 748 bytes · Go

Last changed by

Not known.

History of this file

Take it

1package util23import (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 {17	state int32 // "1" is true, "0" is false18}1920// NewAtomicBool returns a new AtomicBool21func NewAtomicBool(initialState bool) *AtomicBool {22	return &AtomicBool{state: convertBoolToInt32(initialState)}23}2425// Get returns the current boolean value synchronously26func (a *AtomicBool) Get() bool {27	return atomic.LoadInt32(&a.state) == 128}2930// Set updates the boolean value synchronously31func (a *AtomicBool) Set(newState bool) bool {32	atomic.StoreInt32(&a.state, convertBoolToInt32(newState))33	return newState34}