ART: Use try-lock for interface marking
Use ObjectTryLock for the performance optimization to help code
that uses classes for locks.
Bug: 72204414
Test: art/test/testrunner/testrunner.py -b --host -t 170
Change-Id: Ic00de7bc15cb7e8369ff7c68cefcb3375f9df140
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index c667fe2..2a80e3c 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -4977,8 +4977,14 @@
// 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 1ef6289..401e852 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 0000000..619c561
--- /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 0000000..28401ad
--- /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 0000000..62f6c5c
--- /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 b8427f4..fa6ef54 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -792,7 +792,6 @@
$DEBUGGER_OPTS \
$DALVIKVM_BOOT_OPT \
$TMP_DIR_OPTION \
- -XX:DumpNativeStackOnSigQuit:false \
-cp $DEX_LOCATION/$TEST_NAME.jar$SECONDARY_DEX $MAIN $ARGS"
# Remove whitespace.