From 0fc0b0d5da38bc3a73689b95626861ca9d70e5de Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 24 Feb 2020 05:33:58 +0100 Subject: IOSUtil.IsMainThread(): Utilize caching ThreadLocal like OSXUtil's variant. --- .../classes/jogamp/nativewindow/ios/IOSUtil.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nativewindow/classes/jogamp/nativewindow/ios/IOSUtil.java b/src/nativewindow/classes/jogamp/nativewindow/ios/IOSUtil.java index 4790f21da..66669d664 100644 --- a/src/nativewindow/classes/jogamp/nativewindow/ios/IOSUtil.java +++ b/src/nativewindow/classes/jogamp/nativewindow/ios/IOSUtil.java @@ -44,6 +44,8 @@ public class IOSUtil implements ToolkitProperties { private static boolean isInit = false; private static final boolean DEBUG = Debug.debug("IOSUtil"); + private static final ThreadLocal tlsIsMainThread = new ThreadLocal(); + /** FIXME HiDPI: OSX unique and maximum value {@value} */ public static final int MAX_PIXELSCALE = 2; @@ -290,8 +292,22 @@ public class IOSUtil implements ToolkitProperties { } } + /** + * Returns true if the current is the UIApplication main-thread. + *

+ * Implementation utilizes a {@link ThreadLocal} storage boolean holding the answer, + * which only gets set at the first call from each individual thread. + * This minimizes unnecessary native callbacks. + *

+ * @return {@code true} if current thread is the UIApplication main-thread, otherwise false. + */ public static boolean IsMainThread() { - return IsMainThread0(); + Boolean isMainThread = tlsIsMainThread.get(); + if( null == isMainThread ) { + isMainThread = new Boolean(IsMainThread0()); + tlsIsMainThread.set(isMainThread); + } + return isMainThread.booleanValue(); } /** Returns the screen refresh rate in Hz. If unavailable, returns 60Hz. */ -- cgit v1.2.3