Files

46 lines
953 B
Go
Raw Permalink Normal View History

2023-01-18 21:42:49 +11:00
//go:build darwin
package application
/*
#cgo CFLAGS: -mmacosx-version-min=10.13 -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include "Cocoa/Cocoa.h"
extern void dispatchOnMainThreadCallback(unsigned int);
static void dispatchOnMainThread(unsigned int id) {
dispatch_async(dispatch_get_main_queue(), ^{
dispatchOnMainThreadCallback(id);
});
}
2023-06-16 20:48:57 +10:00
static bool onMainThread() {
return [NSThread isMainThread];
}
2023-01-18 21:42:49 +11:00
*/
import "C"
2023-06-16 20:48:57 +10:00
func (m *macosApp) isOnMainThread() bool {
return bool(C.onMainThread())
}
2023-01-18 21:42:49 +11:00
func (m *macosApp) dispatchOnMainThread(id uint) {
C.dispatchOnMainThread(C.uint(id))
}
//export dispatchOnMainThreadCallback
func dispatchOnMainThreadCallback(callbackID C.uint) {
mainThreadFunctionStoreLock.RLock()
id := uint(callbackID)
fn := mainThreadFunctionStore[id]
if fn == nil {
2023-02-06 20:50:11 +11:00
Fatal("dispatchCallback called with invalid id: %v", id)
2023-01-18 21:42:49 +11:00
}
delete(mainThreadFunctionStore, id)
mainThreadFunctionStoreLock.RUnlock()
fn()
}