ui: Add read-write lock

This commit is contained in:
Hajime Hoshi 2016-03-26 17:24:40 +09:00
parent 4a60343fe6
commit 75ef9a6d47

View File

@ -86,22 +86,32 @@ func Init() *opengl.Context {
} }
func (u *UserInterface) SetScreenSize(width, height int) bool { func (u *UserInterface) SetScreenSize(width, height int) bool {
u.m.Lock()
defer u.m.Unlock()
return u.setScreenSize(width, height, u.scale) return u.setScreenSize(width, height, u.scale)
} }
func (u *UserInterface) SetScreenScale(scale int) bool { func (u *UserInterface) SetScreenScale(scale int) bool {
u.m.Lock()
defer u.m.Unlock()
return u.setScreenSize(u.width, u.height, scale) return u.setScreenSize(u.width, u.height, scale)
} }
func (u *UserInterface) ScreenScale() int { func (u *UserInterface) ScreenScale() int {
u.m.RLock()
defer u.m.RUnlock()
return u.scale return u.scale
} }
func (u *UserInterface) ActualScreenScale() int { func (u *UserInterface) ActualScreenScale() int {
u.m.RLock()
defer u.m.RUnlock()
return u.actualScreenScale() return u.actualScreenScale()
} }
func (u *UserInterface) Start(width, height, scale int, title string) error { func (u *UserInterface) Start(width, height, scale int, title string) error {
u.m.Lock()
defer u.m.Unlock()
m := glfw.GetPrimaryMonitor() m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode() v := m.GetVideoMode()
mw, _ := m.GetPhysicalSize() mw, _ := m.GetPhysicalSize()
@ -143,6 +153,8 @@ func (u *UserInterface) pollEvents() error {
} }
func (u *UserInterface) DoEvents() error { func (u *UserInterface) DoEvents() error {
u.m.Lock()
defer u.m.Unlock()
if err := u.pollEvents(); err != nil { if err := u.pollEvents(); err != nil {
return err return err
} }
@ -152,7 +164,7 @@ func (u *UserInterface) DoEvents() error {
if err := u.pollEvents(); err != nil { if err := u.pollEvents(); err != nil {
return err return err
} }
if u.IsClosed() { if u.window.ShouldClose() {
return nil return nil
} }
} }
@ -160,14 +172,24 @@ func (u *UserInterface) DoEvents() error {
} }
func (u *UserInterface) Terminate() { func (u *UserInterface) Terminate() {
u.m.Lock()
defer u.m.Unlock()
glfw.Terminate() glfw.Terminate()
} }
func (u *UserInterface) IsClosed() bool { func (u *UserInterface) IsClosed() bool {
u.m.RLock()
defer u.m.RUnlock()
return u.window.ShouldClose() return u.window.ShouldClose()
} }
func (u *UserInterface) SwapBuffers() { func (u *UserInterface) SwapBuffers() {
u.m.Lock()
defer u.m.Unlock()
u.swapBuffers()
}
func (u *UserInterface) swapBuffers() {
// The bound framebuffer must be the default one (0) before swapping buffers. // The bound framebuffer must be the default one (0) before swapping buffers.
u.context.BindZeroFramebuffer() u.context.BindZeroFramebuffer()
// Call glFinish before glfwSwapBuffer to make sure // Call glFinish before glfwSwapBuffer to make sure
@ -200,7 +222,7 @@ func (u *UserInterface) setScreenSize(width, height, scale int) bool {
// To make sure the current existing framebuffers are rendered, // To make sure the current existing framebuffers are rendered,
// swap buffers here before SetSize is called. // swap buffers here before SetSize is called.
u.SwapBuffers() u.swapBuffers()
ch := make(chan struct{}) ch := make(chan struct{})
window := u.window window := u.window