From 45963441c1bf02a52fd1c720cfe560efe1cad2f4 Mon Sep 17 00:00:00 2001 From: inkeliz Date: Mon, 10 Jun 2024 20:49:02 +0100 Subject: [PATCH] app: [macOS] run main.main off the main thread when Gio is embedded When Gio is embedded (such as on Android and iOS), we pretend that the Go library is the main program by running Go main on the main thread. To avoid deadlock, `app.Main` returns immediately to relinquish control of the main thread. This behaviour is suprising (what if something else runs after `app.Main`?) and more importantly is not compatible with app global events received by the main goroutine. Something had to give, and this change starts a new goroutine for calling Go's main. Signed-off-by: inkeliz Signed-off-by: Elias Naur --- app/os_android.go | 1 + app/os_ios.go | 8 +++++--- app/runmain.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/os_android.go b/app/os_android.go index 5b212134..0762074a 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -1318,6 +1318,7 @@ func findClass(env *C.JNIEnv, name string) C.jclass { } func osMain() { + select {} } func newWindow(window *callbacks, options []Option) { diff --git a/app/os_ios.go b/app/os_ios.go index 2a281cb8..db20e5e4 100644 --- a/app/os_ios.go +++ b/app/os_ios.go @@ -405,11 +405,12 @@ const ( ) func osMain() { - if !isMainThread() { - panic("app.Main must be run on the main goroutine") - } switch mainMode { case mainModeUndefined: + if !isMainThread() { + panic("app.Main must be run on the main goroutine") + } + mainMode = mainModeExe var argv []*C.char for _, arg := range os.Args { @@ -423,6 +424,7 @@ func osMain() { case mainModeLibrary: // Do nothing, we're embedded as a library. } + select {} } //export gio_runMain diff --git a/app/runmain.go b/app/runmain.go index a1c1e3d4..d01d85a6 100644 --- a/app/runmain.go +++ b/app/runmain.go @@ -25,6 +25,6 @@ func runMain() { // Indirect call, since the linker does not know the address of main when // laying down this package. fn := mainMain - fn() + go fn() }) }