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
u.t.Call(func() {
u.mainThread.Call(func() {
name = glfw.GetKeyName(gk, 0)
})
return name

View File

@ -25,17 +25,17 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
u.context = newContext(game)
// Initialize the main thread first so the thread is available at u.run (#809).
u.t = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t)
u.mainThread = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.mainThread)
ch := make(chan error, 1)
go func() {
defer u.t.Stop()
defer u.mainThread.Stop()
defer close(ch)
var err error
if u.t.Call(func() {
if u.mainThread.Call(func() {
err = u.init(options)
}); err != nil {
ch <- err
@ -49,30 +49,30 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
}()
u.setRunning(true)
u.t.Loop()
u.mainThread.Loop()
u.setRunning(false)
return <-ch
}
// 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.
// 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.
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.
t := u.t
// As this function is called from the main thread, u.mainThread should never be accessed and can be updated here.
t := u.mainThread
defer func() {
u.t = t
u.mainThread = t
graphicscommand.SetRenderingThread(t)
}()
u.t = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.t)
u.mainThread = thread.NewOSThread()
graphicscommand.SetRenderingThread(u.mainThread)
go func() {
defer u.t.Stop()
defer u.mainThread.Stop()
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)
// Initialize the main thread first so the thread is available at u.run (#809).
u.t = thread.NewNoopThread()
graphicscommand.SetRenderingThread(u.t)
u.mainThread = thread.NewNoopThread()
graphicscommand.SetRenderingThread(u.mainThread)
u.setRunning(true)

View File

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

View File

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