Skip to content

Commit

Permalink
content: add example to show main thread crash
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Jan 23, 2021
1 parent c9ff257 commit 5276776
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
20 changes: 9 additions & 11 deletions content/assets/zero-alloc-call-sched/app/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewWindow() (*Win, error) {
if err != nil {
return
}
w.win.MakeContextCurrent()
})
return w, nil
}
Expand All @@ -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)
}
32 changes: 32 additions & 0 deletions content/assets/zero-alloc-call-sched/app/window_test.go
Original file line number Diff line number Diff line change
@@ -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()
})
}
1 change: 1 addition & 0 deletions content/assets/zero-alloc-call-sched/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions content/assets/zero-alloc-call-sched/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 5276776

Please sign in to comment.