--- jsr166/src/jsr166e/Striped64.java 2011/08/05 17:08:04 1.3 +++ jsr166/src/jsr166e/Striped64.java 2013/01/14 01:59:15 1.8 @@ -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 { /* @@ -42,7 +40,7 @@ abstract class Striped64 extends Number * * A single spinlock ("busy") is used for initializing and * resizing the table, as well as populating slots with new Cells. - * There is no need for a blocking lock: When the lock is not + * There is no need for a blocking lock; when the lock is not * available, threads try other slots (or the base). During these * retries, there is increased contention and reduced locality, * which is still better than alternatives. @@ -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. @@ -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()); } } - }