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"
)
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
}

View File

@ -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() {
}

View File

@ -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