src/jdk.internal.vm.compiler/share/classes/jdk.internal.vm.compiler.libgraal/src/jdk/internal/vm/compiler/libgraal/LibGraal.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54601 c40b2a190173
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
    23 
    23 
    24 
    24 
    25 package jdk.internal.vm.compiler.libgraal;
    25 package jdk.internal.vm.compiler.libgraal;
    26 
    26 
    27 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
    27 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
       
    28 import jdk.vm.ci.hotspot.HotSpotSpeculationLog;
    28 import jdk.vm.ci.services.Services;
    29 import jdk.vm.ci.services.Services;
    29 
    30 
    30 /**
    31 /**
    31  * JDK13+ version of {@code LibGraal}.
    32  * JDK13+ version of {@code LibGraal}.
    32  */
    33  */
    33 public class LibGraal {
    34 public class LibGraal {
    34 
    35 
    35     public static boolean isAvailable() {
    36     public static boolean isAvailable() {
    36         return isCurrentRuntime() || libgraalIsolate != 0L;
    37         return inLibGraal() || isolate != 0L;
    37     }
    38     }
    38 
    39 
    39     public static boolean isCurrentRuntime() {
    40     public static boolean isSupported() {
       
    41         return true;
       
    42     }
       
    43 
       
    44     public static boolean inLibGraal() {
    40         return Services.IS_IN_NATIVE_IMAGE;
    45         return Services.IS_IN_NATIVE_IMAGE;
    41     }
    46     }
    42 
    47 
    43     public static long getIsolate() {
    48     public static void registerNativeMethods(HotSpotJVMCIRuntime runtime, Class<?> clazz) {
    44         if (isCurrentRuntime() || !isAvailable()) {
       
    45             throw new IllegalStateException();
       
    46         }
       
    47         return libgraalIsolate;
       
    48     }
       
    49 
       
    50     public static long getIsolateThread() {
       
    51         if (isCurrentRuntime()) {
       
    52             throw new IllegalStateException();
       
    53         }
       
    54         return CURRENT_ISOLATE_THREAD.get();
       
    55     }
       
    56 
       
    57     @SuppressWarnings("unused")
       
    58     public static long[] registerNativeMethods(HotSpotJVMCIRuntime runtime, Class<?> clazz) {
       
    59         if (clazz.isPrimitive()) {
    49         if (clazz.isPrimitive()) {
    60             throw new IllegalArgumentException();
    50             throw new IllegalArgumentException();
    61         }
    51         }
    62         if (isCurrentRuntime() || !isAvailable()) {
    52         if (inLibGraal() || !isAvailable()) {
    63             throw new IllegalStateException();
    53             throw new IllegalStateException();
    64         }
    54         }
    65         // Waiting for https://bugs.openjdk.java.net/browse/JDK-8220623
    55         runtime.registerNativeMethods(clazz);
    66         // return runtime.registerNativeMethods(clazz);
       
    67         throw new IllegalStateException("Requires JDK-8220623");
       
    68     }
    56     }
    69 
    57 
    70     @SuppressWarnings("unused")
       
    71     public static long translate(HotSpotJVMCIRuntime runtime, Object obj) {
    58     public static long translate(HotSpotJVMCIRuntime runtime, Object obj) {
    72         if (!isAvailable()) {
    59         if (!isAvailable()) {
    73             throw new IllegalStateException();
    60             throw new IllegalStateException();
    74         }
    61         }
    75         // return runtime.translate(obj);
    62         if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
    76         throw new IllegalStateException("Requires JDK-8220623");
    63             throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
       
    64         }
       
    65         return runtime.translate(obj);
    77     }
    66     }
    78 
    67 
    79     @SuppressWarnings("unused")
       
    80     public static <T> T unhand(HotSpotJVMCIRuntime runtime, Class<T> type, long handle) {
    68     public static <T> T unhand(HotSpotJVMCIRuntime runtime, Class<T> type, long handle) {
    81         if (!isAvailable()) {
    69         if (!isAvailable()) {
    82             throw new IllegalStateException();
    70             throw new IllegalStateException();
    83         }
    71         }
    84         // return runtime.unhand(type, handle);
    72         if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
    85         throw new IllegalStateException("Requires JDK-8220623");
    73             throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
       
    74         }
       
    75         return runtime.unhand(type, handle);
    86     }
    76     }
    87 
       
    88     private static final ThreadLocal<Long> CURRENT_ISOLATE_THREAD = new ThreadLocal<>() {
       
    89         @Override
       
    90         protected Long initialValue() {
       
    91             return attachThread(libgraalIsolate);
       
    92         }
       
    93     };
       
    94 
       
    95     private static final long libgraalIsolate = Services.IS_BUILDING_NATIVE_IMAGE ? 0L : initializeLibgraal();
       
    96 
    77 
    97     private static long initializeLibgraal() {
    78     private static long initializeLibgraal() {
    98         try {
    79         try {
    99             // Initialize JVMCI to ensure JVMCI opens its packages to
    80             // Initialize JVMCI to ensure JVMCI opens its packages to
   100             // Graal otherwise the call to HotSpotJVMCIRuntime.runtime()
    81             // Graal otherwise the call to HotSpotJVMCIRuntime.runtime()
   101             // below will fail on JDK13+.
    82             // below will fail on JDK13+.
   102             Services.initializeJVMCI();
    83             Services.initializeJVMCI();
   103 
    84 
   104             // Waiting for https://bugs.openjdk.java.net/browse/JDK-8220623
    85             HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
   105             // HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
    86             long[] nativeInterface = runtime.registerNativeMethods(LibGraal.class);
   106             // long[] nativeInterface = runtime.registerNativeMethods(LibGraal.class);
    87             return nativeInterface[1];
   107             // return nativeInterface[1];
       
   108             return 0L;
       
   109         } catch (UnsupportedOperationException e) {
    88         } catch (UnsupportedOperationException e) {
   110             return 0L;
    89             return 0L;
   111         }
    90         }
   112     }
    91     }
   113 
    92 
   114     /**
    93     static final long isolate = Services.IS_BUILDING_NATIVE_IMAGE ? 0L : initializeLibgraal();
   115      * Attaches the current thread to a thread in {@code isolate}.
    94 
   116      *
    95     static boolean isCurrentThreadAttached(HotSpotJVMCIRuntime runtime) {
   117      * @param isolate
    96         return runtime.isCurrentThreadAttached();
   118      */
    97     }
   119     private static native long attachThread(long isolate);
    98 
       
    99     public static boolean attachCurrentThread(HotSpotJVMCIRuntime runtime, boolean isDaemon) {
       
   100         return runtime.attachCurrentThread(isDaemon);
       
   101     }
       
   102 
       
   103     public static void detachCurrentThread(HotSpotJVMCIRuntime runtime) {
       
   104         runtime.detachCurrentThread();
       
   105     }
       
   106 
       
   107     static native long getCurrentIsolateThread(long iso);
       
   108 
       
   109     public static long getFailedSpeculationsAddress(HotSpotSpeculationLog log) {
       
   110         return log.getFailedSpeculationsAddress();
       
   111     }
   120 }
   112 }