Class AppKitMainThread

java.lang.Object
panamagl.platform.macos.AppKitMainThread

public class AppKitMainThread extends Object
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.foreign API (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