--- old/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java 2016-06-30 14:24:16.413772291 +0200 +++ new/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java 2016-06-30 14:24:16.353771941 +0200 @@ -35,11 +35,15 @@ package java.util.concurrent.atomic; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.VarHandle; import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; import java.util.function.DoubleBinaryOperator; import java.util.function.LongBinaryOperator; +import jdk.internal.misc.SharedSecrets; + /** * A package-local class holding common representation and mechanics * for classes supporting dynamic striping on 64bit values. The class @@ -123,22 +127,21 @@ volatile long value; Cell(long x) { value = x; } final boolean cas(long cmp, long val) { - return U.compareAndSwapLong(this, VALUE, cmp, val); + return VALUE.compareAndSet(this, cmp, val); } final void reset() { - U.putLongVolatile(this, VALUE, 0L); + VALUE.setVolatile(this, 0L); } final void reset(long identity) { - U.putLongVolatile(this, VALUE, identity); + VALUE.setVolatile(this, identity); } - // Unsafe mechanics - private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); - private static final long VALUE; + // VarHandle mechanics + private static final VarHandle VALUE; static { try { - VALUE = U.objectFieldOffset - (Cell.class.getDeclaredField("value")); + MethodHandles.Lookup l = MethodHandles.lookup(); + VALUE = l.findVarHandle(Cell.class, "value", long.class); } catch (ReflectiveOperationException e) { throw new Error(e); } @@ -174,14 +177,14 @@ * CASes the base field. */ final boolean casBase(long cmp, long val) { - return U.compareAndSwapLong(this, BASE, cmp, val); + return BASE.compareAndSet(this, cmp, val); } /** * CASes the cellsBusy field from 0 to 1 to acquire lock. */ final boolean casCellsBusy() { - return U.compareAndSwapInt(this, CELLSBUSY, 0, 1); + return CELLSBUSY.compareAndSet(this, 0, 1); } /** @@ -189,7 +192,7 @@ * Duplicated from ThreadLocalRandom because of packaging restrictions. */ static final int getProbe() { - return U.getInt(Thread.currentThread(), PROBE); + return (int) PROBE.get(Thread.currentThread()); } /** @@ -201,7 +204,7 @@ probe ^= probe << 13; // xorshift probe ^= probe >>> 17; probe ^= probe << 5; - U.putInt(Thread.currentThread(), PROBE, probe); + PROBE.set(Thread.currentThread(), probe); return probe; } @@ -371,20 +374,16 @@ } } - // Unsafe mechanics - private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); - private static final long BASE; - private static final long CELLSBUSY; - private static final long PROBE; + // VarHandle mechanics + private static final VarHandle BASE; + private static final VarHandle CELLSBUSY; + private static final VarHandle PROBE; static { try { - BASE = U.objectFieldOffset - (Striped64.class.getDeclaredField("base")); - CELLSBUSY = U.objectFieldOffset - (Striped64.class.getDeclaredField("cellsBusy")); - - PROBE = U.objectFieldOffset - (Thread.class.getDeclaredField("threadLocalRandomProbe")); + MethodHandles.Lookup l = MethodHandles.lookup(); + BASE = l.findVarHandle(Striped64.class, "base", long.class); + CELLSBUSY = l.findVarHandle(Striped64.class, "cellsBusy", int.class); + PROBE = SharedSecrets.getJavaLangThreadAccess().threadLocalRandomProbeVH(); } catch (ReflectiveOperationException e) { throw new Error(e); }