|
12 | 12 |
|
13 | 13 | package scala.runtime;
|
14 | 14 |
|
15 |
| -import java.lang.invoke.MethodHandle; |
16 |
| -import java.lang.invoke.MethodHandles; |
17 |
| -import java.lang.invoke.MethodType; |
18 |
| -import java.lang.reflect.Field; |
19 |
| - |
20 | 15 | /** Not for public consumption. Usage by the runtime only.
|
21 | 16 | */
|
22 | 17 |
|
@@ -145,46 +140,7 @@ private static int anyHashNumber(Number x) {
|
145 | 140 |
|
146 | 141 | // @ForceInline would be nice here.
|
147 | 142 | public static void releaseFence() throws Throwable {
|
148 |
| - VM.RELEASE_FENCE.invoke(); |
149 |
| - } |
150 |
| - |
151 |
| - final static class VM { |
152 |
| - static final MethodHandle RELEASE_FENCE; |
153 |
| - |
154 |
| - static { |
155 |
| - RELEASE_FENCE = mkHandle(); |
156 |
| - } |
157 |
| - |
158 |
| - private static MethodHandle mkHandle() { |
159 |
| - MethodHandles.Lookup lookup = MethodHandles.lookup(); |
160 |
| - try { |
161 |
| - return lookup.findStatic(Class.forName("java.lang.invoke.VarHandle"), "releaseFence", MethodType.methodType(Void.TYPE)); |
162 |
| - } catch (NoSuchMethodException | ClassNotFoundException e) { |
163 |
| - try { |
164 |
| - Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); |
165 |
| - return lookup.findVirtual(unsafeClass, "storeFence", MethodType.methodType(void.class)).bindTo(findUnsafe(unsafeClass)); |
166 |
| - } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException e1) { |
167 |
| - ExceptionInInitializerError error = new ExceptionInInitializerError(e1); |
168 |
| - error.addSuppressed(e); |
169 |
| - throw error; |
170 |
| - } |
171 |
| - } catch (IllegalAccessException e) { |
172 |
| - throw new ExceptionInInitializerError(e); |
173 |
| - } |
174 |
| - } |
175 |
| - |
176 |
| - private static Object findUnsafe(Class<?> unsafeClass) throws IllegalAccessException { |
177 |
| - Object found = null; |
178 |
| - for (Field field : unsafeClass.getDeclaredFields()) { |
179 |
| - if (field.getType() == unsafeClass) { |
180 |
| - field.setAccessible(true); |
181 |
| - found = field.get(null); |
182 |
| - break; |
183 |
| - } |
184 |
| - } |
185 |
| - if (found == null) throw new IllegalStateException("No instance of Unsafe found"); |
186 |
| - return found; |
187 |
| - } |
| 143 | + java.lang.invoke.VarHandle.releaseFence(); |
188 | 144 | }
|
189 | 145 |
|
190 | 146 | /**
|
|
0 commit comments