internal/ui: rename t -> mainThread

This commit is contained in:
Hajime Hoshi 2022-12-28 15:59:53 +09:00
parent 089ba9ee20
commit e9248c6f33
5 changed files with 49 additions and 50 deletions

View File

@ -92,7 +92,7 @@ func (u *userInterfaceImpl) keyName(key Key) string {
} }
var name string var name string
u.t.Call(func() { u.mainThread.Call(func() {
name = glfw.GetKeyName(gk, 0) name = glfw.GetKeyName(gk, 0)
}) })
return name return name

View File

@ -25,17 +25,17 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
u.context = newContext(game) u.context = newContext(game)
// Initialize the main thread first so the thread is available at u.run (#809). // Initialize the main thread first so the thread is available at u.run (#809).
u.t = thread.NewOSThread() u.mainThread = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t) graphicscommand.SetRenderingThread(u.mainThread)
ch := make(chan error, 1) ch := make(chan error, 1)
go func() { go func() {
defer u.t.Stop() defer u.mainThread.Stop()
defer close(ch) defer close(ch)
var err error var err error
if u.t.Call(func() { if u.mainThread.Call(func() {
err = u.init(options) err = u.init(options)
}); err != nil { }); err != nil {
ch <- err ch <- err
@ -49,30 +49,30 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
}() }()
u.setRunning(true) u.setRunning(true)
u.t.Loop() u.mainThread.Loop()
u.setRunning(false) u.setRunning(false)
return <-ch return <-ch
} }
// runOnAnotherThreadFromMainThread is called from the main thread, and calls f on a new goroutine (thread). // runOnAnotherThreadFromMainThread is called from the main thread, and calls f on a new goroutine (thread).
// runOnAnotherThreadFromMainThread creates a new nested main thread and runs the run loop. // runOnAnotherThreadFromMainThread creates a new nested main thread and runs the run loop.
// u.t is updated to the new thread until runOnAnotherThreadFromMainThread is called. // u.mainThread is updated to the new thread until runOnAnotherThreadFromMainThread is called.
// //
// Inside f, another functions that must be called from the main thread can be called safely. // Inside f, another functions that must be called from the main thread can be called safely.
func (u *userInterfaceImpl) runOnAnotherThreadFromMainThread(f func()) { func (u *userInterfaceImpl) runOnAnotherThreadFromMainThread(f func()) {
// As this function is called from the main thread, u.t should never be accessed and can be updated here. // As this function is called from the main thread, u.mainThread should never be accessed and can be updated here.
t := u.t t := u.mainThread
defer func() { defer func() {
u.t = t u.mainThread = t
graphicscommand.SetRenderingThread(t) graphicscommand.SetRenderingThread(t)
}() }()
u.t = thread.NewOSThread() u.mainThread = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t) graphicscommand.SetRenderingThread(u.mainThread)
go func() { go func() {
defer u.t.Stop() defer u.mainThread.Stop()
f() f()
}() }()
u.t.Loop() u.mainThread.Loop()
} }

View File

@ -25,8 +25,8 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
u.context = newContext(game) u.context = newContext(game)
// Initialize the main thread first so the thread is available at u.run (#809). // Initialize the main thread first so the thread is available at u.run (#809).
u.t = thread.NewNoopThread() u.mainThread = thread.NewNoopThread()
graphicscommand.SetRenderingThread(u.t) graphicscommand.SetRenderingThread(u.mainThread)
u.setRunning(true) u.setRunning(true)

View File

@ -109,9 +109,8 @@ type userInterfaceImpl struct {
defaultFramebufferSizeCallback glfw.FramebufferSizeCallback defaultFramebufferSizeCallback glfw.FramebufferSizeCallback
framebufferSizeCallbackCh chan struct{} framebufferSizeCallbackCh chan struct{}
// t is the main thread == the rendering thread. mainThread thread.Thread
t thread.Thread m sync.RWMutex
m sync.RWMutex
} }
const ( const (
@ -479,7 +478,7 @@ func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) {
} }
var w, h int var w, h int
u.t.Call(func() { u.mainThread.Call(func() {
m := u.currentMonitor() m := u.currentMonitor()
if m == nil { if m == nil {
return return
@ -508,7 +507,7 @@ func (u *userInterfaceImpl) IsFullscreen() bool {
return u.isInitFullscreen() return u.isInitFullscreen()
} }
b := false b := false
u.t.Call(func() { u.mainThread.Call(func() {
b = u.isFullscreen() b = u.isFullscreen()
}) })
return b return b
@ -524,7 +523,7 @@ func (u *userInterfaceImpl) SetFullscreen(fullscreen bool) {
return return
} }
u.t.Call(func() { u.mainThread.Call(func() {
if u.isFullscreen() == fullscreen { if u.isFullscreen() == fullscreen {
return return
} }
@ -538,7 +537,7 @@ func (u *userInterfaceImpl) IsFocused() bool {
} }
var focused bool var focused bool
u.t.Call(func() { u.mainThread.Call(func() {
focused = u.window.GetAttrib(glfw.Focused) == glfw.True focused = u.window.GetAttrib(glfw.Focused) == glfw.True
}) })
return focused return focused
@ -559,7 +558,7 @@ func (u *userInterfaceImpl) SetFPSMode(mode FPSModeType) {
u.m.Unlock() u.m.Unlock()
return return
} }
u.t.Call(func() { u.mainThread.Call(func() {
if !u.fpsModeInited { if !u.fpsModeInited {
u.fpsMode = mode u.fpsMode = mode
return return
@ -584,7 +583,7 @@ func (u *userInterfaceImpl) CursorMode() CursorMode {
} }
var mode int var mode int
u.t.Call(func() { u.mainThread.Call(func() {
mode = u.window.GetInputMode(glfw.CursorMode) mode = u.window.GetInputMode(glfw.CursorMode)
}) })
@ -607,7 +606,7 @@ func (u *userInterfaceImpl) SetCursorMode(mode CursorMode) {
u.setInitCursorMode(mode) u.setInitCursorMode(mode)
return return
} }
u.t.Call(func() { u.mainThread.Call(func() {
u.window.SetInputMode(glfw.CursorMode, driverCursorModeToGLFWCursorMode(mode)) u.window.SetInputMode(glfw.CursorMode, driverCursorModeToGLFWCursorMode(mode))
}) })
} }
@ -624,7 +623,7 @@ func (u *userInterfaceImpl) SetCursorShape(shape CursorShape) {
if !u.isRunning() { if !u.isRunning() {
return return
} }
u.t.Call(func() { u.mainThread.Call(func() {
u.setNativeCursor(shape) u.setNativeCursor(shape)
}) })
} }
@ -635,7 +634,7 @@ func (u *userInterfaceImpl) DeviceScaleFactor() float64 {
} }
f := 0.0 f := 0.0
u.t.Call(func() { u.mainThread.Call(func() {
f = u.deviceScaleFactor(u.currentMonitor()) f = u.deviceScaleFactor(u.currentMonitor())
}) })
return f return f
@ -731,7 +730,7 @@ func (u *userInterfaceImpl) registerWindowSetSizeCallback() {
outsideWidth, outsideHeight := u.outsideSize() outsideWidth, outsideHeight := u.outsideSize()
deviceScaleFactor := u.deviceScaleFactor(u.currentMonitor()) deviceScaleFactor := u.deviceScaleFactor(u.currentMonitor())
// In the game's update, u.t.Call might be called. // In the game's update, u.mainThread.Call might be called.
// In order to call it safely, use runOnAnotherThreadFromMainThread. // In order to call it safely, use runOnAnotherThreadFromMainThread.
var err error var err error
u.runOnAnotherThreadFromMainThread(func() { u.runOnAnotherThreadFromMainThread(func() {
@ -1067,7 +1066,7 @@ func (u *userInterfaceImpl) update() (float64, float64, error) {
} }
func (u *userInterfaceImpl) loop() error { func (u *userInterfaceImpl) loop() error {
defer u.t.Call(func() { defer u.mainThread.Call(func() {
u.window.Destroy() u.window.Destroy()
glfw.Terminate() glfw.Terminate()
}) })
@ -1091,7 +1090,7 @@ func (u *userInterfaceImpl) loop() error {
var outsideWidth, outsideHeight float64 var outsideWidth, outsideHeight float64
var deviceScaleFactor float64 var deviceScaleFactor float64
var err error var err error
if u.t.Call(func() { if u.mainThread.Call(func() {
outsideWidth, outsideHeight, err = u.update() outsideWidth, outsideHeight, err = u.update()
deviceScaleFactor = u.deviceScaleFactor(u.currentMonitor()) deviceScaleFactor = u.deviceScaleFactor(u.currentMonitor())
}); err != nil { }); err != nil {
@ -1126,7 +1125,7 @@ func (u *userInterfaceImpl) loop() error {
newImgs[i] = rgba newImgs[i] = rgba
} }
u.t.Call(func() { u.mainThread.Call(func() {
// In the fullscreen mode, reset the icon images and try again later. // In the fullscreen mode, reset the icon images and try again later.
if u.isFullscreen() { if u.isFullscreen() {
u.setIconImages(imgs) u.setIconImages(imgs)
@ -1141,7 +1140,7 @@ func (u *userInterfaceImpl) loop() error {
// However, (*thread).Call is not good for performance due to channels. // However, (*thread).Call is not good for performance due to channels.
// Let's avoid this whenever possible (#1367). // Let's avoid this whenever possible (#1367).
if u.graphicsDriver.IsGL() { if u.graphicsDriver.IsGL() {
u.t.Call(u.swapBuffers) u.mainThread.Call(u.swapBuffers)
} }
if unfocused { if unfocused {

View File

@ -32,7 +32,7 @@ func (w *glfwWindow) IsDecorated() bool {
return w.ui.isInitWindowDecorated() return w.ui.isInitWindowDecorated()
} }
v := false v := false
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True
}) })
return v return v
@ -44,7 +44,7 @@ func (w *glfwWindow) SetDecorated(decorated bool) {
return return
} }
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
w.ui.setWindowDecorated(decorated) w.ui.setWindowDecorated(decorated)
}) })
} }
@ -57,7 +57,7 @@ func (w *glfwWindow) ResizingMode() WindowResizingMode {
return mode return mode
} }
var mode WindowResizingMode var mode WindowResizingMode
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
mode = w.ui.windowResizingMode mode = w.ui.windowResizingMode
}) })
return mode return mode
@ -70,7 +70,7 @@ func (w *glfwWindow) SetResizingMode(mode WindowResizingMode) {
w.ui.m.Unlock() w.ui.m.Unlock()
return return
} }
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
w.ui.setWindowResizingMode(mode) w.ui.setWindowResizingMode(mode)
}) })
} }
@ -80,7 +80,7 @@ func (w *glfwWindow) IsFloating() bool {
return w.ui.isInitWindowFloating() return w.ui.isInitWindowFloating()
} }
var v bool var v bool
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True
}) })
return v return v
@ -91,7 +91,7 @@ func (w *glfwWindow) SetFloating(floating bool) {
w.ui.setInitWindowFloating(floating) w.ui.setInitWindowFloating(floating)
return return
} }
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
w.ui.setWindowFloating(floating) w.ui.setWindowFloating(floating)
}) })
} }
@ -104,7 +104,7 @@ func (w *glfwWindow) IsMaximized() bool {
return false return false
} }
var v bool var v bool
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
v = w.ui.isWindowMaximized() v = w.ui.isWindowMaximized()
}) })
return v return v
@ -126,7 +126,7 @@ func (w *glfwWindow) Maximize() {
w.ui.setInitWindowMaximized(true) w.ui.setInitWindowMaximized(true)
return return
} }
w.ui.t.Call(w.ui.maximizeWindow) w.ui.mainThread.Call(w.ui.maximizeWindow)
} }
func (w *glfwWindow) IsMinimized() bool { func (w *glfwWindow) IsMinimized() bool {
@ -134,7 +134,7 @@ func (w *glfwWindow) IsMinimized() bool {
return false return false
} }
var v bool var v bool
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True
}) })
return v return v
@ -145,7 +145,7 @@ func (w *glfwWindow) Minimize() {
// Do nothing // Do nothing
return return
} }
w.ui.t.Call(w.ui.iconifyWindow) w.ui.mainThread.Call(w.ui.iconifyWindow)
} }
func (w *glfwWindow) Restore() { func (w *glfwWindow) Restore() {
@ -156,7 +156,7 @@ func (w *glfwWindow) Restore() {
// Do nothing // Do nothing
return return
} }
w.ui.t.Call(w.ui.restoreWindow) w.ui.mainThread.Call(w.ui.restoreWindow)
} }
func (w *glfwWindow) Position() (int, int) { func (w *glfwWindow) Position() (int, int) {
@ -164,7 +164,7 @@ func (w *glfwWindow) Position() (int, int) {
panic("ui: WindowPosition can't be called before the main loop starts") panic("ui: WindowPosition can't be called before the main loop starts")
} }
x, y := 0, 0 x, y := 0, 0
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
var wx, wy int var wx, wy int
if w.ui.isFullscreen() { if w.ui.isFullscreen() {
wx, wy = w.ui.origWindowPos() wx, wy = w.ui.origWindowPos()
@ -187,7 +187,7 @@ func (w *glfwWindow) SetPosition(x, y int) {
w.ui.setInitWindowPositionInDIP(x, y) w.ui.setInitWindowPositionInDIP(x, y)
return return
} }
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
w.ui.setWindowPositionInDIP(x, y, w.ui.currentMonitor()) w.ui.setWindowPositionInDIP(x, y, w.ui.currentMonitor())
}) })
} }
@ -198,7 +198,7 @@ func (w *glfwWindow) Size() (int, int) {
return w.ui.adjustWindowSizeBasedOnSizeLimitsInDIP(ww, wh) return w.ui.adjustWindowSizeBasedOnSizeLimitsInDIP(ww, wh)
} }
var ww, wh int var ww, wh int
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
// Unlike origWindowPos, origWindow{Width,Height}InDPI are always updated via the callback. // Unlike origWindowPos, origWindow{Width,Height}InDPI are always updated via the callback.
ww = w.ui.origWindowWidthInDIP ww = w.ui.origWindowWidthInDIP
wh = w.ui.origWindowHeightInDIP wh = w.ui.origWindowHeightInDIP
@ -212,7 +212,7 @@ func (w *glfwWindow) SetSize(width, height int) {
w.ui.setInitWindowSizeInDIP(width, height) w.ui.setInitWindowSizeInDIP(width, height)
return return
} }
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
if w.ui.isWindowMaximized() && runtime.GOOS != "darwin" { if w.ui.isWindowMaximized() && runtime.GOOS != "darwin" {
return return
} }
@ -232,7 +232,7 @@ func (w *glfwWindow) SetSizeLimits(minw, minh, maxw, maxh int) {
return return
} }
w.ui.t.Call(w.ui.updateWindowSizeLimits) w.ui.mainThread.Call(w.ui.updateWindowSizeLimits)
} }
func (w *glfwWindow) SetIcon(iconImages []image.Image) { func (w *glfwWindow) SetIcon(iconImages []image.Image) {
@ -248,7 +248,7 @@ func (w *glfwWindow) SetTitle(title string) {
return return
} }
w.ui.title = title w.ui.title = title
w.ui.t.Call(func() { w.ui.mainThread.Call(func() {
w.ui.setWindowTitle(title) w.ui.setWindowTitle(title)
}) })
} }