From fcd4453e4fab81a18427fc584ed9a4952ee722b1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 14 Feb 2022 01:49:39 +0900 Subject: [PATCH] ebiten: remove RunOnMainThread Unfortunately, there are several issues in RunOnMainThread: * RunOnMainThread cannot be portable: It is impossible to implement this correctly on mobiles. * RunOnMainThread doesn't make sense on mobiles: the rendering works on a different thread (goroutine) on mobiles. * RunOnMainThread can cause deadlocks very easily. Until we find a better solution, let's remove this. Closes #1945 --- internal/graphicscommand/command.go | 8 ++++---- internal/graphicscommand/thread.go | 4 ++-- run.go | 6 ------ 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index f1c9f5c73..889893d23 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -225,7 +225,7 @@ func (q *commandQueue) Enqueue(command command) { // Flush flushes the command queue. func (q *commandQueue) Flush() (err error) { - RunOnRenderingThread(func() { + runOnRenderingThread(func() { err = q.flush() }) return @@ -701,7 +701,7 @@ func (c *newShaderCommand) Exec(indexOffset int) error { // InitializeGraphicsDriverState initialize the current graphics driver state. func InitializeGraphicsDriverState() (err error) { - RunOnRenderingThread(func() { + runOnRenderingThread(func() { err = theGraphicsDriver.Initialize() }) return @@ -711,7 +711,7 @@ func InitializeGraphicsDriverState() (err error) { // If the graphics driver doesn't have an API to reset, ResetGraphicsDriverState does nothing. func ResetGraphicsDriverState() (err error) { if r, ok := theGraphicsDriver.(interface{ Reset() error }); ok { - RunOnRenderingThread(func() { + runOnRenderingThread(func() { err = r.Reset() }) } @@ -721,7 +721,7 @@ func ResetGraphicsDriverState() (err error) { // MaxImageSize returns the maximum size of an image. func MaxImageSize() int { var size int - RunOnRenderingThread(func() { + runOnRenderingThread(func() { size = theGraphicsDriver.MaxImageSize() }) return size diff --git a/internal/graphicscommand/thread.go b/internal/graphicscommand/thread.go index 408e510a9..dcb7d9d61 100644 --- a/internal/graphicscommand/thread.go +++ b/internal/graphicscommand/thread.go @@ -25,8 +25,8 @@ func SetRenderingThread(thread Thread) { theThread = thread } -// RunOnRenderingThread calls f on the rendering thread, and returns an error if any. -func RunOnRenderingThread(f func()) { +// runOnRenderingThread calls f on the rendering thread, and returns an error if any. +func runOnRenderingThread(f func()) { // The thread is nil when 1) GOOS=js or 2) using golang.org/x/mobile/gl. // When golang.org/x/mobile/gl is used, all the GL functions are called via Context, which already runs on an // appropriate thread. diff --git a/run.go b/run.go index f4e024e28..3246cdcb1 100644 --- a/run.go +++ b/run.go @@ -18,7 +18,6 @@ import ( "sync/atomic" "github.com/hajimehoshi/ebiten/v2/internal/clock" - "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/ui" ) @@ -162,11 +161,6 @@ func RunGame(game Game) error { return nil } -// RunOnMainThread calls the given f on the main thread, and blocks until f returns. -func RunOnMainThread(f func()) { - graphicscommand.RunOnRenderingThread(f) -} - func isRunGameEnded() bool { return atomic.LoadInt32(&isRunGameEnded_) != 0 }