From d8652dd78eed95567cb99851196b54b013e15392 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Thu, 18 Sep 2025 21:25:36 +0900 Subject: [PATCH] simplify scala.runtime.Statics.releaseFence --- library/src/scala/runtime/Statics.java | 46 +------------------------- 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/library/src/scala/runtime/Statics.java b/library/src/scala/runtime/Statics.java index 34dc1818065a..a3cc9e8ac45e 100644 --- a/library/src/scala/runtime/Statics.java +++ b/library/src/scala/runtime/Statics.java @@ -12,11 +12,6 @@ package scala.runtime; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; -import java.lang.reflect.Field; - /** Not for public consumption. Usage by the runtime only. */ @@ -145,46 +140,7 @@ private static int anyHashNumber(Number x) { // @ForceInline would be nice here. public static void releaseFence() throws Throwable { - VM.RELEASE_FENCE.invoke(); - } - - final static class VM { - static final MethodHandle RELEASE_FENCE; - - static { - RELEASE_FENCE = mkHandle(); - } - - private static MethodHandle mkHandle() { - MethodHandles.Lookup lookup = MethodHandles.lookup(); - try { - return lookup.findStatic(Class.forName("java.lang.invoke.VarHandle"), "releaseFence", MethodType.methodType(Void.TYPE)); - } catch (NoSuchMethodException | ClassNotFoundException e) { - try { - Class unsafeClass = Class.forName("sun.misc.Unsafe"); - return lookup.findVirtual(unsafeClass, "storeFence", MethodType.methodType(void.class)).bindTo(findUnsafe(unsafeClass)); - } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException e1) { - ExceptionInInitializerError error = new ExceptionInInitializerError(e1); - error.addSuppressed(e); - throw error; - } - } catch (IllegalAccessException e) { - throw new ExceptionInInitializerError(e); - } - } - - private static Object findUnsafe(Class unsafeClass) throws IllegalAccessException { - Object found = null; - for (Field field : unsafeClass.getDeclaredFields()) { - if (field.getType() == unsafeClass) { - field.setAccessible(true); - found = field.get(null); - break; - } - } - if (found == null) throw new IllegalStateException("No instance of Unsafe found"); - return found; - } + java.lang.invoke.VarHandle.releaseFence(); } /**