Browse Source

fix race condition in repeat_timer

pull/1842/head
Ethan Buchman 9 years ago
parent
commit
3dabf304a1
1 changed files with 7 additions and 2 deletions
  1. +7
    -2
      repeat_timer.go

+ 7
- 2
repeat_timer.go View File

@ -15,6 +15,7 @@ type RepeatTimer struct {
name string
ticker *time.Ticker
quit chan struct{}
done chan struct{}
dur time.Duration
}
@ -23,6 +24,7 @@ func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
Ch: make(chan time.Time),
ticker: time.NewTicker(dur),
quit: make(chan struct{}),
done: make(chan struct{}),
name: name,
dur: dur,
}
@ -36,6 +38,8 @@ func (t *RepeatTimer) fireRoutine(ticker *time.Ticker) {
case t_ := <-ticker.C:
t.Ch <- t_
case <-t.quit:
// needed so we know when we can reset t.quit
t.done <- struct{}{}
return
}
}
@ -64,9 +68,10 @@ func (t *RepeatTimer) Stop() bool {
exists := t.ticker != nil
if exists {
t.ticker.Stop()
t.ticker = nil
t.ticker.Stop() // does not close the channel
close(t.quit)
<-t.done
t.ticker = nil
}
return exists
}

Loading…
Cancel
Save