From 42cf9d17507c76c314bd3efb2e7c3c6fd87e48e6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 8 Jun 2019 02:00:40 +0900 Subject: [PATCH] mobile: Lock the OS thread before using mutex As switching the OS thread by mutex could theoretically happen (I think this is almost 0% in Ebiten's case), locking the OS thread should happen before the mutex. --- mobile/impl_mobile.go | 4 ---- mobile/mobile.go | 5 ++++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mobile/impl_mobile.go b/mobile/impl_mobile.go index 8cfb7cd3c..e4edf2458 100644 --- a/mobile/impl_mobile.go +++ b/mobile/impl_mobile.go @@ -18,7 +18,6 @@ package mobile import ( "errors" - "runtime" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/internal/uidriver/mobile" @@ -30,9 +29,6 @@ var ( ) func update() error { - runtime.LockOSThread() - defer runtime.UnlockOSThread() - if chError == nil { return errors.New("mobile: chError must not be nil: Start is not called yet?") } diff --git a/mobile/mobile.go b/mobile/mobile.go index 8d25d9a0c..a968ae969 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -21,6 +21,7 @@ package mobile import ( + "runtime" "sync" "github.com/hajimehoshi/ebiten" @@ -63,7 +64,9 @@ func Start(f func(*ebiten.Image) error, width, height int, scale float64, title // // Update returns error when 1) OpenGL error happens, or 2) f in Start returns error samely as ebiten.Run. func Update() error { - // TODO: Is it safe to call Lock before locking the OS thread? + runtime.LockOSThread() + defer runtime.UnlockOSThread() + mobileMutex.Lock() defer mobileMutex.Unlock()