shad-go/batcher/slow/value.go

37 lines
517 B
Go
Raw Normal View History

2020-02-14 23:01:42 +00:00
// +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
}