mirror of https://github.com/junegunn/fzf
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
671 B
Go
33 lines
671 B
Go
10 years ago
|
package util
|
||
10 years ago
|
|
||
|
import "sync"
|
||
|
|
||
10 years ago
|
// AtomicBool is a boxed-class that provides synchronized access to the
|
||
|
// underlying boolean value
|
||
10 years ago
|
type AtomicBool struct {
|
||
|
mutex sync.Mutex
|
||
|
state bool
|
||
|
}
|
||
|
|
||
10 years ago
|
// NewAtomicBool returns a new AtomicBool
|
||
10 years ago
|
func NewAtomicBool(initialState bool) *AtomicBool {
|
||
|
return &AtomicBool{
|
||
|
mutex: sync.Mutex{},
|
||
|
state: initialState}
|
||
|
}
|
||
|
|
||
10 years ago
|
// Get returns the current boolean value synchronously
|
||
10 years ago
|
func (a *AtomicBool) Get() bool {
|
||
|
a.mutex.Lock()
|
||
|
defer a.mutex.Unlock()
|
||
|
return a.state
|
||
|
}
|
||
|
|
||
10 years ago
|
// Set updates the boolean value synchronously
|
||
10 years ago
|
func (a *AtomicBool) Set(newState bool) bool {
|
||
|
a.mutex.Lock()
|
||
|
defer a.mutex.Unlock()
|
||
|
a.state = newState
|
||
|
return a.state
|
||
|
}
|