From bb799da51f80f029011b9b63d5c43725e88a8363 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 29 Apr 2024 21:50:08 +0900 Subject: [PATCH] internal/ui: use atomic.Int32 instead of atomic.Store/LoadInt32 Updates #2422 --- internal/ui/ui.go | 11 +++++------ internal/ui/ui_mobile.go | 12 ++++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/internal/ui/ui.go b/internal/ui/ui.go index ae42c2e0e..82c2ff546 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -76,7 +76,7 @@ type UserInterface struct { errM sync.Mutex isScreenClearedEveryFrame atomic.Bool - graphicsLibrary int32 + graphicsLibrary atomic.Int32 running atomic.Bool terminated atomic.Bool @@ -106,10 +106,9 @@ func Get() *UserInterface { // newUserInterface must be called from the main thread. func newUserInterface() (*UserInterface, error) { - u := &UserInterface{ - graphicsLibrary: int32(GraphicsLibraryUnknown), - } + u := &UserInterface{} u.isScreenClearedEveryFrame.Store(true) + u.graphicsLibrary.Store(int32(GraphicsLibraryUnknown)) u.whiteImage = u.NewImage(3, 3, atlas.ImageTypeRegular) pix := make([]byte, 4*u.whiteImage.width*u.whiteImage.height) @@ -204,11 +203,11 @@ func (u *UserInterface) SetScreenClearedEveryFrame(cleared bool) { } func (u *UserInterface) setGraphicsLibrary(library GraphicsLibrary) { - atomic.StoreInt32(&u.graphicsLibrary, int32(library)) + u.graphicsLibrary.Store(int32(library)) } func (u *UserInterface) GraphicsLibrary() GraphicsLibrary { - return GraphicsLibrary(atomic.LoadInt32(&u.graphicsLibrary)) + return GraphicsLibrary(u.graphicsLibrary.Load()) } func (u *UserInterface) isRunning() bool { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 613471611..b86e9a755 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -97,7 +97,7 @@ type userInterfaceImpl struct { inputState InputState touches []TouchForInput - fpsMode int32 + fpsMode atomic.Int32 renderRequester RenderRequester m sync.RWMutex @@ -228,11 +228,11 @@ func (u *UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) { } func (u *UserInterface) FPSMode() FPSModeType { - return FPSModeType(atomic.LoadInt32(&u.fpsMode)) + return FPSModeType(u.fpsMode.Load()) } func (u *UserInterface) SetFPSMode(mode FPSModeType) { - atomic.StoreInt32(&u.fpsMode, int32(mode)) + u.fpsMode.Store(int32(mode)) u.updateExplicitRenderingModeIfNeeded(mode) } @@ -294,7 +294,7 @@ func (u *UserInterface) Monitor() *Monitor { func (u *UserInterface) UpdateInput(keys map[Key]struct{}, runes []rune, touches []TouchForInput) { u.updateInputStateFromOutside(keys, runes, touches) - if FPSModeType(atomic.LoadInt32(&u.fpsMode)) == FPSModeVsyncOffMinimum { + if FPSModeType(u.fpsMode.Load()) == FPSModeVsyncOffMinimum { u.renderRequester.RequestRenderIfNeeded() } } @@ -306,11 +306,11 @@ type RenderRequester interface { func (u *UserInterface) SetRenderRequester(renderRequester RenderRequester) { u.renderRequester = renderRequester - u.updateExplicitRenderingModeIfNeeded(FPSModeType(atomic.LoadInt32(&u.fpsMode))) + u.updateExplicitRenderingModeIfNeeded(FPSModeType(u.fpsMode.Load())) } func (u *UserInterface) ScheduleFrame() { - if u.renderRequester != nil && FPSModeType(atomic.LoadInt32(&u.fpsMode)) == FPSModeVsyncOffMinimum { + if u.renderRequester != nil && FPSModeType(u.fpsMode.Load()) == FPSModeVsyncOffMinimum { u.renderRequester.RequestRenderIfNeeded() } }