diff options
| -rw-r--r-- | runtime/class_linker.cc | 10 | ||||
| -rw-r--r-- | test/001-HelloWorld/src/Main.java | 33 | ||||
| -rw-r--r-- | test/170-interface-init/expected.txt | 1 | ||||
| -rw-r--r-- | test/170-interface-init/info.txt | 2 | ||||
| -rw-r--r-- | test/170-interface-init/src/Main.java | 50 | ||||
| -rwxr-xr-x | test/etc/run-test-jar | 1 |
6 files changed, 92 insertions, 5 deletions
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc index c667fe2f30..2a80e3c609 100644 --- a/runtime/class_linker.cc +++ b/runtime/class_linker.cc @@ -4977,8 +4977,14 @@ bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, // interface since we can still avoid the traversal. This is purely a performance optimization. if (result) { // TODO This should be done in a better way - ObjectLock<mirror::Class> lock(self, iface); - iface->SetRecursivelyInitialized(); + // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this + // interface. It is bad (Java) style, but not impossible. Marking the recursive + // initialization is a performance optimization (to avoid another idempotent visit + // for other implementing classes/interfaces), and can be revisited later. + ObjectTryLock<mirror::Class> lock(self, iface); + if (lock.Acquired()) { + iface->SetRecursivelyInitialized(); + } } return result; } diff --git a/test/001-HelloWorld/src/Main.java b/test/001-HelloWorld/src/Main.java index 1ef6289559..401e8529cd 100644 --- a/test/001-HelloWorld/src/Main.java +++ b/test/001-HelloWorld/src/Main.java @@ -14,8 +14,37 @@ * limitations under the License. */ +import java.util.concurrent.*; + +interface I { +} + +class A implements I { + static int x = (int)(10*Math.random()); // Suppress initialization. +} + public class Main { - public static void main(String[] args) { - System.out.println("Hello, world!"); + public static void main(String[] args) throws Exception { + + final CountDownLatch first = new CountDownLatch(1); + final CountDownLatch second = new CountDownLatch(1); + + new Thread(new Runnable() { + public void run() { + try { + synchronized(I.class) { + first.countDown(); + second.await(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + + first.await(); + new A(); + second.countDown(); + System.out.println("Hello, world!"); } } diff --git a/test/170-interface-init/expected.txt b/test/170-interface-init/expected.txt new file mode 100644 index 0000000000..619c56180b --- /dev/null +++ b/test/170-interface-init/expected.txt @@ -0,0 +1 @@ +Done. diff --git a/test/170-interface-init/info.txt b/test/170-interface-init/info.txt new file mode 100644 index 0000000000..28401adf08 --- /dev/null +++ b/test/170-interface-init/info.txt @@ -0,0 +1,2 @@ +Check that holding a lock on an interface class does not block initialization of an +implementing class. diff --git a/test/170-interface-init/src/Main.java b/test/170-interface-init/src/Main.java new file mode 100644 index 0000000000..62f6c5cab2 --- /dev/null +++ b/test/170-interface-init/src/Main.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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. + */ + +import java.util.concurrent.CountDownLatch; + +interface I { +} + +class A implements I { + static int x = (int)(10*Math.random()); // Suppress compile-time initialization. +} + +public class Main { + public static void main(String[] args) throws Exception { + final CountDownLatch first = new CountDownLatch(1); + final CountDownLatch second = new CountDownLatch(1); + + new Thread(new Runnable() { + public void run() { + try { + synchronized(I.class) { + first.countDown(); + second.await(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + + first.await(); + new A(); // Will initialize A. + second.countDown(); + + System.out.println("Done."); + } +} diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar index b8427f491b..fa6ef54c93 100755 --- a/test/etc/run-test-jar +++ b/test/etc/run-test-jar @@ -792,7 +792,6 @@ dalvikvm_cmdline="$INVOKE_WITH $GDB $ANDROID_ROOT/bin/$DALVIKVM \ $DEBUGGER_OPTS \ $DALVIKVM_BOOT_OPT \ $TMP_DIR_OPTION \ - -XX:DumpNativeStackOnSigQuit:false \ -cp $DEX_LOCATION/$TEST_NAME.jar$SECONDARY_DEX $MAIN $ARGS" # Remove whitespace. |