uidriver/mobile: Use timer.NewTimer instead of time.After

timer.NewTimer is much better than time.After in terms of memory
usage. See also https://golang.org/pkg/time/#After
This commit is contained in:
Hajime Hoshi 2019-06-01 01:27:04 +09:00
parent ca907e2846
commit 13ce1c28b4
2 changed files with 12 additions and 3 deletions

View File

@ -232,13 +232,15 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
u8.Release() u8.Release()
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
t := time.NewTimer(timeout)
defer t.Stop()
select { select {
case err := <-ch: case err := <-ch:
if err != nil { if err != nil {
return nil, err return nil, err
} }
case <-time.After(timeout): case <-t.C:
// Sometimes decode fails without calling the callbacks (#464). // Sometimes decode fails without calling the callbacks (#464).
// Let's just try again in this case. // Let's just try again in this case.
return nil, errTimeout return nil, errTimeout

View File

@ -63,12 +63,16 @@ func (u *UserInterface) Render(chError <-chan error) error {
return errors.New("ui: chError must not be nil") return errors.New("ui: chError must not be nil")
} }
// TODO: Check this is called on the rendering thread // TODO: Check this is called on the rendering thread
t := time.NewTimer(500 * time.Millisecond)
defer t.Stop()
select { select {
case err := <-chError: case err := <-chError:
return err return err
case renderCh <- struct{}{}: case renderCh <- struct{}{}:
return opengl.Get().DoWork(renderEndCh) return opengl.Get().DoWork(renderEndCh)
case <-time.After(500 * time.Millisecond): case <-t.C:
// This function must not be blocked. We need to break for timeout. // This function must not be blocked. We need to break for timeout.
return nil return nil
} }
@ -247,10 +251,13 @@ func (u *UserInterface) scaleImpl() float64 {
func (u *UserInterface) update(context driver.UIContext) error { func (u *UserInterface) update(context driver.UIContext) error {
render: render:
for { for {
t := time.NewTimer(500 * time.Millisecond)
defer t.Stop()
select { select {
case <-renderCh: case <-renderCh:
break render break render
case <-time.After(500 * time.Millisecond): case <-t.C:
context.SuspendAudio() context.SuspendAudio()
continue continue
} }