internal/ui: use atomic.Int32 instead of atomic.Store/LoadInt32

Updates #2422
This commit is contained in:
Hajime Hoshi 2024-04-29 21:50:08 +09:00
parent 59fb259181
commit bb799da51f
2 changed files with 11 additions and 12 deletions

View File

@ -76,7 +76,7 @@ type UserInterface struct {
errM sync.Mutex errM sync.Mutex
isScreenClearedEveryFrame atomic.Bool isScreenClearedEveryFrame atomic.Bool
graphicsLibrary int32 graphicsLibrary atomic.Int32
running atomic.Bool running atomic.Bool
terminated atomic.Bool terminated atomic.Bool
@ -106,10 +106,9 @@ func Get() *UserInterface {
// newUserInterface must be called from the main thread. // newUserInterface must be called from the main thread.
func newUserInterface() (*UserInterface, error) { func newUserInterface() (*UserInterface, error) {
u := &UserInterface{ u := &UserInterface{}
graphicsLibrary: int32(GraphicsLibraryUnknown),
}
u.isScreenClearedEveryFrame.Store(true) u.isScreenClearedEveryFrame.Store(true)
u.graphicsLibrary.Store(int32(GraphicsLibraryUnknown))
u.whiteImage = u.NewImage(3, 3, atlas.ImageTypeRegular) u.whiteImage = u.NewImage(3, 3, atlas.ImageTypeRegular)
pix := make([]byte, 4*u.whiteImage.width*u.whiteImage.height) 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) { func (u *UserInterface) setGraphicsLibrary(library GraphicsLibrary) {
atomic.StoreInt32(&u.graphicsLibrary, int32(library)) u.graphicsLibrary.Store(int32(library))
} }
func (u *UserInterface) GraphicsLibrary() GraphicsLibrary { func (u *UserInterface) GraphicsLibrary() GraphicsLibrary {
return GraphicsLibrary(atomic.LoadInt32(&u.graphicsLibrary)) return GraphicsLibrary(u.graphicsLibrary.Load())
} }
func (u *UserInterface) isRunning() bool { func (u *UserInterface) isRunning() bool {

View File

@ -97,7 +97,7 @@ type userInterfaceImpl struct {
inputState InputState inputState InputState
touches []TouchForInput touches []TouchForInput
fpsMode int32 fpsMode atomic.Int32
renderRequester RenderRequester renderRequester RenderRequester
m sync.RWMutex m sync.RWMutex
@ -228,11 +228,11 @@ func (u *UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) {
} }
func (u *UserInterface) FPSMode() FPSModeType { func (u *UserInterface) FPSMode() FPSModeType {
return FPSModeType(atomic.LoadInt32(&u.fpsMode)) return FPSModeType(u.fpsMode.Load())
} }
func (u *UserInterface) SetFPSMode(mode FPSModeType) { func (u *UserInterface) SetFPSMode(mode FPSModeType) {
atomic.StoreInt32(&u.fpsMode, int32(mode)) u.fpsMode.Store(int32(mode))
u.updateExplicitRenderingModeIfNeeded(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) { func (u *UserInterface) UpdateInput(keys map[Key]struct{}, runes []rune, touches []TouchForInput) {
u.updateInputStateFromOutside(keys, runes, touches) u.updateInputStateFromOutside(keys, runes, touches)
if FPSModeType(atomic.LoadInt32(&u.fpsMode)) == FPSModeVsyncOffMinimum { if FPSModeType(u.fpsMode.Load()) == FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded() u.renderRequester.RequestRenderIfNeeded()
} }
} }
@ -306,11 +306,11 @@ type RenderRequester interface {
func (u *UserInterface) SetRenderRequester(renderRequester RenderRequester) { func (u *UserInterface) SetRenderRequester(renderRequester RenderRequester) {
u.renderRequester = renderRequester u.renderRequester = renderRequester
u.updateExplicitRenderingModeIfNeeded(FPSModeType(atomic.LoadInt32(&u.fpsMode))) u.updateExplicitRenderingModeIfNeeded(FPSModeType(u.fpsMode.Load()))
} }
func (u *UserInterface) ScheduleFrame() { 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() u.renderRequester.RequestRenderIfNeeded()
} }
} }