mainthread: Cause panic when trying to run before initialization

This commit is contained in:
Hajime Hoshi 2019-02-02 19:00:15 +09:00
parent bcd9db669d
commit 05d78b4232
2 changed files with 17 additions and 1 deletions

View File

@ -16,18 +16,23 @@ package mainthread
import (
"runtime"
"sync/atomic"
)
func init() {
runtime.LockOSThread()
}
var funcs = make(chan func())
var (
started = int32(0)
funcs = make(chan func())
)
// Loop starts the main-thread loop.
//
// Loop must be called on the main thread.
func Loop(ch <-chan error) error {
atomic.StoreInt32(&started, 1)
for {
select {
case f := <-funcs:
@ -41,6 +46,10 @@ func Loop(ch <-chan error) error {
// Run calls f on the main thread.
func Run(f func() error) error {
if atomic.LoadInt32(&started) == 0 {
panic("mainthread: the mainthread loop is not started yet")
}
ch := make(chan struct{})
var err error
funcs <- func() {

View File

@ -189,6 +189,13 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
}
i.image.ReplacePixels(pixels, x, y, width, height)
// TODO: We wanted to skip copying pixels, but this can cause reading-pixels before the driver is initialized.
//
// if !IsRestoringEnabled() {
// i.makeStale()
// return
// }
if x == 0 && y == 0 && width == w && height == h {
if pixels != nil {
if i.basePixels == nil {