2020-08-18 09:06:36 +00:00
|
|
|
package atomicflag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-08-18 10:30:39 +00:00
|
|
|
type AtomicFlag struct {
|
2020-08-18 09:06:36 +00:00
|
|
|
value bool
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:30:39 +00:00
|
|
|
func New(value bool) *AtomicFlag {
|
|
|
|
return &AtomicFlag{value: value}
|
2020-08-18 09:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:30:39 +00:00
|
|
|
func (f *AtomicFlag) Get() bool {
|
2020-08-18 09:06:36 +00:00
|
|
|
f.mutex.RLock()
|
|
|
|
defer f.mutex.RUnlock()
|
|
|
|
return f.value
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:30:39 +00:00
|
|
|
func (f *AtomicFlag) Set(value bool) {
|
2020-08-18 09:06:36 +00:00
|
|
|
f.mutex.Lock()
|
|
|
|
defer f.mutex.Unlock()
|
|
|
|
f.value = value
|
|
|
|
}
|