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()
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
t := time.NewTimer(timeout)
defer t.Stop()
select {
case err := <-ch:
if err != nil {
return nil, err
}
case <-time.After(timeout):
case <-t.C:
// Sometimes decode fails without calling the callbacks (#464).
// Let's just try again in this case.
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")
}
// TODO: Check this is called on the rendering thread
t := time.NewTimer(500 * time.Millisecond)
defer t.Stop()
select {
case err := <-chError:
return err
case renderCh <- struct{}{}:
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.
return nil
}
@ -247,10 +251,13 @@ func (u *UserInterface) scaleImpl() float64 {
func (u *UserInterface) update(context driver.UIContext) error {
render:
for {
t := time.NewTimer(500 * time.Millisecond)
defer t.Stop()
select {
case <-renderCh:
break render
case <-time.After(500 * time.Millisecond):
case <-t.C:
context.SuspendAudio()
continue
}