Skip to content

Commit

Permalink
Remove nested thread.Main calls in macOS tests
Browse files Browse the repository at this point in the history
Those are not reentrant.
  • Loading branch information
sergystepanov committed Oct 20, 2023
1 parent 1d595da commit e983e79
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pkg/worker/caged/libretro/graphics/opengl.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func ReadFramebuffer(bytes, w, h uint) []byte {
return data
}

func getFbo() uint32 { return opt.fbo }
func fbo() uint32 { return opt.fbo }

func SetBuffer(size int) { buf = make([]byte, size) }

Expand Down
15 changes: 6 additions & 9 deletions pkg/worker/caged/libretro/graphics/sdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"unsafe"

"github.com/giongto35/cloud-game/v3/pkg/logger"
"github.com/giongto35/cloud-game/v3/pkg/worker/thread"
"github.com/veandco/go-sdl2/sdl"
)

Expand Down Expand Up @@ -61,7 +60,7 @@ func NewSDLContext(cfg Config, log *logger.Logger) (*SDL, error) {

var err error
// In OSX 10.14+ window creation and context creation must happen in the main thread
thread.Main(func() { display.w, display.glWCtx, err = createWindow() })
display.w, display.glWCtx, err = createWindow()
if err != nil {
return nil, fmt.Errorf("window fail: %w", err)
}
Expand All @@ -81,14 +80,13 @@ func NewSDLContext(cfg Config, log *logger.Logger) (*SDL, error) {
func (s *SDL) Deinit() error {
s.log.Debug().Msg("[SDL/OpenGL] shutdown...")
destroyFramebuffer()
var err error

// In OSX 10.14+ window deletion must happen in the main thread
thread.Main(func() {
err = s.destroyWindow()
})
err := s.destroyWindow()
if err != nil {
return fmt.Errorf("[SDL/OpenGL] deinit fail: %w", err)
}

sdl.Quit()
s.log.Debug().Msgf("[SDL/OpenGL] shutdown codes:(%v, %v)", sdl.GetError(), GetGLError())
return nil
Expand Down Expand Up @@ -134,6 +132,5 @@ func (s *SDL) setAttribute(attr sdl.GLattr, value int) {
}
}

func GetGlFbo() uint32 { return getFbo() }

func GetGlProcAddress(proc string) unsafe.Pointer { return sdl.GLGetProcAddress(proc) }
func GlFbo() uint32 { return fbo() }
func GlProcAddress(proc string) unsafe.Pointer { return sdl.GLGetProcAddress(proc) }
21 changes: 12 additions & 9 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,16 @@ func (n *Nanoarch) LoadGame(path string) error {
bufS := uint(n.sysAvInfo.geometry.max_width*n.sysAvInfo.geometry.max_height) * n.Video.PixFmt.BPP
graphics.SetBuffer(int(bufS))
n.log.Info().Msgf("Set buffer: %v", byteCountBinary(int64(bufS)))
if n.LibCo {
C.same_thread(C.init_video_cgo)
} else {
runtime.LockOSThread()
initVideo()
runtime.UnlockOSThread()
}

thread.Main(func() {
if n.LibCo {
C.same_thread(C.init_video_cgo)
} else {
runtime.LockOSThread()
initVideo()
runtime.UnlockOSThread()
}
})
}

// set default controller types on all ports
Expand Down Expand Up @@ -671,11 +674,11 @@ func coreLog(level C.enum_retro_log_level, msg *C.char) {
}

//export coreGetCurrentFramebuffer
func coreGetCurrentFramebuffer() C.uintptr_t { return (C.uintptr_t)(graphics.GetGlFbo()) }
func coreGetCurrentFramebuffer() C.uintptr_t { return (C.uintptr_t)(graphics.GlFbo()) }

//export coreGetProcAddress
func coreGetProcAddress(sym *C.char) C.retro_proc_address_t {
return (C.retro_proc_address_t)(graphics.GetGlProcAddress(C.GoString(sym)))
return (C.retro_proc_address_t)(graphics.GlProcAddress(C.GoString(sym)))
}

//export coreEnvironment
Expand Down
10 changes: 2 additions & 8 deletions pkg/worker/room/room_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"runtime"
"sync"
"testing"
"time"

"github.com/giongto35/cloud-game/v3/pkg/com"
"github.com/giongto35/cloud-game/v3/pkg/config"
Expand Down Expand Up @@ -57,11 +56,6 @@ type conf struct {
noLog bool
}

func (r testRoom) Close() {
r.Room.Close()
time.Sleep(1 * time.Second) // hack: wait room destruction (atm impossible to tell)
}

func (r testRoom) WaitFrame(n int) app.RawFrame {
var wg sync.WaitGroup
wg.Add(1)
Expand Down Expand Up @@ -131,9 +125,9 @@ func TestAll(t *testing.T) {

for _, test := range tests {
var frame app.RawFrame
room := room(conf{game: test.game, codec: encoder.VP8, autoGlContext: autoGlContext, autoAppStart: false})
flip := test.system == "gl"
thread.Main(func() { frame = room.WaitFrame(test.frames) })
room := room(conf{game: test.game, codec: encoder.VP8, autoGlContext: autoGlContext, autoAppStart: false})
frame = room.WaitFrame(test.frames)
room.Close()

if renderFrames {
Expand Down
9 changes: 2 additions & 7 deletions pkg/worker/thread/mainthread_darwin.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package thread

import (
"runtime"
"sync"
)
import "runtime"

type fun struct {
fn func()
done chan struct{}
}

var dPool = sync.Pool{New: func() any { return make(chan struct{}) }}
var fq = make(chan fun, runtime.GOMAXPROCS(0))

func init() {
Expand Down Expand Up @@ -38,8 +34,7 @@ func Run(run func()) {

// Call queues function f on the main thread and blocks until the function f finishes.
func Call(f func()) {
done := dPool.Get().(chan struct{})
defer dPool.Put(done)
done := make(chan struct{})
fq <- fun{fn: f, done: done}
<-done
}

0 comments on commit e983e79

Please sign in to comment.