--- jsr166/src/jsr166e/Striped64.java 2011/08/02 18:22:20 1.2 +++ jsr166/src/jsr166e/Striped64.java 2013/01/09 02:51:37 1.7 @@ -6,13 +6,11 @@ package jsr166e; import java.util.Random; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; /** * A package-local class holding common representation and mechanics * for classes supporting dynamic striping on 64bit values. The class - * extends Number so that concrete subclasses can publicly do so. + * extends Number so that concrete subclasses must publicly do so. */ abstract class Striped64 extends Number { /* @@ -79,9 +77,9 @@ abstract class Striped64 extends Number */ /** - * Padded variant of AtomicLong supporting only raw accesses plus - * CAS The value field is placed between pads, hoping that the JVM - * doesn't reorder them. + * Padded variant of AtomicLong supporting only raw accesses plus CAS. + * The value field is placed between pads, hoping that the JVM doesn't + * reorder them. * * JVM intrinsics note: It would be possible to use a release-only * form of CAS here, if it were provided. @@ -140,7 +138,7 @@ abstract class Striped64 extends Number */ static final ThreadHashCode threadHashCode = new ThreadHashCode(); - /** Nomber of CPUS, to place bound on table size */ + /** Number of CPUS, to place bound on table size */ static final int NCPU = Runtime.getRuntime().availableProcessors(); /** @@ -150,7 +148,7 @@ abstract class Striped64 extends Number /** * Base value, used mainly when there is no contention, but also as - * a fallback during table initializion races. Updated via CAS. + * a fallback during table initialization races. Updated via CAS. */ transient volatile long base; @@ -166,14 +164,14 @@ abstract class Striped64 extends Number } /** - * CAS the base field + * CASes the base field. */ final boolean casBase(long cmp, long val) { return UNSAFE.compareAndSwapLong(this, baseOffset, cmp, val); } /** - * CAS the busy field from 0 to 1 to acquire lock. + * CASes the busy field from 0 to 1 to acquire lock. */ final boolean casBusy() { return UNSAFE.compareAndSwapInt(this, busyOffset, 0, 1); @@ -279,7 +277,7 @@ abstract class Striped64 extends Number /** - * Set base and all cells to the given value + * Sets base and all cells to the given value. */ final void internalReset(long initialValue) { Cell[] as = cells; @@ -321,22 +319,23 @@ abstract class Striped64 extends Number private static sun.misc.Unsafe getUnsafe() { try { return sun.misc.Unsafe.getUnsafe(); - } catch (SecurityException se) { - try { - return java.security.AccessController.doPrivileged - (new java.security - .PrivilegedExceptionAction() { - public sun.misc.Unsafe run() throws Exception { - java.lang.reflect.Field f = sun.misc - .Unsafe.class.getDeclaredField("theUnsafe"); - f.setAccessible(true); - return (sun.misc.Unsafe) f.get(null); - }}); - } catch (java.security.PrivilegedActionException e) { - throw new RuntimeException("Could not initialize intrinsics", - e.getCause()); - } + } catch (SecurityException tryReflectionInstead) {} + try { + return java.security.AccessController.doPrivileged + (new java.security.PrivilegedExceptionAction() { + public sun.misc.Unsafe run() throws Exception { + Class k = sun.misc.Unsafe.class; + for (java.lang.reflect.Field f : k.getDeclaredFields()) { + f.setAccessible(true); + Object x = f.get(null); + if (k.isInstance(x)) + return k.cast(x); + } + throw new NoSuchFieldError("the Unsafe"); + }}); + } catch (java.security.PrivilegedActionException e) { + throw new RuntimeException("Could not initialize intrinsics", + e.getCause()); } } - }