diff --git a/tomo.go b/tomo.go index db7ae72..e3bd279 100644 --- a/tomo.go +++ b/tomo.go @@ -1,8 +1,10 @@ package tomo +import "sync" import "image" import "errors" +var backendLock sync.Mutex var backend Backend // Run initializes a backend, runs the specified callback function, and runs the @@ -17,7 +19,10 @@ func Run (callback func ()) error { back, err := Initialize() if err != nil { return err } + + backendLock.Lock() backend = back + backendLock.Unlock() callback() return backend.Run() @@ -32,11 +37,16 @@ func assertBackend () { func Stop () { assertBackend() backend.Stop() + + backendLock.Lock() backend = nil + backendLock.Unlock() } func Do (callback func ()) { - backend.Do(callback) + backendLock.Lock() + if backend != nil { backend.Do(callback) } + backendLock.Unlock() } func NewWindow (bounds image.Rectangle) (MainWindow, error) {