Skip to content

Commit

Permalink
[Truffle] Tidy up UnsafeHolder.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 10, 2016
1 parent c9eb0d8 commit fd6e410
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 52 deletions.
Expand Up @@ -28,11 +28,8 @@
// Checkstyle: stop

final class Target_org_jruby_truffle_util_UnsafeHolder {
static boolean SUPPORTS_FENCES = false;
static long ARRAY_OBJECT_BASE_OFFSET;
static long ARRAY_OBJECT_INDEX_SCALE;

static void fullFence() {
throw new UnsupportedOperationException();
}
}

Expand Down
Expand Up @@ -113,11 +113,7 @@ public abstract static class FullMemoryBarrierPrimitiveNode extends CoreMethodNo

@Specialization
public Object fullMemoryBarrier() {
if (UnsafeHolder.SUPPORTS_FENCES) {
UnsafeHolder.fullFence();
} else {
throw new UnsupportedOperationException();
}
UnsafeHolder.fullFence();
return nil();
}
}
Expand Down
47 changes: 4 additions & 43 deletions truffle/src/main/java/org/jruby/truffle/util/UnsafeHolder.java
Expand Up @@ -27,61 +27,22 @@
package org.jruby.truffle.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public final class UnsafeHolder {

private UnsafeHolder(){}

/**
* Holds a reference to Unsafe if available, null otherwise.
*/

public static final sun.misc.Unsafe U = loadUnsafe();

public static final boolean SUPPORTS_FENCES = supportsFences();
public static final long ARRAY_OBJECT_BASE_OFFSET = arrayObjectBaseOffset();
public static final long ARRAY_OBJECT_INDEX_SCALE = arrayObjectIndexScale();


private static sun.misc.Unsafe loadUnsafe() {
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (sun.misc.Unsafe) f.get(null);
} catch (Exception e) {
return null;
} catch (NoClassDefFoundError ncdfe) {
// Google AppEngine raises NCDFE for Unsafe rather than CNFE
} catch (Throwable e) {
return null;
}
}

private static long arrayObjectBaseOffset() {
if(U == null)
return 0;
return U.arrayBaseOffset(Object[].class);
}

private static long arrayObjectIndexScale() {
if(U == null)
return 0;
return U.arrayIndexScale(Object[].class);
}

private static boolean supportsFences() {
if(U == null)
return false;
try {
Method m = U.getClass().getDeclaredMethod("fullFence");
if(m != null)
return true;
} catch (Exception e) {
}
return false;
}

//// The following methods are Java8 only. They will throw undefined method errors if invoked without checking for fence support


public static void fullFence() {
U.fullFence();
}
Expand Down

0 comments on commit fd6e410

Please sign in to comment.