From b450374ffeda1af2adf72941264ed928212649df Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Fri, 24 Jan 2025 20:34:54 +0000 Subject: [PATCH] Fixup getting the right main thread ID for mobile --- internal/async/goroutine.go | 7 +++---- internal/driver/glfw/loop.go | 1 + internal/driver/mobile/driver.go | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/async/goroutine.go b/internal/async/goroutine.go index 65e9b270b9..3c86018b81 100644 --- a/internal/async/goroutine.go +++ b/internal/async/goroutine.go @@ -10,12 +10,11 @@ import ( ) // mainGoroutineID stores the main goroutine ID. -// This ID must be initialized in main.init because -// a main goroutine may not equal to 1 due to the -// influence of a garbage collector. +// This ID must be initialized during setup by calling `SetMainGoroutine` because +// a main goroutine may not equal to 1 due to the influence of a garbage collector. var mainGoroutineID uint64 -func init() { +func SetMainGoroutine() { mainGoroutineID = goroutineID() } diff --git a/internal/driver/glfw/loop.go b/internal/driver/glfw/loop.go index 085ad947b9..4a075ad95a 100644 --- a/internal/driver/glfw/loop.go +++ b/internal/driver/glfw/loop.go @@ -28,6 +28,7 @@ var initOnce = &sync.Once{} // Arrange that main.main runs on main thread. func init() { runtime.LockOSThread() + async.SetMainGoroutine() } // force a function f to run on the main thread diff --git a/internal/driver/mobile/driver.go b/internal/driver/mobile/driver.go index a2a96b78e4..fecd0b1bba 100644 --- a/internal/driver/mobile/driver.go +++ b/internal/driver/mobile/driver.go @@ -74,6 +74,10 @@ func init() { func (d *driver) DoFromGoroutine(fn func(), wait bool) { async.EnsureNotMain(func() { + if d.queuedFuncs == nil { + fn() // before the app actually starts + return + } var done chan struct{} if wait { done = common.DonePool.Get() @@ -167,6 +171,7 @@ func (d *driver) Run() { d.running = true app.Main(func(a app.App) { + async.SetMainGoroutine() d.app = a settingsChange := make(chan fyne.Settings) d.queuedFuncs = async.NewUnboundedChan[func()]()