1
0
Fork 0

Add atomic bool

master
Philip O'Toole 7 months ago
parent 70cfe680cc
commit 8453834bce

@ -2,6 +2,7 @@ package store
import ( import (
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -43,3 +44,28 @@ func (t *AtomicTime) Sub(tt *AtomicTime) time.Duration {
defer t.mu.RUnlock() defer t.mu.RUnlock()
return t.t.Sub(tt.t) return t.t.Sub(tt.t)
} }
// AtomicBool is a boolean with atomic operations.
type AtomicBool struct {
state int32 // 1 for true, 0 for false
}
// NewAtomicBool returns a new AtomicBool initialized to false.
func NewAtomicBool() *AtomicBool {
return &AtomicBool{state: 0}
}
// Set sets the AtomicBool to true.
func (b *AtomicBool) Set() {
atomic.StoreInt32(&b.state, 1)
}
// Unset sets the AtomicBool to false.
func (b *AtomicBool) Unset() {
atomic.StoreInt32(&b.state, 0)
}
// Is returns true if the AtomicBool is true, false otherwise.
func (b *AtomicBool) Is() bool {
return atomic.LoadInt32(&b.state) == 1
}

Loading…
Cancel
Save