The Do function is now thread safe

This commit is contained in:
Sasha Koshka 2023-07-16 01:06:24 -04:00
parent 1cb3be8de8
commit 9f4e8a539a
1 changed files with 11 additions and 1 deletions

12
tomo.go
View File

@ -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) {