Package panamagl.platform.macos
Class AppKitMainThread
java.lang.Object
panamagl.platform.macos.AppKitMainThread
Provides main thread detection and dispatch on macOS using only the Panama FFI — no JNI and
no native companion library required.
This is functionally equivalent to JOGL's OSXUtil.IsMainThread() and
OSXUtil.RunOnMainThread(), with the following advantages:
- No dependency on any external native library.
- Implemented entirely with the standard
java.lang.foreignAPI (Panama FFI). - Consistent with the rest of PanamaGL's approach of calling native APIs directly from Java.
Uses two macOS system APIs from libSystem:
pthread_main_np()— returns 1 if the calling thread is the main POSIX thread.dispatch_sync_f(queue, context, work)— synchronously dispatches a C function pointer to a GCD queue and blocks until it completes.
Important limitation
runOnMainThread(Runnable) uses dispatch_sync_f on the main dispatch queue.
This requires the main thread to be running a CFRunLoop or pumping its dispatch queue.
If the main thread is idle (e.g., main() has returned without entering a run loop),
the call will deadlock.
Calling runOnMainThread while already on the main thread is safe — the task is
executed directly to avoid deadlock.
- Author:
- Martin Pernollet
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanReturnstrueif the calling thread is the macOS main (AppKit) thread.static voidrunOnMainThread(Runnable task) Executes the given task on the macOS main thread and blocks until it completes.
-
Method Details
-
isMainThread
public static boolean isMainThread()Returnstrueif the calling thread is the macOS main (AppKit) thread. -
runOnMainThread
Executes the given task on the macOS main thread and blocks until it completes.If the calling thread is already the main thread, the task is executed directly to avoid a deadlock (dispatching synchronously to the main queue from the main thread would block forever).
- Parameters:
task- the task to run on the main thread- Throws:
RuntimeException- if the native dispatch call fails or the task throws
-