From 5a64f8299e4a61734af438567c70805f06e69d49 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 30 Jul 2023 22:42:24 +0900 Subject: [PATCH] internal/thread: refactoring: add a common interface Thread --- internal/graphicscommand/thread.go | 8 ++------ internal/thread/thread.go | 21 ++++++++++++++++++--- internal/ui/ui_glfw.go | 11 +++-------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/internal/graphicscommand/thread.go b/internal/graphicscommand/thread.go index d15c01aa62ec..05765b37c3a4 100644 --- a/internal/graphicscommand/thread.go +++ b/internal/graphicscommand/thread.go @@ -18,16 +18,12 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/thread" ) -var theRenderThread Thread = thread.NewNoopThread() - -type Thread interface { - Call(f func()) -} +var theRenderThread thread.Thread = thread.NewNoopThread() // SetRenderThread must be called from the rendering thread where e.g. OpenGL works. // // TODO: Create thread in this package instead of setting it externally. -func SetRenderThread(thread Thread) { +func SetRenderThread(thread thread.Thread) { theRenderThread = thread } diff --git a/internal/thread/thread.go b/internal/thread/thread.go index 9ef25b1e4026..55e28637c847 100644 --- a/internal/thread/thread.go +++ b/internal/thread/thread.go @@ -19,6 +19,13 @@ import ( "runtime" ) +type Thread interface { + Loop(ctx context.Context) error + Call(f func()) + + private() +} + // OSThread represents an OS thread. type OSThread struct { funcs chan func() @@ -66,6 +73,9 @@ func (t *OSThread) Call(f func()) { <-t.done } +func (t *OSThread) private() { +} + // NoopThread is used to disable threading. type NoopThread struct{} @@ -74,10 +84,15 @@ func NewNoopThread() *NoopThread { return &NoopThread{} } -// Loop does nothing +// Loop does nothing. func (t *NoopThread) Loop(ctx context.Context) error { return nil } -// Call executes the func immediately -func (t *NoopThread) Call(f func()) { f() } +// Call executes the func immediately. +func (t *NoopThread) Call(f func()) { + f() +} + +func (t *NoopThread) private() { +} diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 7bfa54313f65..67bd36ddc102 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -17,7 +17,6 @@ package ui import ( - stdcontext "context" "errors" "fmt" "image" @@ -34,6 +33,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk" + "github.com/hajimehoshi/ebiten/v2/internal/thread" ) func driverCursorModeToGLFWCursorMode(mode CursorMode) int { @@ -115,16 +115,11 @@ type userInterfaceImpl struct { darwinInitOnce sync.Once bufferOnceSwappedOnce sync.Once - mainThread threadInterface - renderThread threadInterface + mainThread thread.Thread + renderThread thread.Thread m sync.RWMutex } -type threadInterface interface { - Loop(ctx stdcontext.Context) error - Call(f func()) -} - const ( maxInt = int(^uint(0) >> 1) minInt = -maxInt - 1