thread: Bug fix: Queue funcs instead of panic

Now the thread object is created at (*UserInterface).Run, we don't
have to care whether the (main) thread is started or not when
Call is called. Admit queueing the functions.

Fixes #884
This commit is contained in:
Hajime Hoshi 2019-06-21 11:09:48 +09:00
parent 3eee4754c5
commit 8dc2301e54
2 changed files with 4 additions and 18 deletions

View File

@ -16,13 +16,11 @@ package thread
import (
"context"
"sync/atomic"
)
// Thread represents an OS thread.
type Thread struct {
started int32
funcs chan func()
funcs chan func()
}
// New creates a new thread.
@ -38,9 +36,6 @@ func New() *Thread {
//
// Loop must be called on the thread.
func (t *Thread) Loop(context context.Context) {
atomic.StoreInt32(&t.started, 1)
defer atomic.StoreInt32(&t.started, 0)
loop:
for {
select {
@ -56,10 +51,6 @@ loop:
//
// Do not call this from the same thread. This would block forever.
func (t *Thread) Call(f func() error) error {
if atomic.LoadInt32(&t.started) == 0 {
panic("thread: the thread loop is not started yet")
}
ch := make(chan struct{})
var err error
t.funcs <- func() {

View File

@ -172,13 +172,6 @@ func getCachedMonitor(wx, wy int) (*cachedMonitor, bool) {
return nil, false
}
func (u *UserInterface) mainThreadLoop(context context.Context) error {
u.setRunning(true)
u.t.Loop(context)
u.setRunning(false)
return nil
}
func (u *UserInterface) isRunning() bool {
u.m.Lock()
v := u.running
@ -575,7 +568,9 @@ func (u *UserInterface) Run(width, height int, scale float64, title string, uico
}
}()
u.mainThreadLoop(ctx)
u.setRunning(true)
u.t.Loop(ctx)
u.setRunning(false)
return <-ch
}