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 <inkeliz@inkeliz.com>
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
inkeliz
2024-06-10 20:49:02 +01:00
committed by Elias Naur
parent be8d9df848
commit 45963441c1
3 changed files with 7 additions and 4 deletions
+1
View File
@@ -1318,6 +1318,7 @@ func findClass(env *C.JNIEnv, name string) C.jclass {
} }
func osMain() { func osMain() {
select {}
} }
func newWindow(window *callbacks, options []Option) { func newWindow(window *callbacks, options []Option) {
+5 -3
View File
@@ -405,11 +405,12 @@ const (
) )
func osMain() { func osMain() {
if !isMainThread() {
panic("app.Main must be run on the main goroutine")
}
switch mainMode { switch mainMode {
case mainModeUndefined: case mainModeUndefined:
if !isMainThread() {
panic("app.Main must be run on the main goroutine")
}
mainMode = mainModeExe mainMode = mainModeExe
var argv []*C.char var argv []*C.char
for _, arg := range os.Args { for _, arg := range os.Args {
@@ -423,6 +424,7 @@ func osMain() {
case mainModeLibrary: case mainModeLibrary:
// Do nothing, we're embedded as a library. // Do nothing, we're embedded as a library.
} }
select {}
} }
//export gio_runMain //export gio_runMain
+1 -1
View File
@@ -25,6 +25,6 @@ func runMain() {
// Indirect call, since the linker does not know the address of main when // Indirect call, since the linker does not know the address of main when
// laying down this package. // laying down this package.
fn := mainMain fn := mainMain
fn() go fn()
}) })
} }