Skip to content

Commit

Permalink
internal/thread: refactoring: add a common interface Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Jul 30, 2023
1 parent 81b7fd7 commit 5a64f82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 2 additions & 6 deletions internal/graphicscommand/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
21 changes: 18 additions & 3 deletions internal/thread/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{}

Expand All @@ -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() {
}
11 changes: 3 additions & 8 deletions internal/ui/ui_glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package ui

import (
stdcontext "context"
"errors"
"fmt"
"image"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5a64f82

Please sign in to comment.