uidriver/mobile: Reduce mutex usages

This commit is contained in:
Hajime Hoshi 2020-08-29 00:24:30 +09:00
parent 91aa6a0f3a
commit b76c34336d

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"runtime/debug" "runtime/debug"
"sync" "sync"
"sync/atomic"
"unicode" "unicode"
"golang.org/x/mobile/app" "golang.org/x/mobile/app"
@ -49,7 +50,7 @@ var (
renderEndCh = make(chan struct{}) renderEndCh = make(chan struct{})
theUI = &UserInterface{ theUI = &UserInterface{
foreground: true, foreground: 1,
errCh: make(chan error), errCh: make(chan error),
// Give a default outside size so that the game can start without initializing them. // Give a default outside size so that the game can start without initializing them.
@ -122,7 +123,7 @@ type UserInterface struct {
outsideHeight float64 outsideHeight float64
sizeChanged bool sizeChanged bool
foreground bool foreground int32
errCh chan error errCh chan error
// Used for gomobile-build // Used for gomobile-build
@ -239,9 +240,11 @@ func (u *UserInterface) appMain(a app.App) {
} }
func (u *UserInterface) SetForeground(foreground bool) { func (u *UserInterface) SetForeground(foreground bool) {
u.m.Lock() var v int32
u.foreground = foreground if foreground {
u.m.Unlock() v = 1
}
atomic.StoreInt32(&u.foreground, v)
if foreground { if foreground {
hooks.ResumeAudio() hooks.ResumeAudio()
@ -363,7 +366,6 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
// //
// SetOutsideSize is concurrent safe. // SetOutsideSize is concurrent safe.
func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) { func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) {
// Called from ebitenmobileview.
u.m.Lock() u.m.Lock()
if u.outsideWidth != outsideWidth || u.outsideHeight != outsideHeight { if u.outsideWidth != outsideWidth || u.outsideHeight != outsideHeight {
u.outsideWidth = outsideWidth u.outsideWidth = outsideWidth
@ -406,10 +408,7 @@ func (u *UserInterface) SetFullscreen(fullscreen bool) {
} }
func (u *UserInterface) IsFocused() bool { func (u *UserInterface) IsFocused() bool {
u.m.Lock() return atomic.LoadInt32(&u.foreground) != 0
fg := u.foreground
u.m.Unlock()
return fg
} }
func (u *UserInterface) IsRunnableOnUnfocused() bool { func (u *UserInterface) IsRunnableOnUnfocused() bool {