internal/thread: refactoring: add a common interface Thread

This commit is contained in:
Hajime Hoshi 2023-07-30 22:42:24 +09:00
parent 81b7fd7641
commit 5a64f8299e
3 changed files with 23 additions and 17 deletions

View File

@ -18,16 +18,12 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/thread" "github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
var theRenderThread Thread = thread.NewNoopThread() var theRenderThread thread.Thread = thread.NewNoopThread()
type Thread interface {
Call(f func())
}
// SetRenderThread must be called from the rendering thread where e.g. OpenGL works. // SetRenderThread must be called from the rendering thread where e.g. OpenGL works.
// //
// TODO: Create thread in this package instead of setting it externally. // TODO: Create thread in this package instead of setting it externally.
func SetRenderThread(thread Thread) { func SetRenderThread(thread thread.Thread) {
theRenderThread = thread theRenderThread = thread
} }

View File

@ -19,6 +19,13 @@ import (
"runtime" "runtime"
) )
type Thread interface {
Loop(ctx context.Context) error
Call(f func())
private()
}
// OSThread represents an OS thread. // OSThread represents an OS thread.
type OSThread struct { type OSThread struct {
funcs chan func() funcs chan func()
@ -66,6 +73,9 @@ func (t *OSThread) Call(f func()) {
<-t.done <-t.done
} }
func (t *OSThread) private() {
}
// NoopThread is used to disable threading. // NoopThread is used to disable threading.
type NoopThread struct{} type NoopThread struct{}
@ -74,10 +84,15 @@ func NewNoopThread() *NoopThread {
return &NoopThread{} return &NoopThread{}
} }
// Loop does nothing // Loop does nothing.
func (t *NoopThread) Loop(ctx context.Context) error { func (t *NoopThread) Loop(ctx context.Context) error {
return nil return nil
} }
// Call executes the func immediately // Call executes the func immediately.
func (t *NoopThread) Call(f func()) { f() } func (t *NoopThread) Call(f func()) {
f()
}
func (t *NoopThread) private() {
}

View File

@ -17,7 +17,6 @@
package ui package ui
import ( import (
stdcontext "context"
"errors" "errors"
"fmt" "fmt"
"image" "image"
@ -34,6 +33,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hooks"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk" "github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
func driverCursorModeToGLFWCursorMode(mode CursorMode) int { func driverCursorModeToGLFWCursorMode(mode CursorMode) int {
@ -115,16 +115,11 @@ type userInterfaceImpl struct {
darwinInitOnce sync.Once darwinInitOnce sync.Once
bufferOnceSwappedOnce sync.Once bufferOnceSwappedOnce sync.Once
mainThread threadInterface mainThread thread.Thread
renderThread threadInterface renderThread thread.Thread
m sync.RWMutex m sync.RWMutex
} }
type threadInterface interface {
Loop(ctx stdcontext.Context) error
Call(f func())
}
const ( const (
maxInt = int(^uint(0) >> 1) maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1 minInt = -maxInt - 1