Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Batcher struct {
- valueSlow *slow.Value
- value interface{}
- mu sync.Mutex
- con *sync.Cond
- done, running bool
- }
- func (b *Batcher) Load() interface{} {
- b.mu.Lock()
- defer b.mu.Unlock()
- if b.done {
- b.done = false
- b.mu.Unlock()
- b.value = b.valueSlow.Load()
- b.mu.Lock()
- b.con.Broadcast()
- return b.value
- }
- if b.running {
- b.con.Wait()
- if !b.done {
- b.con.Wait()
- return b.value
- }
- }
- b.running = true
- b.mu.Unlock()
- b.value = b.valueSlow.Load()
- b.mu.Lock()
- b.done = true
- b.con.Broadcast()
- return b.value
- }
- func NewBatcher(v *slow.Value) *Batcher {
- mu := sync.Mutex{}
- return &Batcher{valueSlow: v, mu: mu, con: sync.NewCond(&mu)}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement