Fix deadlock in Stop

This commit is contained in:
2024-08-16 17:40:39 -04:00
parent 91a8ae2fa5
commit da1b316d28

22
tomo.go
View File

@@ -1,15 +1,37 @@
package tomo
import "sync"
import "image"
import "git.tebibyte.media/tomo/tomo/canvas"
// TODO this really sucks. It might be a good idea to have Do be the entry point
// for every off-thread call, and Stop should just call backend.Stop within
// backend.Do. This is because Do is a queue and is not vulnerable to recursive
// locking.
var stopping bool
var stoppingLock sync.Mutex
func isStopping () bool {
stoppingLock.Lock()
defer stoppingLock.Unlock()
return stopping
}
func setStopping (is bool) {
stoppingLock.Lock()
defer stoppingLock.Unlock()
stopping = is
}
// Stop stops the currently running backend.
func Stop () {
if isStopping() { return }
setStopping(true)
backendLock.Lock()
defer backendLock.Unlock()
if backend == nil { return }
backend.Stop()
backend = nil
setStopping(false)
}
// Do performs a callback function in the event loop thread as soon as possible.