mobile: Bug fix: mutex is necessary for start and update

mobile's Start and Update can be called from different threads.
Especially, on Android, this behavior is noted explicitly at
https://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer
This commit is contained in:
Hajime Hoshi 2018-05-03 22:02:43 +09:00
parent f1927d8aca
commit 7a00f0f599

View File

@ -18,17 +18,22 @@ package mobile
import (
"errors"
"sync"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/ui"
)
var (
chError <-chan error
running bool
chError <-chan error
running bool
mobileMutex sync.Mutex
)
func update() error {
mobileMutex.Lock()
defer mobileMutex.Unlock()
if chError == nil {
return errors.New("mobile: chError must not be nil: Start is not called yet?")
}
@ -39,6 +44,9 @@ func update() error {
}
func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) {
mobileMutex.Lock()
defer mobileMutex.Unlock()
running = true
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title)
}