mirror of https://github.com/Hilbis/Hilbish
perf: use waitgroup instead of loop to wait on timers
parent
a4b358fd9c
commit
d808534da6
6
main.go
6
main.go
|
@ -301,11 +301,7 @@ func exit(code int) {
|
|||
// wait for all timers to finish before exiting.
|
||||
// only do that when not interactive
|
||||
if !interactive {
|
||||
for {
|
||||
if timers.running == 0 {
|
||||
os.Exit(code)
|
||||
}
|
||||
}
|
||||
timers.wait()
|
||||
}
|
||||
|
||||
os.Exit(code)
|
||||
|
|
2
timer.go
2
timer.go
|
@ -35,6 +35,7 @@ func (t *timer) start() error {
|
|||
|
||||
t.running = true
|
||||
t.th.running++
|
||||
t.th.wg.Add(1)
|
||||
t.ticker = time.NewTicker(t.dur)
|
||||
|
||||
go func() {
|
||||
|
@ -68,6 +69,7 @@ func (t *timer) stop() error {
|
|||
t.channel <- struct{}{}
|
||||
t.running = false
|
||||
t.th.running--
|
||||
t.th.wg.Done()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
var timers *timerHandler
|
||||
type timerHandler struct {
|
||||
mu *sync.RWMutex
|
||||
wg *sync.WaitGroup
|
||||
timers map[int]*timer
|
||||
latestID int
|
||||
running int
|
||||
|
@ -22,9 +23,14 @@ func newTimerHandler() *timerHandler {
|
|||
timers: make(map[int]*timer),
|
||||
latestID: 0,
|
||||
mu: &sync.RWMutex{},
|
||||
wg: &sync.WaitGroup{},
|
||||
}
|
||||
}
|
||||
|
||||
func (th *timerHandler) wait() {
|
||||
th.wg.Wait()
|
||||
}
|
||||
|
||||
func (th *timerHandler) create(typ timerType, dur time.Duration, fun *rt.Closure) *timer {
|
||||
th.mu.Lock()
|
||||
defer th.mu.Unlock()
|
||||
|
|
Loading…
Reference in New Issue