changeset 22462:7a7dd51e7e0b

Ensure that calling HotSpotJVMCIRuntime.runtime() initializes JVMCI correctly
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Mon, 24 Aug 2015 19:16:03 -0700
parents 07d9f0909560
children d9f5c93a83d3
files jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotJVMCICompilerConfig.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotJVMCIRuntime.java
diffstat 2 files changed, 32 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotJVMCICompilerConfig.java	Mon Aug 24 19:14:50 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotJVMCICompilerConfig.java	Mon Aug 24 19:16:03 2015 -0700
@@ -64,6 +64,7 @@
      * utility function used to call other static initialization methods.
      */
     static Boolean selectCompiler(String compilerName) {
+        assert compilerFactory == null;
         for (CompilerFactory factory : Services.load(CompilerFactory.class)) {
             if (factory.getCompilerName().equals(compilerName)) {
                 compilerFactory = factory;
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotJVMCIRuntime.java	Mon Aug 24 19:14:50 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotJVMCIRuntime.java	Mon Aug 24 19:16:03 2015 -0700
@@ -40,23 +40,37 @@
 
 public final class HotSpotJVMCIRuntime implements HotSpotJVMCIRuntimeProvider, HotSpotProxified {
 
-    private static final HotSpotJVMCIRuntime instance;
+    /**
+     * The proper initialization of this class is complex because it's tangled up with the
+     * initialization of the JVMCI and really should only ever be triggered through
+     * {@link JVMCI#getRuntime}. However since {@link #runtime} can also be called directly it
+     * should also trigger proper initialization. To ensure proper ordering, the static initializer
+     * of this class initializes {@link JVMCI} and then access to {@link Once#instance} triggers the
+     * final initialization of the {@link HotSpotJVMCIRuntime}.
+     */
+    static {
+        JVMCI.initialize();
+    }
 
-    static {
-        try (InitTimer t0 = timer("HotSpotJVMCIRuntime.<clinit>")) {
-            try (InitTimer t = timer("StartupEventListener.beforeJVMCIStartup")) {
-                for (StartupEventListener l : Services.load(StartupEventListener.class)) {
-                    l.beforeJVMCIStartup();
+    static class DelayedInit {
+        private static final HotSpotJVMCIRuntime instance;
+
+        static {
+            try (InitTimer t0 = timer("HotSpotJVMCIRuntime.<clinit>")) {
+                try (InitTimer t = timer("StartupEventListener.beforeJVMCIStartup")) {
+                    for (StartupEventListener l : Services.load(StartupEventListener.class)) {
+                        l.beforeJVMCIStartup();
+                    }
                 }
-            }
 
-            try (InitTimer t = timer("HotSpotJVMCIRuntime.<init>")) {
-                instance = new HotSpotJVMCIRuntime();
-            }
+                try (InitTimer t = timer("HotSpotJVMCIRuntime.<init>")) {
+                    instance = new HotSpotJVMCIRuntime();
+                }
 
-            try (InitTimer t = timer("HotSpotJVMCIRuntime.completeInitialization")) {
-                // Why deferred initialization? See comment in completeInitialization().
-                instance.completeInitialization();
+                try (InitTimer t = timer("HotSpotJVMCIRuntime.completeInitialization")) {
+                    // Why deferred initialization? See comment in completeInitialization().
+                    instance.completeInitialization();
+                }
             }
         }
     }
@@ -65,14 +79,15 @@
      * Gets the singleton {@link HotSpotJVMCIRuntime} object.
      */
     public static HotSpotJVMCIRuntime runtime() {
-        assert instance != null;
-        return instance;
+        assert DelayedInit.instance != null;
+        return DelayedInit.instance;
     }
 
     /**
      * Do deferred initialization.
      */
     public void completeInitialization() {
+        compiler = HotSpotJVMCICompilerConfig.getCompilerFactory().createCompiler(this);
 
         // Proxies for the VM/Compiler interfaces cannot be initialized
         // in the constructor as proxy creation causes static
@@ -80,7 +95,6 @@
         // proxied methods. Some of these static initializers (e.g. in
         // HotSpotMethodData) rely on the static 'instance' field being set
         // to retrieve configuration details.
-        compiler = HotSpotJVMCICompilerConfig.getCompilerFactory().createCompiler(this);
 
         CompilerToVM toVM = this.compilerToVm;
 
@@ -113,7 +127,7 @@
      * Gets the kind of a word value on the {@linkplain #getHostJVMCIBackend() host} backend.
      */
     public static Kind getHostWordKind() {
-        return instance.getHostJVMCIBackend().getCodeCache().getTarget().wordKind;
+        return runtime().getHostJVMCIBackend().getCodeCache().getTarget().wordKind;
     }
 
     protected/* final */CompilerToVM compilerToVm;