Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 1 addition & 45 deletions library/src/scala/runtime/Statics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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();
}

/**
Expand Down
Loading