app/internal/app: introduce runOnMain for main thread callbacks

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-17 11:58:42 +02:00
parent a3dc2d6cdc
commit 65d79f295f
3 changed files with 45 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: Unlicense OR MIT
package window
/*
#include "os_darwin.h"
*/
import "C"
var mainFuncs = make(chan func(), 1)
// runOnMain runs the function on the main thread.
func runOnMain(f func()) {
go func() {
mainFuncs <- f
C.gio_wakeupMainThread()
}()
}
//export gio_dispatchMainFuncs
func gio_dispatchMainFuncs() {
for {
select {
case f := <-mainFuncs:
f()
default:
return
}
}
}
+3
View File
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Unlicense OR MIT
__attribute__ ((visibility ("hidden"))) void gio_wakeupMainThread(void);
+12
View File
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Unlicense OR MIT
@import Dispatch;
#include "os_darwin.h"
#include "_cgo_export.h"
void gio_wakeupMainThread(void) {
dispatch_async(dispatch_get_main_queue(), ^{
gio_dispatchMainFuncs();
});
}