shad-go/batcher/slow/value.go
2020-02-15 02:04:08 +03:00

36 lines
517 B
Go

// +build !change
package slow
import (
"sync"
"sync/atomic"
"time"
)
type Value struct {
mu sync.Mutex
value interface{}
readRunning int32
}
func (s *Value) Load() interface{} {
if atomic.SwapInt32(&s.readRunning, 1) == 1 {
panic("another load is running")
}
defer atomic.StoreInt32(&s.readRunning, 0)
s.mu.Lock()
value := s.value
s.mu.Unlock()
time.Sleep(time.Millisecond)
return value
}
func (s *Value) Store(v interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
s.value = v
}