diff options
author | Alexey Ushakov <Alexey.Ushakov@jetbrains.com> | 2018-01-17 16:02:52 +0300 |
---|---|---|
committer | Alexey Ushakov <Alexey.Ushakov@jetbrains.com> | 2018-01-17 16:02:52 +0300 |
commit | 5dfb30ae68b2c54d58c98a9195709c031f823581 (patch) | |
tree | 26a67e5e37f1069403308cda46bc21fce9a229e8 | |
parent | 75037f8f87296caa15760bfaea6317c8d3b9cb8d (diff) | |
download | jdk8u_jdk-5dfb30ae68b2c54d58c98a9195709c031f823581.tar.gz |
JRE-624 CThreading.isAppKit() fails to detect main app thread if it was renamedjb8u152-b1141
Added app main thread fix
-rw-r--r-- | src/macosx/classes/sun/lwawt/macosx/CThreading.java | 25 | ||||
-rw-r--r-- | src/macosx/native/sun/osxapp/ThreadUtilities.m | 11 | ||||
-rw-r--r-- | test/jb/sun/lwawt/macosx/CThreading/IsAppKit.java | 67 |
3 files changed, 98 insertions, 5 deletions
diff --git a/src/macosx/classes/sun/lwawt/macosx/CThreading.java b/src/macosx/classes/sun/lwawt/macosx/CThreading.java index d09304edec..4dce49ed2c 100644 --- a/src/macosx/classes/sun/lwawt/macosx/CThreading.java +++ b/src/macosx/classes/sun/lwawt/macosx/CThreading.java @@ -37,17 +37,24 @@ import java.util.concurrent.FutureTask; public class CThreading { static String APPKIT_THREAD_NAME = "AWT-AppKit"; - static String FX_APPKIT_THREAD_NAME = "JavaFX Application Thread"; static boolean isEventQueue() { return EventQueue.isDispatchThread(); } + private static native boolean isMainThread(); + private static boolean isAppKit() { - return APPKIT_THREAD_NAME.equals(Thread.currentThread().getName()) || - FX_APPKIT_THREAD_NAME.equals(Thread.currentThread().getName()); + if (APPKIT_THREAD_NAME.equals(Thread.currentThread().getName())) return true; + + if (isMainThread()) { + Thread.currentThread().setName(APPKIT_THREAD_NAME); + return true; + } + return false; } + static boolean assertEventQueue() { final boolean isEventQueue = isEventQueue(); assert isEventQueue : "Threading violation: not EventQueue thread"; @@ -72,7 +79,7 @@ public class CThreading { return isNotAppKitThread; } - public static <V> V executeOnAppKit(Callable<V> command) throws Throwable { + public static <V> V executeOnAppKit(final Callable<V> command) throws Throwable { if (!isAppKit()) { Dispatch dispatch = Dispatch.getInstance(); @@ -80,7 +87,15 @@ public class CThreading { throw new AWTError("Could not get Dispatch object"); } - FutureTask<V> future = new FutureTask<>(command); + Callable<V> commandWithTNameFix = () -> { + if (!APPKIT_THREAD_NAME.equals(Thread.currentThread().getName())) { + Thread.currentThread().setName(APPKIT_THREAD_NAME); + } + + return command.call(); + }; + + FutureTask<V> future = new FutureTask<>(commandWithTNameFix); dispatch.getNonBlockingMainQueueExecutor().execute(future); diff --git a/src/macosx/native/sun/osxapp/ThreadUtilities.m b/src/macosx/native/sun/osxapp/ThreadUtilities.m index 7458431010..c00753de66 100644 --- a/src/macosx/native/sun/osxapp/ThreadUtilities.m +++ b/src/macosx/native/sun/osxapp/ThreadUtilities.m @@ -104,3 +104,14 @@ void OSXAPP_SetJavaVM(JavaVM *vm) jvm = vm; } +/* + * Class: sun_lwawt_macosx_CThreading + * Method: isMainThread + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CThreading_isMainThread + (JNIEnv *env, jclass c) +{ + return [NSThread isMainThread]; +} + diff --git a/test/jb/sun/lwawt/macosx/CThreading/IsAppKit.java b/test/jb/sun/lwawt/macosx/CThreading/IsAppKit.java new file mode 100644 index 0000000000..3029db5774 --- /dev/null +++ b/test/jb/sun/lwawt/macosx/CThreading/IsAppKit.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @test + * @summary regression test on JRE-624 CThreading.isAppKit() fails to detect main app thread if it was renamed + * @compile -XDignore.symbol.file IsAppKit.java + * @run main/othervm IsAppKit + */ + +import javax.swing.*; +import java.util.concurrent.*; +import sun.lwawt.macosx.*; + +/* + * Description: The test checks detection of the main application thread + * + */ + +public class IsAppKit { + public static void main(String[] args) throws Exception { + + final CountDownLatch counter = new CountDownLatch(1); + + SwingUtilities.invokeAndWait(() -> { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.pack(); + frame.setVisible(true); + counter.countDown(); + }); + + counter.await(); + + CThreading.privilegedExecuteOnAppKit(() -> { + String name = Thread.currentThread().getName(); + if (!"AWT-AppKit".equals(name)) { + throw new RuntimeException("Unexpected thread name: " + name); + } + + Thread.currentThread().setName("Some other thread name"); + return null; + }); + + + String name = CThreading.privilegedExecuteOnAppKit(() -> { + return Thread.currentThread().getName(); + }); + + if (!"AWT-AppKit".equals(name)) { + throw new RuntimeException("Unexpected thread name: " + name); + } + } +} |