diff --git a/content/assets/zero-alloc-call-sched/app/window.go b/content/assets/zero-alloc-call-sched/app/window.go index c2da39d..47d4ebf 100644 --- a/content/assets/zero-alloc-call-sched/app/window.go +++ b/content/assets/zero-alloc-call-sched/app/window.go @@ -32,6 +32,7 @@ func NewWindow() (*Win, error) { if err != nil { return } + w.win.MakeContextCurrent() }) return w, nil } @@ -44,24 +45,21 @@ func Terminate() { } // Closed asserts if the given window is closed. +// This function can be called from any thread. func (w *Win) Closed() (stop bool) { - return mainthread.CallV(func() interface{} { - return w.win.ShouldClose() - }).(bool) + return w.win.ShouldClose() } // Update updates the frame buffer of the given window. +// This function can be called from any thread. func (w *Win) Update() { - mainthread.Call(func() { - w.win.SwapBuffers() - // glfw.WaitEventsTimeout(1.0 / 30) - glfw.PollEvents() - }) + w.win.SwapBuffers() + // glfw.WaitEventsTimeout(1.0 / 30) + glfw.PollEvents() } // Stop stops the given window. +// This function can be called from any thread. func (w *Win) Stop() { - mainthread.Call(func() { - w.win.SetShouldClose(true) - }) + w.win.SetShouldClose(true) } diff --git a/content/assets/zero-alloc-call-sched/app/window_test.go b/content/assets/zero-alloc-call-sched/app/window_test.go new file mode 100644 index 0000000..a07cd65 --- /dev/null +++ b/content/assets/zero-alloc-call-sched/app/window_test.go @@ -0,0 +1,32 @@ +package app + +import ( + "testing" + "x/mainthread" + "x/thread" + + "github.com/go-gl/gl/v3.3-core/gl" +) + +// This test will crash! +func TestApp(t *testing.T) { + mainthread.Init(func() { + w, _ := NewWindow() + defer Terminate() + + renderThread := thread.New() + renderThread.Call(func() { + // Initialize gl from a different thread + gl.Init() + // gl.MakeContextCurrent() + gl.GoStr(gl.GetString(gl.VERSION)) + }) + mainthread.Call(func() { + // Initialize gl from the main thread can + // prevent crash from the GetVersion crash. + // gl.Init() + gl.GoStr(gl.GetString(gl.VERSION)) + }) + w.Stop() + }) +} diff --git a/content/assets/zero-alloc-call-sched/go.mod b/content/assets/zero-alloc-call-sched/go.mod index 95858f5..794fc0e 100644 --- a/content/assets/zero-alloc-call-sched/go.mod +++ b/content/assets/zero-alloc-call-sched/go.mod @@ -3,6 +3,7 @@ module x go 1.16 require ( + github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265 golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 ) diff --git a/content/assets/zero-alloc-call-sched/go.sum b/content/assets/zero-alloc-call-sched/go.sum index 177a0fd..1b7e9ac 100644 --- a/content/assets/zero-alloc-call-sched/go.sum +++ b/content/assets/zero-alloc-call-sched/go.sum @@ -1,3 +1,5 @@ +github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= +github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265 h1:BcbKYUZo/TKPsiSh7LymK3p+TNAJJW3OfGO/21sBbiA= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=